'cannot read the properties of undefined 'highest'
I want to check if the member that was mentioned role is as the same position of the bot or higher, but I am getting an error.
const member = message.mentions.users.first();
const reason = args.slice(1).join(' ') || 'No reason specified.'
if (member.roles.highest.position >= message.guild.client.roles.highest.position) return message.reply('I cannot moderate this user as their highest role is higher than mine or I have the same highest role position as them.')
The error:
TypeError: Cannot read properties of undefined (reading 'highest')
I am discord.js v13 and Node.js v16
Solution 1:[1]
It's important to remember that in Discord (and, consequently, Discord.js), Users are absolutely not the same as Members. message.mentions.users.first(); returns a User object, which doesn't have any property named roles.
You seem to want the members property on message.mentions instead, which returns a Collection of GuildMember objects, each of which should have the roles property:
const member = message.mentions.members.first();
const reason = args.slice(1).join(' ') || 'No reason specified.'
if (member.roles.highest.position >= message.guild.client.roles.highest.position) return message.reply('I cannot moderate this user as their highest role is higher than mine or I have the same highest role position as them.')
Solution 2:[2]
You are using assigning a User to member, and message.guild.client returns a Client object, which does not have .roles. Use .mentions.members and .guild.me instead
const member = message.mentions.members.first();
const reason = args.slice(1).join(' ') || 'No reason specified.'
if (member.roles.highest.position >= message.guild.me.roles.highest.position) return message.reply('...')
Solution 3:[3]
When you use message.guild.client, you get the client which instantiated the guild and it doesn't have a roles property. Instead you can use:
const member = message.mentions.members.first();
const botMember = message.guild.members.cache.get(client.user.id)
const reason = args.slice(1).join(' ') || 'No reason specified.'
if (member.roles.highest.position >= botMember.roles.highest.position) return message.reply('I cannot moderate this user as their highest role is higher than mine or I have the same highest role position as them.')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | esqew |
| Solution 2 | MrMythical |
| Solution 3 | Caladan |
