简体   繁体   中英

Discord.js multiple reaction collector

How do I make this code work with multiple reactions? I would like the embed to have multiple reactions, and based on the selected reaction, it would give a different response Here's the code:

module.exports = {
    name: 'test',
    description: "ping command",
    async execute(message, args, Discord){
        
        var newEmbed = new Discord.MessageEmbed()
        .setColor('#A5775C')
        .setTitle('Reactions')
        .setDescription('*React to this!')
        
const MAX_REACTIONS = 1;
        
      const sentMessage = await message.channel.send({embeds: [newEmbed]});

      await sentMessage.react('🐸');

      const filter = (reaction, user) => reaction.emoji.name === '🐸' && !user.bot;

      const collector = sentMessage.createReactionCollector({
        filter,
        max: MAX_REACTIONS,
      });

      collector.on('end', (collected, reason) => {

        if (reason === 'limit')
          return message.channel.send(`You reacted with 🐸!`);
      });
    }
  }```

First you need to add all the needed reactions to the message:

await sentMessage.react('emoji');
await sentMessage.react('emoji_2');
await sentMessage.react('emoji_3');
// ... //

Now, you need to edit the filter(reaction, user) function. reaction.emoji.name === '🐸' means that the collector will only respond to one emoji - 🐸. If you want the collector to respond to different emojis, you can just delete this expression. In this case, the collector will respond to any emoji. But, you can also make the collector respond to a specific list of emoji:

const filter = (reaction, user) => ["emoji1", "emoji2", "emoji3" /* ... */].includes(reaction.emoji.name) && !user.bot
// the collector will now respond to all the emoji in the array

And finally, to display the user-selected emoji instead of 🐸, replace the string in message.channel.send() :

message.channel.send(`You reacted with ${collected.first().emoji.name}!`);

Also, I can offer you 2 more optional things to improve the code:

  1. Since the code is written specifically to collect one reaction, the MAX_REACTIONS constant probably won't change. So you can get rid of it and use 1 when creating the collector.
  2. Because you do not pass the time property when you create the collector, the collector will last indefinitely if the author of the command does not choose a reaction. Therefore, you can pass the idle property and specify the time in milliseconds when creating the collector. After the specified number of milliseconds of inactivity, the collector will stop. For example:
const collector = sentMessage.createReactionCollector({
    filter,
    max: 1,
    idle: 10000 // the collector will stop after 10 seconds of inactivity
});

If the collector stops due to inactivity, reason will be "idle" , inside collector.on("end", ...) :

collector.on('end', (collected, reason) => {
    if (reason === "limit") {
        return message.channel.send(`You reacted with ${collected.first().emoji.name}!`);
    } else if (reason === "idle") {
        // ... //
    }
});

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