skip to Main Content

I’m developing a discord bot and faced a small problem, I use "messageReactionRemove" and "messageReactionAdd" to remove or add certain roles, the problem happens when the user leaves the server, as when he leaves, certain user reactions are removed using "guildMemberRemove", which again calls "messageReactionRemove", but the user is no longer on the server (error).

client.on('messageReactionRemove', (reaction, user) => {
    if (reaction.message.id == '1110918756189884496') {
    let role = reaction.message.guild.roles.cache.find(role => role.name === "Verified");
    reaction.message.guild.members.cache.get(user.id).roles.remove(role); 
    }
});
client.on("guildMemberRemove", (member) => {
    const channel = member.guild.channels.cache.find((ch) => ch.id === '1109131318350053457');
    channel.messages.fetch(`1110918756189884496`).then(message => {
    message.reactions.cache.find(reaction => reaction.emoji.name == "✅").users.remove(member.user.id);
    })
});

If I use fetch() instead of cache, then I get an error (Unknown member)

Do you have any ideas? Thanks for any information

2

Answers


  1. Chosen as BEST ANSWER

    I found another similar post, but I didn't find an answer there: How to check the existence of a user in Discord.js without errors?

    However, I found a way around it! Instead of deleting the user's reaction when he leaves the server, we delete it as soon as he connects to the server (in my case this logic works, since the server content is unlocked for the user only after agreeing to the rules => reacting)

    client.on('guildMemberAdd', member => {
    const channel = member.guild.channels.cache.find((ch) => ch.id === '1109131318350053457');
    channel.messages.fetch(`1110918756189884496`).then(message => {
    message.reactions.cache.find(reaction => reaction.emoji.name == "✅").users.remove(member.user.id);
    })
    

    });


  2. You can use .catch to catch the error

    client.on("guildMemberRemove", (member) => {
        member.guild.members.fetch(member.id)
            .then(member => {
                member.guild.channels.cache.get('1109131318350053457').messages.fetch(`1110918756189884496`)
                    .then(message => {
                        message.reactions.cache.find(reaction => reaction.emoji.name == "✅").users.remove(member.user.id);
                    })
            })
            .catch(() => { })
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search