You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人无法响应消息,开启MessageContent意图仍无效

问题排查与修复

你的代码存在两个关键问题,导致机器人无法响应消息:

  1. 事件名称错误:Discord.js v14及以上版本已移除原message事件,替换为messageCreate事件,使用旧事件名称无法触发消息响应逻辑。
  2. 客户端实例化错误:你从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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 16:45:37