Home > database >  cannot read the properties of undefined 'highest'
cannot read the properties of undefined 'highest'

Time:04-08

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

CodePudding user response:

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.')

CodePudding user response:

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('...')

CodePudding user response:

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.')

  • Related