Change user nickname with discord.js

96.8k views Asked by At

I wonder if you can help (I search it and nothing...) I am learning how to work with discord.js node and I want to change my user nickname (not the username itself)

My code is

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
    console.log('I am ready!');
});

client.on('message', message => {
    if (message.content.includes('changeNick')) {
        client.setNickname({nick: message.content.replace('changeNick ', '')});
    }
});

client.login('token');
14

There are 14 answers

0
Zaidhaan Hussain On

According to the Docs, You can only change a guild members nickname, so you need to get the member object of your client from its id, and set the nickname of it as shown in the docs, Like this:

message.guild.members.get(bot.user.id).setNickname("some nickname");

Note that this will not work always, your client will need to have changeNickname or it won't work

0
Harsh Singh On

According to the example from the docs, something like

message.guild.members.get(message.author.id).setNickname("");

would give you your desired results, if you want the user to only be able to change his/her nicks through the command.

0
E3saR On

Try to use this: Docs Guild#Me

var newNickname = "Nickname !"
message.guild.me.setNickname(newNickname);

Note: bot should get MANAGE_NICKNAMES

0
BotNC On

You can use the GuildMember#setNickname function like this:

var nick = message.content.split(" ").slice(1).join(" ");
message.member.setNickname(nick);
0
Jellybee On

First, to make your command better, main variables.

let messageArray = message.content.split(" ");

let cmd = messageArray[0];

let args = messageArray.slice(1)

To make the command, type this: if (cmd === 'changeNick') { }

Inside of the brackets, we need to type the member variables.

let mentionedMember = message.mentions.members.first()

let mentionedUser = message.mentions.users.first()

let nickname = args.slice(1).join(' ')

After that, to see that it did change the nickname of a member, type this:

message.channel.send(mentionedMember.setNickname(nickname)

0
DEG0029 On

like @Zaidhaan said, heres a little snippet that only trys to set the nickname if it has the perms

    if (message.guild.members.get(bot.user.id).hasPermission("MANAGE_NICKNAMES") && message.guild.members.get(bot.user.id).hasPermission("CHANGE_NICKNAME")) {
        message.guild.members.get(bot.user.id).setNickname("Nickname Here");
    } else {
        message.channel.sendMessage("I dont have the permissons to change my nickname in this server.");
    }

since your getting the user via message.guild.members.get() it requires the perms to edit nicknames on the server

0
Eudrino On

should you use discord.js GuildMember.setNickname function instead of Client.setNickname ()

client.on ('message', async message => {
  var nickname = message.content.split (" ").slice (1).join (" ");
  if (message.content.startsWith ('setNickname')) {
    message.member.setNickname (nickname);
  }
});
0
Dark Ninja On

don't know if you are trying to set a user's nickname or your bot's nickname, so I put both, hope it helps!

Change bot nickname:


const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
    console.log('I am ready!');
});

client.on('message', message => {
    if (message.content.includes('changeNick')) {
        message.guild.me.setNickname(message.content.replace('changeNick ', ''));
    }
});

client.login('token');

User nickname:


const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
    console.log('I am ready!');
});

client.on('message', message => {
    if (message.content.includes('changeNick')) {
        message.member.setNickname(message.content.replace('changeNick ', ''));
    }
});

client.login('token');

Happy to help!

0
Tyler Berger On

Use message.content.startsWith() and message.author.setNickname():

client.on('message', message => {
  if (message.content.startsWith('changeNick')) {
    message.author.setNickname({
      nick: message.content.replace('changeNick ', '')
    });
  }
});
0
SirLemons On

You can use the GuildMember#setNickname method for this one. Let me set a quick example:

const nickName = "PREFERRED NICKNAME"
message.member.setNickname(nickName)
0
K1nzy_The_Idiot On

It should work if you use this

message.guild.members.get("Random ID or something").setNickname("RandomName")
0
FireController1847 On

Discord.js implements changing nicknames by getting the GuildMember from the message, and using the GuildMember#setNickname method. Here's a simple example of setting the nickname of the user who ran the message:

if (message.content.includes('changeNick')) {
    message.member.setNickname(message.content.replace('changeNick ', ''));
}

But this simply won't do in the case of your bot not having permission to set a user's nickname. If you want to set a user's nickname, the bot itself will have to have permission to set nicknames. This requires a little more trickery, but you can do this using Guild#me to get the GuildMember, and then use GuildMember#hasPermission to check for the MANAGE_NICKNAMES permission found in Permissions#Flags. I know this can be a lot to take in, so here's an example of doing everything I just said put together.

if (message.content.includes('changeNick')) {
    if (!message.guild.me.hasPermission('MANAGE_NICKNAMES')) return message.channel.send('I don\'t have permission to change your nickname!');
    message.member.setNickname(message.content.replace('changeNick ', ''));
}

And this will work to set the user who ran the command's nickname. But what if we want to change the BOT'S nickname, not the user's? Well that's simple. We can just replace message.member.setNickname with message.guild.me.setNickname, similarly to how we checked permissions. This will change the bot's nickname instead of the user who ran the command's. Happy coding!

0
asciidude On

To do this, we need to get the first mentioned user. Doing that would be:

let nickedUser = message.mentions.users.first();

After we do that, we need to then get what we want to call the user. I just use nickReason, you can do whatever you want to do.

let nickReason = args.join(" ").slice(22);

Alright, after that we need to check if there was no mentioned user. This is easy:

if(!nickedUser) return message.channel.send("No mentioned user.");

Once after adding that, we need to check if there is a valid name, this is easy as well:

if(!nickReason) return message.channel.send("Not having a name is not a good nickname.");

Now we check if they have the MANAGE_NICKNAMES permissions, which is also easy. We will also check if the mentioned user has MANAGE_NICKNAMES permission:

if(!message.member.hasPermission("MANAGE_NICKNAMES")) return message.channel.send("No permissions!");

if(nick.hasPermission("MANAGE_NICKNAMES")) return message.channel.send("No need to change the nickname of someone that can change nicknames as well.");

Alright, now that we have this, we change their nickname!

nickedUser.setNickname(nickReason);

message.channel.send("Changed " + nickedUser + "'s nickname!");
0
Luuk On

What it looks like is that you are trying to give your bot a nickname To change the bot's nickname use this:

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
    console.log('I am ready!');
});

client.on('message', message => {
    let args = message.content.split(" ");

    if (args[0].toLowerCase() === "changenick") { //command is 'changeNick <new nickname>
       var newNickname = args.slice(1).join(" ");
       message.guild.me.setNickname(newNickname);
    }
});

If you want to change a users nickname you should use this:

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
    console.log('I am ready!');
});

client.on('message', message => {

    let args = message.content.split(" ");

    if(args[0].toLowerCase() === "changenick"){ //command is 'changeNick <@usertag> <new nickname>'
        if(!message.mentions.users.first()) return message.channel.send("You didn't specify a user!");
         const user = message.mentions.members.first();
         var newNickname = args.slice(2).join(" ");
         user.setNickname(newNickname);
    }
});