修复 Discord.js 中的“CLIENT_MISSING_INTENTS”错误
在您提供的代码中,您遇到“CLIENT_MISSING_INTENTS”错误,因为您没有指定您的机器人应从 Discord 接收的意图API。
意图是允许您控制机器人可以响应的事件的标志。如果没有指定必要的意图,您的机器人将无法接收来自 Discord 用户的消息,从而导致此错误。
解决方案:
要解决此问题,您当你实例化你的 Discord 客户端时,需要添加适当的意图。以下是包含意图的更新代码:
const Discord = require('discord.js'); // Specify the intents that your bot should receive const client = new Discord.Client({ intents: [ Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages ] }); client.on('message', (msg) => { // Send back a reply when the specific command has been written by a user. if (msg.content === '!hello') { msg.reply('Hello, World!'); } }); client.login('my_token');
对于 Discord.js v13,语法略有不同:
const Discord = require('discord.js'); // Specify the intents that your bot should receive const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] }); client.on('message', (msg) => { // Send back a reply when the specific command has been written by a user. if (msg.content === '!hello') { msg.reply('Hello, World!'); } }); client.login('my_token');
通过添加这些意图,您的机器人将能够监听对于“消息”事件并做出相应的响应。
其他信息:
以上是如何解决 Discord.js'CLIENT_MISSING_INTENTS”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!