【问题标题】:Discord Bot JS V12 client on message ReactionDiscord Bot JS V12 客户端上的消息反应
【发布时间】:2022-01-10 15:27:32
【问题描述】:

我已将此代码放入我的index.js,但它仅适用于机器人启动后的新消息。

并且在机器人启动之前发送了一条旧消息,它不起作用。他只是忽略了该消息。

client.on("messageReactionAdd", async (reaction, user) => {
        if (user.bot) return;

        if (reaction.message.channel.id == channelverify) {
            if (reaction.emoji.name == checkemoji) {
                await reaction.message.guild.members.cache.get(user.id).roles.add(inwonerrole);
                await reaction.message.guild.members.cache.get(user.id).roles.add(whitelistrole);
            }
        }else {
            return;
        }
    });
    
    client.on("messageReactionRemove", async (reaction, user) => {
        if (user.bot) return;
        if (reaction.message.channel.id == channelverify) {
            if (reaction.emoji.name == checkemoji) {
                await reaction.message.guild.members.cache.get(user.id).roles.remove(inwonerrole);
                await reaction.message.guild.members.cache.get(user.id).roles.remove(whitelistrole);
            }
        }else {
            return;
        }
    });

【问题讨论】:

  • 检查您是否在客户端初始化中启用了GUILD_MESSAGE_REACTIONS 意图。

标签: node.js discord discord.js


【解决方案1】:

messageReactionAdd 事件仅在缓存消息时触发 -- 所以旧消息的反应不会被触发

但是你可以获取它们 - 所以它们被缓存了

/*
TextChannel = The channel which messages you want to get
*/
TextChannel.messages.fetch()

这将使 discord.js 缓存频道的消息;您不需要将其保存在变量或常量中(必须在client.on 之前完成获取)

缓存所有频道的消息后,事件将随之触发;因为它们被缓存了。

【讨论】:

  • 您的第一行messageReactionAdd event is only fired with cached messages -- So old messages' reactions won't get fired 是虚假信息。即使机器人正在重新启动,它也应该可以工作。问题可能与意图有关,但 OP 没有将他的其余代码放在他/她的帖子中,所以我们无法确定。
  • 一开始我以为是intents问题,但没有intents,即使有新消息也不会触发
  • (编辑)这是真的,但GUILD_MESSAGESGUILD_MESSAGE_REACTIONS 都是不同的意图。如果没有GUILD_MESSAGE_REACTIONS,即使您没有启用反应意图(反之亦然),消息事件仍然可以正常工作,我已经检查过了。
  • @node_modules 未缓存的消息没有收到反应事件
  • 查看活动的description
猜你喜欢
  • 1970-01-01
  • 2020-07-12
  • 2020-07-31
  • 2022-01-08
  • 2022-01-13
  • 1970-01-01
  • 2022-01-08
  • 2021-01-03
  • 1970-01-01
相关资源
最近更新 更多