Search code examples
discorddiscord.js

(discord.js) Add role to "user"


Is there a way to get "member" from "user"? I use slash command and i want to add role to a member. But there is no way to find member in slash command. Here the way how I find user.

const user = interaction.options.getUser('user')

when i try

user.roles.add(role id)

some error occur:

user.roles.add(role id)
           ^
"add" is not a function

Solution

  • When you get the user through interaction.options, you receive a User object. But to add roles, you need a GuildMember object. So you have to change user to:

    const user = interaction.guild.members.cache.get(interaction.options.getUser('user').id)
    

    Easier Way

    At the time of writing the answer, I didn't know we could do this but you can just use interaction.options.getMember('user') to get the GuildMember details of the mentioned user. Even if you use .addUserOption to add the option, this method would work. So you can do this:

    const user = interaction.options.getMember('user')