Reputation: 382
I'm a relatively experienced developer (graduated recently), trying to get back into discord bots after I made a few a couple of years ago.
I've been following the guide on discordjs.guide, but even with the basic "hello world" style program, I am already stuck.
The program gives no errors when running, but no interactions come through in the console.
process.env.BOT_TOKEN
is the correct value.client.login
runs successfully.Is there something super obvious I'm not understanding/seeing? Otherwise, what else should I try as a troubleshooting step?
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', interaction => {
console.log(interaction);
});
client.login(process.env.BOT_TOKEN);
package.json:
{
"name": "timebot",
"version": "1.0.0",
"description": "",
"main": "start.js",
"scripts": {
"start": "node -r dotenv/config start.js dotenv_config_path=secrets.env",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^13.1.0",
"dotenv": "^10.0.0"
}
}
Upvotes: 6
Views: 6511
Reputation: 382
Turns out, messages are not classified as "interactions" by discord.js. Additionally, you have to specify the intent to listen for messages, otherwise, the event won't be passed to your bot. Here is the revised code with those two critical changes:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES] });
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', interaction => {
console.log(interaction);
});
client.on("messageCreate", message => {
console.log(message);
});
client.login(process.env.BOT_TOKEN);
Upvotes: 10