Discordjs - lastMessageId always null event after fetching

Viewed 195

Introduction :

I'm pretty new at creating discord BOT and I'm currently trying to make like a ticket bot. In order to do that, I need to check if the user that add the reaction is allow to do it or not.

Code :

I already checked this (successfully) when the user is using a command thanks to this (isAllow is a custom function that works when passing member object in params) :

if (Common.isAllow(message.member)) { /* code ... */ }

But I also want to check for the roles of the user that is reacting to a message, in order to create a new channel if the user is allowed to do it.

The problem is that when I get the user object, the lastMessageId is null, so I cannot get member object and check its roles.

The code that is checking for reaction is this one :

client.on("messageReactionAdd", async (reaction, user) => {
    // When a reaction is received, check if the structure is partial
    if (reaction.partial) {
        // If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
        try {
            await reaction.fetch();
        } catch (error) {
            console.error('Something went wrong when fetching the message: ', error);
            // Return as `reaction.message.author` may be undefined/null
            return;
        }
    }
    
    switch (reaction.message.channel.id) {
        // Check if the message has been sent into the lawyer contact channel
        case Config.CHANNELS.LAWYER_CONTACT:
        case Config.CHANNELS.CONFIG:
            checkForLawyerChannelReaction(reaction, user);
            break;
        default:
            break; 
    }    
});

If the reaction has been made in the good channel (LAWYER_CONTACT) or in the config channel (CONFIG), it will call this function, which is the one trying to check for permission :

function checkForLawyerChannelReaction(reaction, user) {
    console.log('checkForLawyerChanelReaction');
    console.log(user);

    if (Common.isAllow( /* member object */ ) { /* code ... */ }

The response I get for console.log(user) is : Result

I already tried to fetch user data like this, and it doesn't change a thing : console.log(await client.users.fetch('123456789123456789')); (ID has been changed, of course)

Note that the BOT has joined (again) yesterday with administrator permissions, and message has been sent by the user since yesterday.

Does somebody has a fix for this or information that I may not have yet ?

Thank you for your time.

1 Answers

You can get the GuildMember object by passing the user as an argument into the Guild.member(user) method.

const guildMember = reaction.message.guild.member(user);
Related