So, I am making a bot and here's a part of my code what has a problem:
if(message.content.startsWith("$sudo make-mod ")) {
target = message.mentions.users.first();
let role = message.guild.roles.cache.find(r => r.id = "<@&938235657929773077>")
console.log(target)
target.roles.add(role.id)
}
and I get this error:
target.roles.add(role.id)
^
TypeError: Cannot read property 'add' of undefined
I don't know what's the problem. Can someone help???
When you use message.mentions.users.first(), it returns a User which doesn't have a roles property. Instead, you need a GuildMember, which does have the roles property and you can fetch the GuildMember by using message.mentions.members.first(). You can also add an if check to see whether the mentioned user was valid or not by using this: if (!target) return
You need to use members
target = message.mentions.members.first()