How to wait and then check for how many reactions a message has (discord.js)

Viewed 304

I am currently attempting to write a command where you can recommend a new idea to the server. I want the command to be able (on its own) to check whenever there have been 3 of either the or the emoji. Then, the bot will delete the old message and send a new one that states whether the command has been approved or denied.

    async run(client, message, args){
        const channelToSend = message.guild.channels.cache.find(channel => channel.name === '-recommendations');
        const Author = message.author;        ;
        const newEmbed = new Discord.MessageEmbed()
        .setColor('#FFB6C1')
        .setTitle(`Server Recommendation:`)
        .setDescription(args.slice(0).join(' '))
        .setAuthor(message.author.tag, message.author.avatarURL())

        const errorEmbed = new Discord.MessageEmbed()
        .setColor('#FFB6C1')
        .setDescription('You need to reccomend something.');

        const sentEmbed = new Discord.MessageEmbed()
        .setColor('RANDOM')
        .setDescription('Recommendation sent! Thank you.');

        try {
          gogo();
        } catch(e) {
          
        } 

      async function gogo() {
        if(message.length <= '0' || message.content == null || message.content == undefined){
            message.channel.send(errorEmbed);
        } else{
          const question = await channelToSend.send(newEmbed); 

          let ryC = 0;

          let rnC = 0;


          ['', ''].forEach(async el => await question.react(el)); 

          const collector = question.createReactionCollector(filter, { max: 4, time: 5000 });
          collector.on('collect', r => 
          {
            if(r == ''){
              ryc ++;
            } else if (r == ''){
              rnC ++;
            } 
          });
          collector.on('end', async collected => {
            if(ryC >= rnC){
              const verifiedEmbed = new Discord.MessageEmbed()
              .setColor('#FFB6C1')
              .setTitle(`Server Recommendation:`)
              .setDescription(args.slice(0).join(' '))
              .setAuthor(message.author.tag, message.author.avatarURL())
              .setFooter('**Recommendation Approved**');

              await question.delete();

              channelToSend.send(verifiedEmbed);
            }

            if(rnC > ryC){
              const verifiedEmbed = new Discord.MessageEmbed()
              .setColor('#FFB6C1')
              .setTitle(`Server Recommendation:`)
              .setDescription(args.slice(0).join(' '))
              .setAuthor(message.author.tag, message.author.avatarURL())
              .setFooter('**Recommendation Denied**');

              await question.delete();

              channelToSend.send(verifiedEmbed);
            }
          });
        }
      }
    }
3 Answers

You can make a callback to react each time an emoji reaction is post. You count the emojis and react accordingly.

client.on('messageReactionAdd', (reaction, author) => {
    if(reaction.message.author == bot){
    //Here you can check the message itself, the author, a tag on the message or in its content, title ...
        if(reaction.message.reactions.cache.get('').count == 3){
            //Delete message
            //Post New message
        }
    }
}

It would be better to add a callback directly to the message, but I don't know how to do it. Even better, you could attach the callback when you post your recommendation and detach it when emoji >= 3 with client.off('messageReactionAdd', ...).

Edit : I changed the if statement. Because you don't want to keep the message in memory, you just need to find a way that fits you to determine if this message is a recommendation.

Try to create a global variable that increments when someone clicks on the specific reaction. Every time one of the reactions is clicked you can check which has already 3 reacts.

After some time of re-assessment and a partial answer from @Zekovski I have come up with this code.

Essentially it's impossible for me to recheck the message forever until it has so many reactions, so I added the messageReactionAdd event, from there I check if the channel is where the reactions are being sent, next we make sure the message is an embed, from there on we see if the message has three reaction or three reactions.

Finally, we take the original embed and edit it adding on total yesses and no-s. Then send it!

    const baseEvent = require("../BaseClasses/baseEvent");
    const Discord = require('discord.js')
    
    module.exports = class messageReactionAdd extends baseEvent {
        constructor(){
            super('messageReactionAdd');
        }
    
        async run(client, reaction){
          const recChannel = '773052032151715846';
          if(reaction.message.channel.id == recChannel){
            if(reaction.message.embeds[0].title.includes('Recommendation')){
              let yC = reaction.message.reactions.cache.get('').count;
              let nC = reaction.message.reactions.cache.get('').count;
              let total = [];
              reaction.message.reactions.cache.forEach(e => {
                if(e.emoji.name == ''){
                  //yC = yC + 1;
                  console.log(yC);
                }
                else if(e.emoji.name == ''){
                  //nC = nC + 1;
                  console.log(nC);
                }
              });
    
              console.log(`${yC} : yesses , ${nC} : no's`);
              console.log(total);
    
              if(yC >=2){
                const oldEmbed = reaction.message.embeds[0];
                const verifiedEmbed = new Discord.MessageEmbed(oldEmbed)
                .addFields(
                  { name: 'Total :', value: yC, inline: true },
                  { name: 'Total :', value: nC, inline: true}
                ).setFooter('**Recommendation Approved**');
    
                  reaction.message.delete();
                  reaction.message.channel.send(verifiedEmbed);
              } else if(nC >=3){
                  const oldEmbed = reaction.message.embeds[0];
                  const deniedEmbed = new Discord.MessageEmbed(oldEmbed).setFooter('**Recommendation Denied**');
    
                  reaction.message.delete();
                  recChannel.send(deniedEmbed);
              }
            }
          }
        }
    }
Related