Home > Enterprise >  Discord.js | Add a role with a reaction
Discord.js | Add a role with a reaction

Time:10-06

I've been on my discord bot for several days because I encountered a problem ...

I wanted to add the possibility of having a role following a reaction.

But it doesn't work as soon as I add the line to add the role, if I remove it it works fine.

code :

bot.on("messageReactionAdd", (reaction, member) => {



    if(reaction.message.id === "894623823855493171"){

        member.roles.add('892423129345978438');
        reaction.message.channel.send(`Tu as réagi : ✅ ${member}`);

    }
       
})

Thank you for your help! ;)

CodePudding user response:

You need to give the user the role as an object and not as a role id. To add the role your code will look like this:

bot.on("messageReactionAdd", (reaction, member) => {

    if(reaction.message.id === "894623823855493171"){
        const role = reaction.message.guild.roles.cache.get("894623823855493171");
        member.roles.add(role);
        reaction.message.channel.send(`Tu as réagi : ✅ ${member}`);
    }
       
})

CodePudding user response:

member is actually a User. The better way to do this is to get the GuildMember from the Guild. You can use GuildMemberManager.resolve

bot.on("messageReactionAdd", (reaction, user) => {
  if(reaction.message.id === "894623823855493171"){
    const member = reaction.guild.members.resolve(user)
    member.roles.add('892423129345978438');
    reaction.message.channel.send(`Tu as réagi : ✅ ${member}`);
  }
})
  • Related