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

Discord机器人未实现ping回复pong功能的问题排查求助

Discord机器人ping-pong功能故障排查与修复

你的代码存在两个关键问题导致功能无法正常运行,以下是修复方案:

问题1:事件名称过时

Discord.js v14及更高版本中,接收消息的事件已从message更改为messageCreate,原代码监听的message事件不会被触发。

问题2:缺少必要的网关意图

要读取服务器内的消息内容,需要添加两个关键意图:

  • GatewayIntentBits.GuildMessages:允许机器人接收服务器内的消息
  • GatewayIntentBits.MessageContent:允许机器人读取消息的文本内容
    同时需要在Discord开发者门户的机器人设置页面,开启"Message Content Intent"开关(位于Privileged Gateway Intents板块下)。

修复后的完整代码

require('dotenv').config();

const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent
    ]
})

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', msg => {
    if (msg.content === 'ping') {
      msg.reply('pong');
    }
});

client.login(process.env.DISCORD_TOKEN);

额外注意事项

  • 确保Discord.js版本为v14或更高,版本过低可执行升级命令:npm install discord.js@latest
  • 确认.env文件中的DISCORD_TOKEN为正确的机器人令牌

内容的提问来源于stack exchange,提问作者holomii

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 20:26:15