Search code examples
javascriptnode.jsdiscorddiscord.jsroles

Discord.js cannot find role from guild


I'm making a Discord bot using discord.js, however, it is not letting me to get the guild's roles and finding a particular one. It's kind of confusing, so here is the code:

//Note: this is inside a messageReactionAdd event
let guild = reaction.message.guild;
console.log(guild.id);
console.log(guild.roles);
let role = guild.roles.cache.find(r => r.name == "Member");
reaction.member.roles.add(role);

The console spits out the guild ID and a long map of roles and info, but it says that roles is not a property of undefined at the line that defines role. I have no idea why it's doing this.


Solution

  • Your problem isn't on the role itself, your problem is on reaction.member. Reaction doesn't have member property.

    Just change this->

    reaction.member.roles.add(role);
    

    To (messageReactionAdd has user Parameter) ->

    guild.members.cache.get(user.id).roles.add(role)