繁体   English   中英

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

[英]discord.js How do you make a reaction collector

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

.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是否因为您希望接受其他用户的反应。 但是,您可以检查 if .user.bot以确保您不会计算机器人的反应。 如果您不想限制机器人收集反应的时间,也可以删除time选项。

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

查看下面的工作代码:

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);
    }
  }
});

结果:

在此处输入图像描述

你想用这样的if语句检查collected.size

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

希望我的问题是正确的。

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

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

有关更多信息,您可以查看discord.js 指南或 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))
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM