【问题标题】:'DiscordAPIError: Unknown Message' when deleting message upon a reaction'DiscordAPIError: Unknown Message' 删除消息时的反应
【发布时间】:2019-11-25 20:07:27
【问题描述】:

我正在尝试使用我的 Discord.js 机器人在 JavaScript 中制作菜单。到目前为止,一切正常,除非我应用删除消息的反应。该消息被删除,但此错误在控制台中被垃圾邮件:

DiscordAPIError: Unknown Message

我检查了其他区域并小心地只删除了一次消息,但即便如此,我还是觉得机器人试图多次删除它。我也尝试过 return 来尝试停止代码,并为 delete 方法设置一个计时器。这些尝试都没有解决错误。

这是我正在使用的代码:

message.channel.send(exampleEmbed2).then(sentEmbed => {

    sentEmbed.react('????');  
    sentEmbed.react('✅');
    sentEmbed.react('❌');

    client.on('messageReactionAdd', (reaction, user) => {

        if (!user.bot && reaction.emoji.name === '????') {
            sentEmbed.delete();
        }

    });
});

【问题讨论】:

  • 欢迎来到 Stack Overflow!你怎么能确定 messageReactionAdd 是用 '????' 触发的多次?
  • 我不知道,通常代码只有在有新反应时才会运行
  • 我认为@JeroenHeier 的意思是,使用这段代码,每当有人用 ???? 对任何消息做出反应时,它都会运行您的代码。您可能想要添加更多控制,尤其是对附加反应的消息。

标签: javascript bots discord discord.js


【解决方案1】:

正如Gruntzy 解释的那样......

...使用此代码,每次有人使用 ? 对任何消息做出反应时,都会运行您的代码。

与将另一个侦听器附加到嵌套的messageReactionAdd 事件相反,请使用Reaction Collectors。这些是为此目的而设计的,并且只会在对您附加它们的消息做出反应时触发。

考虑这个使用基于承诺的收集器版本的示例(请参阅Message.awaitReactions())...

message.channel.send(exampleEmbed2)
  .then(async sentEmbed => { // Somewhat awkward to switch to async/await here, but much cleaner.
    await sentEmbed.react('?');
    await sentEmbed.react('✅');
    await sentEmbed.react('❌');

    const filter = (reaction, user) => ['?', '✅', '❌'].includes(reaction.emoji.name) && user.id === message.author.id
    const [reaction] = await sentEmbed.awaitReactions(filter, { maxMatches: 1 });
//        ^^^^^^^^^^
//  This is a destructuring
//  assignment. It takes the
//  first (and only) element
//  from the Collection/Map.

    if (reaction.emoji.name === '?') await sentEmbed.delete();
//  Do stuff with the other reactions...
  })
  .catch(console.error);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2021-06-04
    • 2018-11-05
    • 2021-01-04
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多