I was trying to give myself a role when executing a "/" command, and ive tryed many solutions from stackoverflow but none of them work for discord.js v14. Im pretty new to js.
tryed:
const guild = interaction.guild
const role = guild.roles.cache.find(role => role.name === 'Admin');
var dcuser = interaction.user.id
dcuser.roles.add(role)
Got:
TypeError: Cannot read properties of undefined (reading ‘add’)
2
Answers
The user.id (string) does not have .add(role) method
Try this
In
Discord.js v14
, the roles property is not directly accessible on the User object. Instead, you need to get theGuildMember
object for the user and then use the roles property on that object to add or remove roles.Here’s how you can give a user a role with a slash command: