如何删除用户反应的消息

时间:2020-06-02 02:58:06

标签: javascript bots discord discord.js

尝试删除对bot作出反应后bot答复的原始用户命令。由于某种原因,它每隔一段时间才会工作,当它工作时,它将中继DiscordAPIError: Unknown Message错误

const isValidCommand = (message, cmdName) => message.content.toLowerCase().startsWith(PREFIX + cmdName)
client.on('message', function(message) {
  if (message.author.bot) return;
  if (isValidCommand(message, "banker"))
    message.reply("Bankers are on the way to get your cash. Please be patient as Bankers are busy individuals as well. If there is a major delay you are welcome to use !HeadBanker.      Bankers Please React With Money Bag When Sent/Sending!").then(r_msg =>
      r_msg.react('?'))
  if (isValidCommand(message, "banker"))
    message.channel.send("<@&717082379470110885> Banker Cash Needed")
    .then(msg => {
      msg.delete({
        timeout: 0100
      })
    })
  if (isValidCommand(message, "headbanker"))
    message.reply("I see you have pinged Head Banker. If its between 14:00TCT and 5:00TCT you should get a response within a minute or two max. If no response in five minutes you have permission to ping again.").then(r_msg =>
      r_msg.react('?'))
  if (isValidCommand(message, "headbanker"))
    message.channel.send("<@&716843712092569640> Cash Needed")
    .then(msg => {
      msg.delete({
          timeout: 0100
        })
        .catch(console.log);
    })

  client.on('messageReactionAdd', (reaction, user) => {
    let limit = 2;
    if (reaction.emoji.name == '?' && reaction.count >= limit) reaction.message.delete()
      .catch(console.log);
    if (reaction.emoji.name == '?' && reaction.count >= limit) message.delete()
      .catch(console.log);
  });
})

1 个答案:

答案 0 :(得分:0)

我认为这是由于您在messageReactionAdd监听器的内部声明了message监听器而导致的:这意味着每次您会收到一条新消息,客户端将添加一个新的处理程序,因此它将尝试多次删除同一条消息,从而导致API中出现“未知消息”错误。
您应该尝试使用Message.awaitReactions()Message.createReactionCollector()创建一个反应收集器,而不是在客户端上为反应添加侦听器。这是一个示例:

const isValidCommand = (message, cmdName) => message.content.toLowerCase().startsWith(PREFIX + cmdName)

client.on('message', function(message) {
  if (message.author.bot) return;
  if (isValidCommand(message, "banker")) { // Use single if statements where possible
    message.reply("Bankers are on the way to get your cash. Please be patient as Bankers are busy individuals as well. If there is a major delay you are welcome to use !HeadBanker.      Bankers Please React With Money Bag When Sent/Sending!").then(r_msg =>
      r_msg.react('?'))
    message.channel.send("<@&717082379470110885> Banker Cash Needed")
      .then(msg => {
        msg.delete({ timeout: 0100 })
      })
  }
  if (isValidCommand(message, "headbanker")) {
    message.reply("I see you have pinged Head Banker. If its between 14:00TCT and 5:00TCT you should get a response within a minute or two max. If no response in five minutes you have permission to ping again.").then(r_msg =>
      r_msg.react('?'))
    message.channel.send("<@&716843712092569640> Cash Needed")
      .then(msg => {
        msg.delete({ timeout: 0100 })
          .catch(console.log);
      })
  }

  message.awaitReactions(r => r.emoji.name == '?', { max: 2 }).then(collected => {
    if (collected.size >= 2)
      message.delete()
  })
})

此外,最好不要重复if条语句,应尽可能合并它们。