0
votes

I am making a set nickname command and here is my code:

if (message.mentions.members.first().roles.highest.position > bot.user.roles.highest.position) return message.channel.send("My highest role is lower than the mentioned user's role");

But that code there is an error: Type error: Cannot read property of "highest" of undefined

I also tried this:

if (message.mentions.members.first().roles.highest.comparePositionTo(bot.user.roles.highest) > 0) {
    return message.channel.send("Your highest role is lower than the mentioned user's role");
}

Any idea how to fix?

1
Take a look at this, it may work stackoverflow.com/questions/55888823/…Scurgery

1 Answers

1
votes

bot.user is a User object, which does not contain any server-related data. What you're looking for is a GuildMember object.

In order to get the role for your bot, you need to fetch the member from the guild. This can be achieved with GuildMemberManager.resolve. You can use the guild's built-in property for this.

Your resulting code should look like this:

if(message.mentions.members.first().roles.highest.position > message.guild.members.resolve(bot.user).roles.highest.position)
    return message.channel.send("My highest role is lower than the mentioned user's role");

Sometimes, .resolve will return undefined. In that case, you need to use .fetch. However, since you're getting the roles of your own bot, this shouldn't be much of an issue.