Discord.js v14 机器人提及自动回复功能失效问题求助
解决Discord机器人被提及无响应的问题
问题根源及修复步骤
1. 缺少必要的意图权限
Discord.js v14及以上版本中,监听messageCreate事件需要同时开启以下三个意图,你目前只添加了其中一个:
GatewayIntentBits.Guilds(已添加)GatewayIntentBits.GuildMessages:用于监听服务器内的消息事件GatewayIntentBits.MessageContent:用于读取消息内容,属于特权意图,必须在Discord开发者后台手动开启
操作说明:
- 登录Discord开发者后台,进入你的机器人应用页面
- 切换到「Bot」标签,找到「Privileged Gateway Intents」区域,开启「Message Content Intent」
- 在代码的客户端初始化部分,补充完整意图数组:
const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] });
2. 代码过滤逻辑有误
你原代码中message.type == "REPLY"的判断会直接忽略所有回复类型的消息,导致用户回复消息时提及机器人的情况无法触发响应。同时,提及判断可以更明确指向用户提及:
修改后的messageCreate事件代码:
client.on("messageCreate", (message) => { // 忽略机器人发送的消息 if (message.author.bot) return; // 仅忽略@here和@everyone提及,保留回复消息的处理 if (message.content.includes("@here") || message.content.includes("@everyone")) return; // 判断是否提及了当前机器人用户 if (message.mentions.users.has(client.user.id)) { message.channel.send("Slash Command: /help"); } });
内容的提问来源于stack exchange,提问作者ImAnthox
相关产品推荐
相关产品推荐

