0

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 2

0

Sorry for missunderstanding your question first.

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.fetch(user.id)
            .then(member => {
                member.roles.remove(role);
            })
            .catch(() => { });
    }
});
2
  • Thanks for the answer, but the problem does not happen with "guildMemberRemove" but with "messageReactionRemove", when trying to delete a role for a user who no longer exists, if I use cache to get the user => error, if I use fetch() => error (unknown member), it seems there is no way to check if the user still exists on the server without receiving an error
    – Cutefier
    May 25 at 5:50
  • @Cutefier Sorry for missunderstanding. I edited my answer
    – NoobDev
    May 27 at 17:08
0

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);
})

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.