discord.js messageReactionAdd事件无响应问题求助
问题解决:Discord Bot messageReactionAdd事件不触发
核心原因:缺失必要网关意图
你的机器人客户端未启用GuildMessageReactions意图,这是监听消息添加反应事件的必备配置,当前intents数组中缺少此项。
修复方案
1. 补充网关意图
修改客户端初始化代码,添加GatewayIntentBits.GuildMessageReactions:
const { Client, GatewayIntentBits } = require('discord.js') const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessageReactions // 新增该意图 ] })
2. 适配非缓存消息(可选)
如果需要监听机器人启动前发送的旧消息(此类消息不在缓存中),需启用Message等partials,并在事件中处理partial对象:
const client = new Client({ intents: [ // 保留原有意图,加上GuildMessageReactions GatewayIntentBits.GuildMessageReactions ], partials: ['MESSAGE', 'REACTION', 'USER'] // 启用partials }) // 修改messageReactionAdd事件处理逻辑 client.on('messageReactionAdd', async (reaction, user) => { // 若为partial对象,先获取完整数据 if (reaction.partial) { try { await reaction.fetch(); } catch (error) { console.error('获取反应数据失败:', error); return; } } console.log("reacted"); })
3. 验证机器人权限
确保机器人在目标服务器拥有读取消息历史权限,否则无法正常监听反应事件。
内容的提问来源于stack exchange,提问作者user21484220
相关产品推荐
相关产品推荐

