0

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

  • 1
    Does this answer your question? [What is the difference between a User and a GuildMember in discord.js?](https://stackoverflow.com/questions/63979076/what-is-the-difference-between-a-user-and-a-guildmember-in-discord-js) – Lioness100 Nov 26 '20 at 22:26
  • I could not find a good answer for me there, no – DreamRocket Nov 27 '20 at 21:42
  • The problem is described in the link I provided. You used `message.mentions.users.first()`, which returns a User, but then expected it to be a GuildMember – Lioness100 Nov 28 '20 at 00:57
  • message.mentions.members.first() – Wide Nov 28 '20 at 03:27
  • Oh, I see, I was confused about what the link was fixing. Thank you! – DreamRocket Dec 01 '20 at 02:29

0 Answers0