Home > other >  how do I make a setNickname command in discord.js
how do I make a setNickname command in discord.js

Time:05-03

I created a Change Nickname command in discord.js v13, and It's not working. I am not getting any errors. My code:

        const target = message.mentions.members.first();
        const nickname = args.slice(1).join(' ');
        if(!target) return message.channel.send('Please specify a target to change nickname');
        if(!nickname) return message.channel.send('Please specify a nickname to change');


        target.setNickname(nickname);

I am using node.js v16

The Commands

This user's nickname should chnage to oskol

CodePudding user response:

I'm going to throw a guess that the nickname you are trying to set is null or empty, this will cause discord to just reset the nickname to the users normal discord username.

Make sure to debug the values that are being passed on and providing such information when making a question on here as it will help people more easily help you.



With that said, the below code worked fine for me

    const target = msg.mentions.members.first();
    if (!target) return msg.reply('Please mention a user');
    const nick = args[1];
    if (!nick) return msg.reply('Please provide a nickname');
    const oldNick = target.nickname;
    if (oldNick === nick) return msg.reply('That user already has that nickname');
    console.log(`Changing ${target.user.tag}'s nickname from ${oldNick} to ${nick}`);
    target.setNickname('');

CodePudding user response:

Your code is working for me, but make sure your bot has the following Permissions and the bot's role is above the role of the users who want to edit his nick: Change this: https://i.stack.imgur.com/lKP9h.png To this: https://i.stack.imgur.com/xX8GF.png Also make sure your command is lowercase because uppercase characters are not allowed in command names.

  • Related