Discord 机器人未接收交互

Tha*_*ane 6 javascript node.js discord discord.js

我是一个相对经验丰富的开发人员(最近毕业),在几年前制作了一些之后,试图重新回到不和谐的机器人中。

我一直在遵循discordjs.guide上的指南,但即使使用基本的“hello world”风格的程序,我也已经陷入困境。

程序运行时没有错误,但控制台中没有交互。

  • 当我启动脚本时,机器人切换到“在线”。奇怪的是,在我重新生成令牌之前,机器人不会切换回“离线”状态。不确定这是否相关。
  • 我已经验证了process.env.BOT_TOKEN这是正确的值。
  • 我已经确认client.login运行成功。
  • 我尝试过直接通过 DMing 机器人
  • 我尝试过在公会频道上发送消息
  • 我尝试过 @ing 机器人

有什么非常明显的我不理解/没有看到的东西吗?否则,我还应该尝试什么作为故障排除步骤?

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);
Run Code Online (Sandbox Code Playgroud)

包.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"
    }
}
Run Code Online (Sandbox Code Playgroud)

Tha*_*ane 10

事实证明,discord.js 并未将消息归类为“交互”。此外,您必须指定侦听消息的意图,否则,事件将不会传递给您的机器人。这是修改后的代码,其中有两个关键更改:

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);
Run Code Online (Sandbox Code Playgroud)