简体   繁体   English

如何在一定量的反应后使反应消失?

[英]How to make a reaction disappear after a certain amount of reactions?

I want to make a poll command, where I use the command, and when the reaction reaches a certain amount of reactions it (the reaction) gets deleted.我想制作一个轮询命令,在我使用该命令的地方,当反应达到一定数量的反应时,它(反应)被删除。 That would be useful to see who wins if you understand me.如果你理解我的话,这将有助于看看谁赢了。

That's what I'm using right now for only adding a reaction.这就是我现在用来添加反应的东西。

bot.on('message', msg => {
    if(msg.content === "memes"){
        msg.react("🤏🏾")
    }
})

You can use reaction collectors to collect user reactions on a message.您可以使用反应收集器来收集用户对消息的反应。 The collector has options like maxUsers that you can use to set the maximum number of users to react.收集器具有诸如maxUsers之类的选项,您可以使用这些选项来设置要做出反应的最大用户数。 Once it's reached, the collector.on('end') fires, so you can use it to get the number of users reacted, etc.一旦到达, collector.on('end')就会触发,因此您可以使用它来获取做出反应的用户数量等。

You can remove the reactions using message.reactions.removeAll();您可以使用message.reactions.removeAll();

Check the snippet below:检查下面的片段:

if (message.content === 'memes') {
  const EMOJI = '🤏🏾';
  const filter = (reaction, user) =>
    reaction.emoji.name === EMOJI && !user.bot;
  const collector = message.createReactionCollector(filter, {
    // optional, the maximum time in ms the collector collects reactions
    time: 10000,
    // maximum number of users to react
    // it includes the bot's reaction too
    maxUsers: 3,
  });

  message.react(EMOJI);

  collector.on('collect', (reaction, user) => {
    // it fires every time someone reacts to the message
    console.log('collected', reaction);
  });

  collector.on('end', async (collected, reason) => {
    const reaction = collected.get(EMOJI);

    if (!reaction) {
      return message.channel.send('Oops, there were no reactions');
    }

    // it includes the bot too
    const allUsers = await reaction.users.fetch();
    // collection of users who reacted to the message, bot removed
    const users = allUsers.filter((user) => user != client.user);

    message.channel.send(
      `Poll has ended because of ${reason}. Received ${users.size} reactions.`
    );
    message.channel.send(
      `Users reacted: ${users.array().join(', ')}`
    );

    // remove reactions
    message.reactions.removeAll();
  });
}

If you want to have an array of emojis, you can use an EMOJIS array instead:如果你想要一个表情符号数组,你可以使用一个EMOJIS数组来代替:

if (message.content === 'memes') {
  const EMOJIS = ['🤏🏾', '😱'];
  const filter = (reaction, user) =>
    EMOJIS.includes(reaction.emoji.name) && !user.bot;
  const collector = message.createReactionCollector(filter, {
    time: 10000,
    maxUsers: 3,
  });

  EMOJIS.forEach((emoji) => message.react(emoji));

  collector.on('collect', (reaction, user) => {
    // it fires every time someone reacts to the message
  });

  collector.on('end', async (collected, reason) => {
    if (!collected.size) {
      message.reactions.removeAll();
      return message.channel.send('Oops, there were no reactions');
    }

    // you can use the collected reactions any way you want
    console.log(collected);

    // remove reactions
    message.reactions.removeAll();
  });
}

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

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