discord.js机器人无消息响应问题求助,调整Intents未解决
Discord.js机器人不响应消息的解决方案
核心问题与修复步骤:
- 事件名称错误:Discord.js v14+版本中,消息事件已从
message更改为messageCreate,旧事件名无法触发消息监听。 - 登录逻辑位置错误:
client.login(config.token)被嵌套在消息事件回调内,导致机器人仅在收到消息后才尝试登录,正确做法是将登录逻辑放在所有事件监听之外,确保机器人完成登录后再处理事件。 - API延迟属性变更:v14移除了
client.ping,需改用client.ws.ping获取API延迟数据。 - Intents权限验证:即使设置了全Intents数值
3276799,仍需在Discord开发者平台的机器人页面,开启Privileged Gateway Intents中的Message Content Intent(消息响应必备),以及Presence Intent、Server Members Intent(若你的机器人需要相关功能)。
修正后的完整代码:
const Discord = require("discord.js"); const client = new Discord.Client({ intents: 3276799 }); const config = require("./config.json"); client.on("messageCreate", async message => { if (message.author.bot) return; if (message.channel.type === "dm") return; if (!message.content.startsWith(config.prefix)) return; const args = message.content.slice(config.prefix.length).trim().split(/ +/g); const comando = args.shift().toLowerCase(); if (comando === "ping") { const m = await message.channel.send("Ping?"); m.edit(`Pong! 消息延迟是 ${m.createdTimestamp - message.createdTimestamp}ms。API延迟是 ${Math.round(client.ws.ping)}ms`); } }); client.login(config.token);
内容的提问来源于stack exchange,提问作者duduzinpro
相关产品推荐
相关产品推荐

