【问题标题】:Reaction Role Panel Stops working after a bot restart Discord.JS机器人重启 Discord.JS 后反应角色面板停止工作
【发布时间】:2021-09-30 02:16:24
【问题描述】:

我有一个有效的反应角色菜单,可以很好地分配和取消角色,直到机器人重新启动然后它停止给予/取消角色,我正在努力寻找与此反应菜单工作方式相匹配的解决方案。

client.on('messageReactionAdd', async (reaction, user) =>  {
             if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot)
            if(!reaction.message.guild) return;
    
    
            if (reaction.message.channel.id === channel) {
              if (reaction.emoji.name === reactone){
                
                  await reaction.message.guild.members.cache.get(user.id).roles.add(roleone);
              }
              if (reaction.emoji.name === reacttwo){
                
                  await reaction.message.guild.members.cache.get(user.id).roles.add(roletwo);
              }
              if (reaction.emoji.name === reactthree){
                
                  await reaction.message.guild.members.cache.get(user.id).roles.add(rolethree);
              }
              if (reaction.emoji.name === reactfour){
                
                  await reaction.message.guild.members.cache.get(user.id).roles.add(rolefour);
              }
              //add more here
            } else{
              return
            }
            });

【问题讨论】:

  • 有错误吗?尝试做一些调试;将console.log 语句放在这段代码的各个部分(例如,在事件处理程序的顶部,在每个if 条件检查之后,在每个角色分配之前和之后),看看你的代码有多远。这样做会告诉您代码的哪一部分是问题所在,或者您的代码是否正在执行。
  • 我觉得可能跟缓存有关
  • 我看不到任何错误出现,并且在机器人重新启动之前它工作正常,我也认为它与缓存有关但是我正在努力查看我需要修复/编辑的位置以确保它缓存正确

标签: discord.js


【解决方案1】:

客户端没有从重启前发送的消息中接收到messageReactionAdd 事件。您需要获取这些消息才能从这些消息中接收messageReactionAdd。

有一个名为ready 的事件将在启动完成时执行。这是使用以下代码fetch 这些消息的理想场所:

const channel = client.channels.cache.get('ID'); # Channel ID where the msg was sent
channel.messages.fetch({ limit: 10 });

或者如果你知道message ID:

const channel = client.channels.cache.get('ID'); # Channel ID where the msg was sent
channel.messages.fetch({ around: MESSAGE_ID, limit: 1 });

阅读更多关于official discord.js documentation

【讨论】:

  • 感谢您的回答,但我似乎无法让它为我工作已将它放在 client.on("ready", () => {这里} 这是正确还是错误的地方?
猜你喜欢
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 2022-01-15
  • 1970-01-01
  • 2021-09-04
  • 2023-03-29
  • 2021-02-07
相关资源
最近更新 更多