How do I collect the total reactions for an embed message? | discord.js v13

Viewed 1043

I'm trying to create an embed where the bot has reacted to the message, and I've successfully done that. However, I'm now trying to gather the total reactions for two emojis for that message. I've seen multiple methods online but none of them seem to work properly. Some throw errors and others just don't give any response as if the code doesn't exist to begin with.

Here is my code:

const discord = require('discord.js');
const db = require('quick.db');

module.exports = {
    name: 'command',
    run: async (message, args) => {

        const suggestionEmbed = new discord.MessageEmbed()
            .setColor('#000000')
            .setTitle('[!] Hello')
            .setTimestamp()
        
        message.channel.send({ embeds: [suggestionEmbed] }).then(sEmbed => {
            sEmbed.react('⬆️');
            sEmbed.react('⬇️');
        });

    }
}

NOTE: I thought maybe module.exports might have been the issue so I moved the entire code with all attempts into the main index.js file and still no luck. No output, No errors.

Here is my attempts:

Attempt 1:

    message.channel.send({ embeds: [suggestionEmbed] })
     .then(sEmbed => {
         sEmbed.react("⬆️");
         sEmbed.react("⬇️");
     });
    const filter = reaction => reaction.emoji.name === '⬆️';
    message.awaitReactions(filter, { time: 10000 })
    .then(collected => collected.map(s => message.reply(`we have ${s.count}`)));

Attempt 2:

    message.channel.send({ embeds: [suggestionEmbed] })
     .then(sEmbed => {
         sEmbed.react("⬆️");
         sEmbed.react("⬇️");
     });
     console.log(suggestionEmbed.reactions.cache.get("⬆️").count);

Output for attempt 2:

console.log(suggestionEmbed.reactions.cache.get("⬆️").count);
TypeError: Cannot read property 'cache' of undefined

How do I collect the total count for that specific message ID that has been sent? So I'm able to check whether its at 5 upvotes, 10 downvotes etc...

EDIT:

Attempt 3 with no output as well or error:

    const emojis = ["⬆️", "⬇️"]; //the emojis to react
       
    message.channel.send({ embeds: [suggestionEmbed] }) 
        .then(async function (message) {
    for (let emoji of emojis) { await message.react(emoji) }
    const filter = (reaction, user) => {
        return reaction.emoji.name === '⬆️' && user.id === message.author.id;
    };
    
    message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
        .then(collected => {
            const reaction = collected.first();

            if (reaction.emoji.name === '⬆️') {
                message.reply('you reacted with up.');
            } else {
                message.reply('you reacted with down.');
            }
        })
        .catch(collected => {
            console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
            message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
        });

Attempt 4 with no output or error:

    const time = 1000 //amount of time to collect for in milliseconds
    const emojis = ["⬆️", "⬇️"]; //the emojis to react
    
    message.channel.send({ embeds: [suggestionEmbed] }) 
    .then(async function (message) {
        for (let emoji of emojis) { await message.react(emoji) }
    const filter = (reaction, user) => {
        return reaction.emoji.name === '⬆️' && user.id === message.author.id;
    };
    
    const collector = message.createReactionCollector(filter, { time: time });
    
    collector.on('collect', (reaction, reactionCollector) => {
        console.log(reaction.count) //or do whatever you want with the reactions
        });
    });

Attempt 5 with no error or output:

    const time = 1000 //amount of time to collect for in milliseconds
    const emojis = ["⬆️", "⬇️"]; //the emojis to react
    
    message.channel.send({ embeds: [suggestionEmbed] }) 
    .then(async function (message) {
        for (let emoji of emojis) { await message.react(emoji) }
    const filter = (reaction, user) => {
        return reaction.emoji.name === '⬆️' && user.id === message.author.id;
    };
    
    const collector = message.createReactionCollector(filter, { time: time });
    
    collector.on('collect', (reaction, user) => {
        console.log(reaction.count) //or do whatever you want with the reactions
        });
    });

Attempt 6 with no error but strange output:

    message.channel.send({ embeds: [suggestionEmbed] }).then(sEmbed => {
    sEmbed.react('⬆️');
    sEmbed.react('⬇️');
    //db.set(`${suggestionID}.ChannelID`, sEmbed.ID);
});

const filter = (reaction, user) => {
    return reaction.emoji.name === '⬆️' && user.id === message.author.id;
};

message.awaitReactions({ filter, max: 5, time: 60000, errors: ['time'] })
    .then(collected => console.log(collected.size))
    .catch(collected => {
        console.log(`After a minute, only ${collected.size} out of 5 reacted.`);
});

This one seems to output just 0 as soon as I run the command, and after one minute it does not output the collected.size in the .catch statement. This block of code I took right out of the discord.js documentation Linked here in the await reactions section.

Attempt 7 with wrong output and no error:

    const embedSend = await message.channel.send({ embeds: [suggestionEmbed] }).then(async sEmbed => {
        await sEmbed.react('⬆️');
        await sEmbed.react('⬇️');
        //db.set(`${suggestionID}.ChannelID`, sEmbed.ID);
        const filter = (reaction, user) => {
            return reaction.emoji.name === '⬆️' && user.id === message.author.id;
        };
        
        const reactions = await sEmbed.awaitReactions({ filter, max: 5, time: 10000, errors: ['time'] })
            .then(collected => console.log(collected.size))
            .catch(collected => {
                console.log(`After a minute, only ${collected.size} out of 5 reacted.`);
        });
    });

This one gives me an output of After a minute, only 0 out of 5 reacted. However, I don't understand why its 0? It doesn't count the bots reaction and when I react within these 10 seconds the total reactions are 2 but the output is still 0 although it's meant to be 1. Any idea why?

2 Answers

I found the solution and mixed the issue, the vast majority of my attempts were all correct. The only issue is that with discord.js v13 I forgot to include Intents.FLAGS.GUILD_MESSAGE_REACTIONS in my intents :) Only took me 23+ Hours to figure that out.

What I think you would have use for is collector it's maybe not exactly what your looking for but sure will help I hope!

 const time = 60000 //amount of time to collect for in milliseconds
 const emojis = ["⬆️", "⬇️"]; //the emojis to react

 message.channel.send({ embeds: [suggestionEmbed] }) 
 .then(async function (message) {
    for (let emoji of emojis) { await message.react(emoji) }
const filter = (reaction, user) => {
    return reaction.emoji.name === '⬆️' && user.id === message.author.id;
};

const collector = message.createReactionCollector(filter, { time: time });

collector.on('collect', (reaction, reactionCollector) => {
    console.log(reaction.count) //or do whatever you want with the reactions
    });
});
Related