Discord机器人无法响应消息,开启MessageContent意图仍无效
问题排查与修复
你的代码存在两个关键问题,导致机器人无法响应消息:
- 事件名称错误:Discord.js v14及以上版本已移除原
message事件,替换为messageCreate事件,使用旧事件名称无法触发消息响应逻辑。 - 客户端实例化错误:你从discord.js导入了
Client,但实例化时误用了Discord.Client,会导致运行报错(未全局声明Discord变量的情况下)。
修正后的代码如下:
const { Client, GatewayIntentBits } = require('discord.js'); // const logger = require('winston'); // 未用到的模块可注释或移除 const bot = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, ], }); const token = "你的真实机器人令牌"; bot.on('ready', () => { console.log('bot已上线'); }); // 使用正确的messageCreate事件 bot.on('messageCreate', msg => { // 避免机器人响应自身消息,防止循环触发 if (msg.author.bot) return; if (msg.content === "hello") { msg.channel.send("helo man"); } }); bot.login(token);
额外注意:
- 必须替换示例中的"69420"为真实有效的机器人令牌。
- 添加
if (msg.author.bot) return;可以避免机器人响应自己发送的消息,减少不必要的触发。
内容的提问来源于stack exchange,提问作者Banana
相关产品推荐
相关产品推荐

