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

Discord.js:如何在messageReactionAdd事件中获取被反应的消息

在Discord.js的messageReactionAdd事件中获取目标消息并检查表情

在messageReactionAdd事件的回调参数里,reaction对象本身就包含了触发反应的消息实例——直接通过reaction.message就能访问到该消息。不过要注意,如果目标消息不在客户端缓存中,你需要先调用fetch()方法来获取完整的消息数据。

示例代码修改

针对你的需求,要检查消息上是否已存在🚫表情,可以这样调整代码:

client.on('messageReactionAdd', async (reaction, user) => {
    // 确保获取完整的消息对象(处理缓存未命中的情况)
    const targetMessage = await reaction.message.fetch();

    // 检查消息上是否已存在🚫表情
    const hasStopReaction = targetMessage.reactions.cache.has('🚫');

    if(reaction.emoji.id === "1014887969808711800") {
        if (user.username !== 'IteroBetBot') {
            console.log('Fnatic');
            voterArray1.push(user.username);
        }
    } else if (reaction.emoji.id === "1065285604578959430") {
        if (user.username !== 'IteroBetBot') {
            console.log('Koi');
            voterArray2.push(user.username);
        }
    } else if (reaction.emoji.name === '🚫') {
        // 用hasStopReaction判断,避免重复触发逻辑
        if (!hasStopReaction) {
            console.log('Found it');
            let finalString1 = '';
            let finalString2 = '';
            voterArray1.forEach(element => {
                finalString1 += element + " "
            });
            voterArray2.forEach(element => {
                finalString2 += element + " "
            });
            finalString1 += ' voted for Fnatic';
            finalString2 += ' voted for Koi';
            client.channels.cache.get('1077613044563202058').send(finalString1);
            client.channels.cache.get('1077613044563202058').send(finalString2);
        }
    }
});

关键说明

  • reaction.message:直接指向触发反应的消息对象,若消息在缓存中则直接可用;若不在缓存,调用await reaction.message.fetch()可从Discord API拉取完整数据。
  • targetMessage.reactions.cache.has('🚫'):利用反应缓存快速判断消息上是否存在🚫表情(默认表情无ID,以名称作为缓存键)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:27:58