最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Discord Bot Not Receiving Interactions - Stack Overflow

programmeradmin1浏览0评论

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 e through in the console.

  • When I start the script, the bot switches to "online." Curiously, the bot does not switch back to "offline" until I regenerate the token. Not sure if that is related.
  • I've verified process.env.BOT_TOKEN is the correct value.
  • I've confirmed client.login runs successfully.
  • I've tried DMing the bot directly
  • I've tried sending messages on guild channels
  • I've tried @ing the bot

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"
    }
}

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 e through in the console.

  • When I start the script, the bot switches to "online." Curiously, the bot does not switch back to "offline" until I regenerate the token. Not sure if that is related.
  • I've verified process.env.BOT_TOKEN is the correct value.
  • I've confirmed client.login runs successfully.
  • I've tried DMing the bot directly
  • I've tried sending messages on guild channels
  • I've tried @ing the bot

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"
    }
}
Share Improve this question edited Aug 13, 2021 at 2:50 Thane asked Aug 13, 2021 at 2:14 ThaneThane 3824 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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);
发布评论

评论列表(0)

  1. 暂无评论