I'm making my first Discord bot and I'm following this tutorial playlist here:
But for some reason my bot will load, say the load message, and even set its custom status but none of the commands are working. I have looked at many other forum posts and none are my problem so I'm very confused. I'm using Discord.js 14.3.0 and Node.js 16.17.0 on my Windows 10 machine. Here is my code:
const { Client, Discord, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
],
});
const token = ("REDACTED");
const prefix = '!';
client.once('ready', () => {
console.log('Bot is online!')
client.user.setActivity('Among Us');
});
client.on('message', message => {
if (message.content === prefix 'ping') {
message.channel.send('Loading data').then(async(msg) => {
msg.delete()
message.channel.send(`Latency is ${msg.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
})
}
});
client.login(token);
Also I have modified a few things and tried some other peoples code and I get no errors, it just won't work
CodePudding user response:
Bots now need a special intent to be able to read messages.
To activate the intent, first go to your Developer Portal, select your application and activate the Message Content Intent. Next add GatewayIntentBits.MessageContent
to your intent list to be able to use it.
Note that if your bot is on 100 or more servers it needs to be verified in order to access intents. More information about the change can be found here.
CodePudding user response:
Discord won't allow bots to read the messages anymore, unless you have a specific permission that is only granted to verified developers. The reason it doesn't work is probably because the rule is now effective.
Actually it's a permission to enable on the developer portal, my bad for this.
Although, the new way of creating commands are the slash commands. Discord.js provided a new guide, very complete on how to use them. They are recommended as they will allow you to do many more things
Note that you will have to register the commands when developing your bot.