如何修复 "Problem with Reactions (Restart bot)"

How to fix "Problem with Reactions (Restart bot)"

我的反应回复系统有问题。

我希望当用户添加反应时回复消息,但当机器人重新启动时它不再被机器人检测到。

你知道如何解决这个问题吗?

这是我当前的代码:

bot.on("messageReactionAdd", function(messageReaction, user){
    if(messageReaction.message.content === "Message"){
        if(user.bot){return}
        messageReaction.message.reply("It works.")
    }
})


bot.on("message", function(message){
        if(message.content.startsWith(prefix + "test")){
          message.delete()
          message.member.createDM().then(m =>  m.send("Message").then(m => m.react("✅")))
        }
}

在最新版本的 discord.js 上,您可以使用 部分事件 来完成此操作。 根据文档 (https://discord.js.org/#/docs/main/master/topics/partials):

Partials allow you to receive events that contain uncached instances, providing structures that contain very minimal data. For example, if you were to receive a messageDelete event with an uncached message, normally Discord.js would discard the event. With partials, you're able to receive the event, with a Message object that contains just an ID.

你需要做的是

const Discord = require('discord.js');
// add "partials" in the bot options to enable partial events
const client = new Discord.Client({"partials": ['CHANNEL', 'MESSAGE']});

[...]
client.on("messageReactionAdd", async function(messageReaction, user){
  // fetch message data if we got a partial event
  if (messageReaction.message.partial) await messageReaction.message.fetch();

  if(messageReaction.message.content === "Message"){
      if(user.bot){return}
      messageReaction.message.reply("It works.")
  }
})

执行此操作时,您必须小心 client.on("message", ...) 调用,以避免访问不可用的数据或方法。 有一个布尔值 message.partial,您可以在不需要时使用它来丢弃部分消息。