简体   繁体   中英

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. Once it's reached, the collector.on('end') fires, so you can use it to get the number of users reacted, etc.

You can remove the reactions using 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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