Discord.js not logging in - javascript

I cannot figure out why my bot is not working. Can someone please tell me why it is not logging in?? It shows the logo (the last console.log line), but does not say, "logged in as foo#1234". (I am not getting any errors as well.)
code removed because of answered question. the code worked fine ;)

Use client.once(‘ready’, async () => {//code here}); as a a pose to client.on()

As mentioned on discord.js Guide, here's the base code to get you started:
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// Login to Discord with your client's token
client.login(token);
Open your terminal and run node index.js (assuming that your file name is index.js) to start the process. If you see "Ready!" after a few seconds, you're good to go!
I recommend for you to follow this guide, it helped me a lot with the first steps with the discord bot.

The Token had reset for some reason, I do not remember resetting it, but after updating the token, it worked fine. Thank you for your answers and helpful comments.
If somebody else is having this problem, try resetting the token again.

Related

Deploy whatsapp-web bot on heroku

I'm creating a whatsapp bot using the node library whatsapp-web.js After I'm done with the script it looks something like (I just put a overview of the orignal script) -
index.js
const {Client, LocalAuth, MessageMedia } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const client = new Client({
puppeteer: {
args: ['--no-sandbox', "--disable-setuid-sandbox"]
},
authStrategy: new LocalAuth()
});
client.on('qr', (qr) => {
console.log('qr received: ', qr);
qrcode.generate(qr, {small:true});
});
client.on('ready', () => {
console.log('READY');
});
client.on('message', async msg => {
let type = msg.type;
let chat = await msg.getChat();
if(chat.isGroup) {
//do something
}else {
//
if(msg.body === "ping") {
msg.reply("pong");
}
}
});
Everything is fine with the script and it works good on linux or ubuntu (I already added puppeteer build pack on that Heroku app). As I need to run that script continuously I decided to put that on a worker process.
Procfile
worker: node index.js
But now the problem comes in role, how can I authenticate here? I decided to remove that line from index.js
qrcode.generate(qr,{small:true});
And insted I thought I will print all the logs on heroku-cli
heroku logs -a wweb-bot
#my app named as wweb-bot
and from there access the key generated as qr. After that I'll turn it into a qrcode and scan it. When I did all setup and try it I was getting a continuously generating logs of qr keys. It's nonstop, and keep generating keys after every 15-20 sec. What's the problem here? Is it because Heroku has a read only environment or anything else is missing? Please help me how can i do it
remove or comment this code
// authStrategy: new LocalAuth()
it will not work on heroku
but as the code is on server, you don't need to scan again and again, you need to scan only you restart your server
but if you are facing puppeteer error then add these buildpacks in heroku /your project/settings/ scrol down to adduildpack
add these two buildpacks
https://github.com/jontewks/puppeteer-heroku-buildpack
https://github.com/heroku/heroku-buildpack-google-chrome
then redeploy your app
Edit: now whatsapp-web.js added new functionality of doing this called RemoteAuthStatergy just go throughout it.

How do I get my Discord bot to send a welcome message?

I've watched numerous videos, and followed examples online, and still can't get my discord bot to simply send a message upon a member joining. Maybe I'm missing out on an important update?
//require the necessary discord.js classes
const { Client, Intents } = require("discord.js");
const { token } = require("./config.json");
//create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
//when the client is ready, run this code (only once)
client.on('ready', () => {
console.log("Online");
});
//sends user a welcome message upon joining
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.get('921490782958014576');
channel.send("Welcome to the server!!")
});
client.login(token);
I get nothing. Nada. I've tried several different pieces of code. I've TRIPLE checked the permissions. This is very frustrating. I've tried unadding and readding the bot to the server. I checked up on the discord.js documentation and nothing seems to be wrong with my code. To me it seems as if the guildMemberAdd isn't emitting anything. I'm using notepad++ to edit my code. I feel I could be using something better for debugging purposes possibly.
The issue may be due to missing intents. Try adding the GUILD_MEMBERS intent to your list of intents like so.
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] });
discord.js docs: https://discord.js.org/#/docs/main/stable/class/Intents?scrollTo=s-FLAGS
Developer docs: https://discord.com/developers/docs/topics/gateway#gateway-intents

Welcome message not sending through Discord.js

So I am coding a discord bot with discord.js and one of the features is that when a new user joins the server the bot sends a message. I am not making it an embed for now, I can do that myself later.
Here is my main welcome.js file specifically made for this feature:
module.exports = (client) => {
const channelId = '868192573011931185' //welcome channel
client.on('guildMemberAdd', (member) => {
console.log(member.name)
const message = `Hello <#${member.id}> and welcome to ${guild.name}!
Please verify through the Server Captcha Bot in your DMs,
then read the rules in <#868192573263605884>
and lastly pick your roles in <#868192573263605880>!`
const channel = member.guild.channels.cache.get(channelId)
channel.send(message)
})
}
And in my index.js file I just imported the file, and added welcome(client).
But for some reason my code is not showing up in the Welcome channel... Any help will be appreciated.
Check your intents are open and check this post for further information:
I don't know the context of your index.js but I suggest reading through this handy guide where they explain how you can handle events in seperate files.
Basically they loop through an events folder(where your welcome.js would be).
Then they dynamically register the events that the client object receives to execute through those event files.
Link: Event handling.
In your code, instead of saying
const channel = member.guild.channels.cache.get(channelId)
try say:
const channel = Client.channels.cache.get(channelId)
Hope it works :)

Commands that don't require prefixes not working. Discord.js

I've been coding a Discord bot using js for a couple days now and most everything has been working fine. I am trying to get my bot to respond to a code word but the command isn't working unless I use my prefix which is !.
I've tried pretty much every variation of the code, but none is working.
Example of the command:
if(message.content.includes('ping')) {
message.reply('Pong');
}
if your code looks like
const Discord = require('discord.js');
const client = new Discord.Client();
let token = "your token here";
client.on('message', message => {
if(message.content.includes('ping')) {
message.reply('Pong');
}
});
client.login(token)
and nothing happens when you send 'ping', then it looks like some technical issue. Otherwise, you have to send the whole code so people can take a look and tell you what is wrong.

Discord Bot Node JS simple error .send

I'm having a weird issue while following this tutorial. Building a JS Discord bot, literally only 33 lines in and its throwing errors about .send being undefined. I've googled around and I can't find anything that has helped get any closer to working this out.
const fs = require("fs");
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
client.login(config.token);
client.on("ready", () => {
client.user.setGame(`on ${client.guilds.size} servers`);
console.log(`Ready to serve on ${client.guilds.size} servers, for ${client.users.size} users.`);
});
client.on("guildMemberAdd", (member) => {
console.log(`New User ${member.user.username} has joined ${member.guild.name}` );
member.guild.defaultChannel.send(`${member.user} has joined this server`);
});
client.on("message", (message) => {
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
if (message.content.startsWith(config.prefix + "ping")) {
message.channel.send("pong!");
} else
if (message.content.startsWith(config.prefix + "foo")) {
message.channel.send("bar!");
}
});
client.on("error", (e) => console.error(e));
client.on("warn", (e) => console.warn(e));
client.on("debug", (e) => console.info(e));
When ran, the console.log works without fuss, but the message to default channel throws the following error in PowerShell
C:\Users\super\Desktop\autoslap\mybot.js:18
member.guild.defaultChannel.send(`${member.user} has joined this server`);
^
TypeError: Cannot read property 'send' of undefined
Any help would be appreciated, getting frustrated over what is probably something so simple.
I know this is a late reply and you might've already figured out how to do this, but I will still explain to those who may need help.
As of 03/08/2017, there is no more Default Channel in guilds on
Discord. The #general default channel can be deleted, and the
guild.defaultChannel property no longer works - From
https://anidiots.guide/frequently-asked-questions.html
If you want an alternative, the code from https://anidiots.guide/frequently-asked-questions.html may do the trick. Just enter the site and scroll down until you see Default Channel!
If your bot has admin perms, its "first writable channel" is the one at the top. It could be any channel, so if their default channel was deleted you can potentially annoy a lot of people.
Hope this helps!
javascript node.js discord discord.js
You would get that error if the default channel of the server has been deleted. Previously, you weren't able to delete the default channel but now you can. To be sure, make a new server and try it there.

Categories

Resources