discord.js机器人在Discord中无回复问题求助
解决方案
核心问题
你使用的discord.js版本(从GatewayIntentBits判断为v14及以上)已废弃message事件,改用messageCreate事件监听消息,这是机器人不响应消息的根本原因。
修改步骤
- 将代码中的
client.on("message", gotMessage)替换为client.on("messageCreate", gotMessage) - 重新运行
node bot.js重启机器人
修改后的完整代码
console.log("Beep Beep"); const { Client, GatewayIntentBits } = require("discord.js"); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, ], }); client.login("TOKEN"); client.on("ready", readyDiscord); function readyDiscord() { console.log("ok"); } client.on("messageCreate", gotMessage); // 关键修改处 function gotMessage(msg) { console.log(msg.content); if (msg.content === "Hello") { msg.reply("Hi"); } }
额外验证项
- 确认机器人在服务器内拥有发送消息和读取消息历史的权限
- 检查机器人令牌
TOKEN是否有效,无过期或权限变更情况
内容的提问来源于stack exchange,提问作者lelocchio
相关产品推荐
相关产品推荐

