Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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 ๐Ÿธ!`);
      });
    }
  }```
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

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") {
        // ... //
    }
});
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!