0

I try to add a role to a user by ID, but I always receive this error

user.roles.add(role)
                                               ^
TypeError: Cannot read properties of undefined (reading 'add')

Here is my code

let role = Guild.roles.cache.find((r) => r.name === dataobj.Guild.name);

client.users.fetch(useritem).then((user) => {
     user.roles.add(role)
    .catch(error => { })
});

dataobj.Guild.name is the name of the role. useritem is the ID of the user.

var role is correct and output the right role. Var user is correct and output the right user.

I tried many things, but nothing works.

Thank for your help.

node_modules
  • 4,790
  • 6
  • 21
  • 37
  • Does this answer your question? [What is the difference between a User and a GuildMember in discord.js?](https://stackoverflow.com/questions/63979076/what-is-the-difference-between-a-user-and-a-guildmember-in-discord-js) – Cannicide Nov 18 '21 at 19:02

1 Answers1

0

You should fetch guild.members instead of client.users and to add role by its ID you should use role.id instead of role

MegaMix_Craft
  • 2,199
  • 3
  • 10
  • 36