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');
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
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!
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
It should work if you use this
message.guild.members.get("Random ID or something").setNickname("RandomName")
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 ', '')
});
}
});
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!");
来源:https://stackoverflow.com/questions/41247353/change-user-nickname-with-discord-js