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

Discord Bot已成功连接,但输入ping无pong响应问题求助

问题分析与解决

你的Discord Bot无法响应ping请求,核心问题出在三个地方:

  • 事件监听名称错误:discord.js v14及以上版本已移除message事件,替换为messageCreate事件,原事件不会触发任何逻辑。
  • 缺少消息内容读取权限:必须启用MESSAGE_CONTENT intent才能获取消息内容,否则无法检测到ping指令。
  • Intents 使用方式不兼容:v14版本推荐使用GatewayIntentBits而非旧版Intents.FLAGS,混用会导致权限配置异常。

修正后的代码

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({ 
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent // 必须添加该intent才能读取消息内容
    ] 
})

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

// 替换为messageCreate事件
client.on("messageCreate", msg => {
  if (msg.content === "ping") {
    msg.reply("pong");
  }
})

client.login("你的Bot Token")

额外注意事项

  • 登录Discord开发者平台的Bot管理页面,在「Privileged Gateway Intents」栏目下开启MESSAGE CONTENT INTENT权限,否则即使代码配置了该intent,Bot也无法读取消息内容。
  • 避免同时导入新旧版本的Discord.js模块,保持代码内API使用逻辑一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 20:35:30