简体   繁体   English

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

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

I am trying to make it so when you get to a certain amount of reactions my bot will send a message and if somebody can help here is my code我正在努力做到这一点,所以当你得到一定数量的反应时,我的机器人会发送一条消息,如果有人可以帮助这里是我的代码

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

Instead of awaitReactions , you could also use createReactionCollector which is probably easier to use and its collector.on() listeners are more readable than awaitReactions 's then() and catch() methods.除了awaitReactions ,您还可以使用createReactionCollector ,它可能更易于使用,并且它的collector.on()侦听器比awaitReactionsthen()catch()方法更具可读性。

You won't need to use message.reactions.cache.get('').count to check the number of reactions, as the end event fires when you reached the maximum and you can send a message inside that.您不需要使用message.reactions.cache.get('').count来检查反应数量,因为当您达到最大值时会触发end事件,您可以在其中发送消息。

Also, in your filter , you don't need to check if user.id === message.author.id as you will want to accept reactions from other users.此外,在您的filter中,您不需要检查user.id === message.author.id是否因为您希望接受其他用户的反应。 However, you can check if .user.bot to make sure that you won't count the bot's reaction.但是,您可以检查 if .user.bot以确保您不会计算机器人的反应。 You can also remove the time option if you don't want to limit the time your bot collects reactions.如果您不想限制机器人收集反应的时间,也可以删除time选项。

Another error was that you called .awaitReactions() on the message itself not the sentMessage .另一个错误是您在message本身而不是sentMessage上调用.awaitReactions()

Check out the working code below:查看下面的工作代码:

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

And the result:结果:

在此处输入图像描述

You want to check the collected.size with an if statement like so:你想用这样的if语句检查collected.size

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

Hope I got the issue right.希望我的问题是正确的。

If I understand it correctly, then you can just change the parameters for the awaitMessage method.如果我理解正确,那么您可以更改awaitMessage方法的参数。 You can remove the time: 00, errors: ['time'] arguments since they're optional and keep the max: 2 .您可以删除time: 00, errors: ['time'] arguments 因为它们是可选的并保留max: 2 That way, the function will only finish once there are 2 reactions (in this case).这样,function 只会在有2反应时完成(在这种情况下)。

I would recommend removing the user.id === message.author.id;我建议删除user.id === message.author.id; from the filter since it seems like you want multiple users to react to the message.来自过滤器,因为您似乎希望多个用户对消息做出反应。

For more information, you can check the discord.js guide or the documentation for awaitReactions .有关更多信息,您可以查看discord.js 指南或 awaitReactions 的文档

Code:代码:

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