Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get reactions on an old message in DiscordJS

I'm using DiscordJs V12.3.1 and I'm trying to return a list of users that have reacted to an old message. (One that was created before the bot was started).

In my current implementation, I get the fetch the message by ID, map through all reactions, then fetch the reaction, and finally map through the users in the reaction.

let cacheChannel = msg.guild.channels.cache.get(channelID); 
if(cacheChannel){
    cacheChannel.messages.fetch(messageID).then(reactionMessage => {                
        reactionMessage.reactions.cache.map(async function(reaction){
            reaction.fetch().then(r => {
                 r.users.cache.map(item => {
                    if(!item.bot) console.log(item.id);                         
                })                        
            })  
        });                                  
    });          
}

The above code works for any reactions that were made since the bot started, however fails to find and users that reacted to the message before the bot was started.

Is there a better way to go about this?

Thanks in advance.

EDIT: Updated the above code based on Lioness100's comment

EDIT2: Managed to get it working with resolve(), see answer below

like image 740
Thorn Avatar asked Sep 18 '25 02:09

Thorn


1 Answers

Right, managed to get it working with the use of .resolve() rather than fetching or mapping through the cache.

var getReactedUsers = async(msg, channelID, messageID, emoji) => {
    let cacheChannel = msg.guild.channels.cache.get(channelID); 
    if(cacheChannel){
        cacheChannel.messages.fetch(messageID).then(reactionMessage => {
            reactionMessage.reactions.resolve(emoji).users.fetch().then(userList => {
                return userList.map((user) => user.id)
            });
        });
    }
}

Hopefully that is helpful if anyone has the same issue. (Note: You have to pass the emoji, but you can retrieve a list of reacted emojis by using the method posted in the question or in Lioness' comment)

And thanks to Lioness100 for your responses.

like image 154
Thorn Avatar answered Sep 19 '25 18:09

Thorn