c4k3ss
c4k3ss

Reputation: 23

How to count reactions of multiple messages?

I am making a discord bot and I want it to say multiple messages and react with a check to it, that I can do, but, I don't know how to make it count the check from the messages so I can make it select the top 5 messages with the most reactions, I can probably to all of it myself but I just need to know how to make it count the reactions from the messages. Please help.

Upvotes: 1

Views: 123

Answers (1)

Akio
Akio

Reputation: 723

Let's say you have ID of those messages in an array.

const allMessages = [
    "1000001",
    "1000002",
    "1000003",
    "1000004",
    "1000005"
];

So we'll run a loop through them

for(let i = 0; i<= allMessages.length; i++){
    let messageID = allMessages[i];
}

Now we'll catch each message separately.
(I'll guess all the messages are in a channel catched as Channel)

for(let i = 0; i<= allMessages.length; i++){
    let messageID = allMessages[i];
    let message = Channel.messages.cache.fetch(messageID);
}

Now we can count the reactions

for(let i = 0; i<= allMessages.length; i++){
    let messageID = allMessages[i];
    let message = Channel.messages.cache.fetch(messageID);
    let count = message.reactions.cache.size;
}

And then we'll add them to a variable which will show all the counts together.
Let's put the code in a function for using anync

Final code

const allMessages = [    //All the ID of messages you wanna count reactions.
    "1000001",
    "1000002",
    "1000003",
    "1000004",
    "1000005"
];
CountReactions = async (MessageArray) => {
    let totalCount = 0;
    for(let i = 0; i<= MessageArray.length; i++){
        let messageID = MessageArray[i];
        let message = await Channel.messages.cache.fetch(messageID);
        let count = message.reactions.cache.size;
        totalCount += count;
    }
    console.log(totalCount);
    //In case
    //return(totalCount);
}
CountReactions(allMessages);

If you want to count any specific emoji / reaction then do this

const allMessages = [    //All the ID of messages you wanna count reactions.
    "1000001",
    "1000002",
    "1000003",
    "1000004",
    "1000005"
];
CountReactions = async (MessageArray) => {
    let totalCount = 0;
    for(let i = 0; i<= MessageArray.length; i++){
        let messageID = MessageArray[i];
        let message = await Channel.messages.cache.fetch(messageID);
        let count = message.reactions.cache.get('♥️').count;
        totalCount += count;
    }
    console.log(totalCount);
    //Just in case
    //return(totalCount);
}
CountReactions(allMessages);

Sorry if it's overcomplicating

Upvotes: 2

Related Questions