微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

discord.js 如何制作反应收集器

如何解决discord.js 如何制作反应收集器

我正在努力做到这一点,当你得到一定数量的反应时,我的机器人会发送一条消息,如果有人可以帮忙,这里是我的代码

.then(function(sentMessage) {
            sentMessage.react('?').catch(() => console.error('emoji Failed to react.')).message.reactions.cache.get('?').count;
            const filter = (reaction,user) => {
    return reaction.emoji.name === '?' && user.id === message.author.id;
};

message.awaitReactions(filter,{ max: 2,time: 00,errors: ['time'] })
    .then(collected => console.log(collected.size))
    .catch(collected => {
        console.log(`After a minute,only ${collected.size} out of 4 reacted.`);
    });
        });
})

解决方法

您还可以使用 awaitReactions 代替 createReactionCollector,它可能更易于使用,并且其 collector.on() 侦听器比 awaitReactionsthen() 更具可读性和 catch() 方法。

您无需使用 message.reactions.cache.get('?').count 来检查反应次数,因为当您达到最大值时会触发 end 事件,您可以在其中发送消息。

此外,在您的 filter 中,您不需要检查是否 user.id === message.author.id,因为您会希望接受其他用户的反应。但是,您可以检查是否 !user.bot 以确保您不会计算机器人的反应。如果您不想限制机器人收集反应的时间,您也可以删除 time 选项。

另一个错误是您在 .awaitReactions() 本身而不是 message 上调用了 sentMessage

查看下面的工作代码:

client.on('message',async (message) => {
  if (message.author.bot || !message.content.startsWith(prefix)) return;

  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();
  const MAX_REACTIONS = 2;

  if (command === 'react') {
    try {
      // send a message and wait for it to be sent
      const sentMessage = await message.channel.send('React to this!');

      // react to the sent message
      await sentMessage.react('?');

      // set up a filter to only collect reactions with the ? emoji
      // and don't count the bot's reaction
      const filter = (reaction,user) => reaction.emoji.name === '?' && !user.bot;

      // set up the collecrtor with the MAX_REACTIONS
      const collector = sentMessage.createReactionCollector(filter,{
        max: MAX_REACTIONS,});

      collector.on('collect',(reaction) => {
        // in case you want to do something when someone reacts with ?
        console.log(`Collected a new ${reaction.emoji.name} reaction`);
      });

      // fires when the time limit or the max is reached
      collector.on('end',(collected,reason) => {
        // reactions are no longer collected
        // if the ? emoji is clicked the MAX_REACTIONS times
        if (reason === 'limit')
          return message.channel.send(`We've just reached the maximum of ${MAX_REACTIONS} reactions.`);
      });
    } catch (error) {
      // "handle" errors
      console.log(error);
    }
  }
});

结果:

enter image description here

,

您想使用 collected.size 语句检查 if,如下所示:

let amount = 4; // any integer
if (collected.size /* returns an integer */ === amount) {
console.log(`Got ${amount} reactions`)
}

希望我的问题是正确的。

,

如果我理解正确,那么您只需更改 awaitMessage 方法的参数即可。您可以删除 time: 00,errors: ['time'] 参数,因为它们是可选的,而保留 max: 2。这样,该函数只会在有 2 次反应(在这种情况下)时完成。

我建议从过滤器中删除 user.id === message.author.id;,因为您似乎希望多个用户对消息做出反应。

有关详细信息,您可以查看 discord.js guidedocumentation for awaitReactions

代码:

message.channel.send("Message reacting to.").then(function (sentMessage) {
    sentMessage.react('?').catch(() => console.error('emoji failed to react.'));
    const filter = (reaction,user) => {
        return reaction.emoji.name === '?';
    };
    message.awaitReactions(filter,{ max: 2 })
        .then(collected => console.log(collected.size))
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?