Search code examples
node.jsdiscorddiscord.jsembedcollectors

How to add multiple reactions with collectors discord.js


I have been having a problem where I have been trying to figure out how to send an embed and then have the bot react to that embed and if you react to it, the bot will post another embed kind of like this,

-algebra, (bot sends embed) (bot reacts to embed) (command user clicks on reaction) (bot posts another embed specific to the reaction)

I really need it to have multiple reactions such as maybe ten and it would react with 1️⃣, 2️⃣, 3️⃣, 4️⃣, 5️⃣, 6️⃣, 7️⃣, 8️⃣, 9️⃣, 🔟

And if possible I need the bot to do this

-algebra (bot posts embed) (bot reacts) (user reacts to the embed with one of the numbers) (bot posts embed corresponding with reaction) (bot reacts) (user reacts) (bot posts another embed corresponding with that)

making an almost infinite chain.

module.exports = {
    name: 'algebra',
    description: 'This is a command that should help you with algebra',
    async execute(message, args, Discord, client){
        let ageEmbed = new Discord.MessageEmbed()
        .setTitle('Algebra! Its pretty damn hard.')
        .addField('So you are just starting off with Algebra and you want to learn the basics, Well no problem just react with 1️⃣')
        .addField('So you want to know about the questions such as "*x* = 21+5, what is *x*?" well no problem just react with 2️⃣')
        .setColor('#FF1493')
      let msg = await message.channel.send(ageEmbed)
      await msg.react('1️⃣');
      await msg.react('2️⃣');
    
    
      const filter_age = (reaction, user) => {
        return reaction.emoji.name === '1️⃣' || '2️⃣' && user.id === message.author.id && !user.bot;
      }
    
      const collector_age = msg.createReactionCollector(filter_age, {
        time: 30000,
        max: 1
      });
    
    
      collector_age.on('collect', async (reaction, user) => {
        if (reaction.emoji.name === '1️⃣') {
          let justbegginingembed = new Discord.MessageEmbed()
          .setDescription(`Just beggining with Algebra..`)
          .addField("This is where just beggining with algebra will be");
          message.channel.send(justbegginingembed)
        } else {
          collector_age.on('collect', async (reaction, user) => {
            if (reaction.emoji.name === '2️⃣') {
              let algebraembed2 = new Discord.MessageEmbed()
              .setDescription('so you want to learn about ....')
              .addField('THIS IS WHERE');
              message.channel.send(algebraembed2)
            }
        })
    }
})}}    

It doesnt work and I dont understand how to fix it. The bot even posts two embeds at times.

Thank you.


Solution

  • When you return the filter the second reaction will always return true because you're not comparing it to anything.
    To fix this issue, replace return reaction.emoji.name === '1️⃣' || '2️⃣' && user.id === message.author.id && !user.bot; with return reaction.emoji.name === '1️⃣' || reaction.emoji.name === '2️⃣' && user.id === message.author.id && !user.bot;.

    Full example:

    module.exports = {
        name: 'algebra',
        description: 'This is a command that should help you with algebra',
        async execute(message, args, Discord, client){
            let ageEmbed = new Discord.MessageEmbed()
              .setTitle('Algebra! Its pretty damn hard.')
              .addField('So you are just starting off with Algebra and you want to learn the basics, Well no problem just react with 1️⃣')
              .addField('So you want to know about the questions such as "*x* = 21+5, what is *x*?" well no problem just react with 2️⃣')
              .setColor('#FF1493')
            let msg = await message.channel.send(ageEmbed);
            await msg.react('1️⃣');
            await msg.react('2️⃣');
        
        
            const filter_age = (reaction, user) => {
                return reaction.emoji.name === '1️⃣' || reaction.emoji.name === '2️⃣' && user.id === message.author.id && !user.bot;
            }
        
            const collector_age = msg.createReactionCollector(filter_age, {
                time: 30000,
                max: 1
            });
        
        
            collector_age.on('collect', async (reaction, user) => {
                if (reaction.emoji.name === '1️⃣') {
                    let justbegginingembed = new Discord.MessageEmbed()
                      .setDescription(`Just beggining with Algebra..`)
                      .addField("This is where just beggining with algebra will be");
                    message.channel.send(justbegginingembed)
                } else {
                    collector_age.on('collect', async (reaction, user) => {
                        if (reaction.emoji.name === '2️⃣') {
                            let algebraembed2 = new Discord.MessageEmbed()
                              .setDescription('so you want to learn about ....')
                              .addField('THIS IS WHERE');
                            message.channel.send(algebraembed2)
                        }
                    });
                }
            });
        }
    }