How to check if mentioned user's roles are higher than message author's roles?
I'm making a mute command, and I want to check if the role of the author is higher than the role of the mentioned user.
This is what I tried
let mentionedrole = message.mentions.roles.first();
let authorrole = message.author.roles
if(mentionedrole.position > authorrole.position) {
message.channel.send(`You don't have access to mute this user. ${message.author}`)
return
}
The error that keeps appearing is
TypeError: Cannot read properties of undefined (reading 'position')
Message.author
doesn't have role
property. This is because author
refers to the user itself, but Message.member
refers to the user as a guild member.
Also, you should use Messsge.member.roles.highest
to get the highest position of role.
The code would be:
let mentionedRole = message.mentions.roles.first();
let authorRole = message.member.roles.highest;
if (mentionedRole.position > authorRole.position) {
message.channel.send(`You don't have access to mute this user. ${message.author}`)
return
}