【问题标题】:Discord.JS ReactionsDiscord.JS 反应
【发布时间】:2020-11-03 16:49:06
【问题描述】:

我正在开发票务机器人,我希望它可以让您对消息做出反应以创建票证。我可以发送消息,但是当我使用 bot.on('messageReactionAdd') 时,如果我的机器人重新启动,它不会检测到已添加反应,除非您发出新消息并对自机器人上线以来创建的消息作出反应。这对我来说是个问题,我知道它是可以解决的。我试过谷歌搜索,但无法解决问题。这里有人可以帮帮我吗?

【问题讨论】:

  • 当机器人启动时,指示它获取您需要它跟踪的消息。假设您在一个永远不会有任何其他消息的专用频道中拥有此“特殊消息”,因此您只需在该频道中获取消息,Discord.js 将跟踪其上的新反应。
  • 如何在 discord.js 中获取消息?

标签: javascript node.js discord discord.js


【解决方案1】:

从 Discord.js v12 开始,您可以在机器人上启用 partials,以允许它为未获取的消息发送事件,但您必须在处理程序开始时自己获取消息:

const Discord = require('discord.js');
// Make sure you instantiate the client with the correct settings
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
client.on('messageReactionAdd', async (reaction, user) => {
    // When we receive a reaction we check if the reaction is partial or not
    if (reaction.partial) {
        // If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
        try {
            await reaction.fetch();
        } catch (error) {
            console.error('Something went wrong when fetching the message: ', error);
            // Return as `reaction.message.author` may be undefined/null
            return;
        }
    }
    // Now the message has been cached and is fully available
    console.log(`${reaction.message.author}'s message "${reaction.message.content}" gained a reaction!`);
    // The reaction is now also fully available and the properties will be reflected accurately:
    console.log(`${reaction.count} user(s) have given the same reaction to this message!`);
});

【讨论】:

    【解决方案2】:

    您可以随心所欲地使用它,但这里有一个示例:

    message.channel.send("React test!").then(messageReaction => {
        messageReaction.react("✅");
        messageReaction.react("⛔");
      });
    

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 2021-02-07
      • 2021-01-28
      • 2021-12-30
      • 2021-06-21
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多