I am making a nickname command for my Discord.JS bot. I want to make it so that if the bot or member has a lower role than the target, it returns an error message. I am using the Commando command handler. Here is my code:
const Commando = require('discord.js-commando')
module.exports = class NicknameCommand extends Commando.Command {
constructor(client) {
super(client, {
name: 'nickname',
aliases: ['nick'],
group: 'misc',
memberName: 'nickname',
userPermissions: [
'MANAGE_NICKNAMES',
'CHANGE_NICKNAME'
],
clientPermissions: [
'MANAGE_NICKNAMES',
'CHANGE_NICKNAME'
],
description: 'Changes the nickname of a user',
argsType: 'multiple'
})
}
run = (message, args) => {
const target = message.mentions.users.first()
if(!target) {
return message.reply('Please specify a valid member to change the nickname of')
}
if(message.member.roles.highest.position < target.roles.highest.position) {
return message.channel.send('You do not have a high enough role to change this member\'s nickname')
}
if(message.guild.me.roles.highest.position < target.roles.highest.position) {
return message.channel.send('I do not have a high enough role to change this member\'s nickname')
}
const member = message.guild.members.cache.get(target.id)
args.shift()
const nickname = args.join(' ')
if(nickname.length > 32) {
return message.reply('That nickname is too long! Please make it less than 32 characters')
}
member.setNickname(nickname)
message.reply(`Successfully changed the user's nickname to '${nickname}'`)
}
}
The Discord Commando makes it so it throws an error through a message in the bot, so I do not have a specific console log error, but the bot responded:
An error occurred while running the command: TypeError: Cannot read property 'highest' of undefined
The error appears to be around line 28. I know that the syntax for the highest role is not correct, but I don't know the correct one. Is there a fix to this? Thanks