[英]Discord Bot Doesn't reply back (Node.js)
我一直在努力为我的 discord 服务器创建一个机器人,所以我使用了来自 discordjs.guide 的 discord 机器人的初始代码,并添加了一个简单的命令来响应“ping”,我想证明我之前做错了什么......但是我没有得到机器人的回应。 我已经通过邀请链接和角色授予它权限,有人可以告诉我代码有什么问题吗? 我安装了 discord.js 和 dotenv
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', function(msg){
if(msg.content === 'ping'){
msg.reply('pong');
}
});
// Login to Discord with your client's token
client.login(token);
这是因为client.on("message")
已被弃用
你应该在你的 discord 客户端上定义 GUILD_MESSAGES
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
在您的服务器上接收消息的事件将是
client.on('messageCreate', function(msg){
if(msg.content === 'ping'){
msg.reply('pong');
}
});
“messageCreate”事件的文档
您的代码缺少GUILD_MESSAGES
意图。 只需将其添加到您的意图中,您的机器人就会响应消息。 请记住,discord 将在 4 月 30 日之后删除消息意图,因此您需要 go 进入https://discord.com/developers并进入机器人部分,打开消息意图。
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS. GUILD_MESSAGES] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', function(msg){
if(msg.content === 'ping'){
msg.reply('pong');
}
});
// Login to Discord with your client's token
client.login(token);
请记住,discord 将在 4 月 30 日之后删除消息意图,因此您需要 go 进入https://discord.com/developers并进入机器人部分,打开消息意图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.