Change user nickname with discord.js

前端 未结 11 1442
清酒与你
清酒与你 2020-12-16 19:15

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)

11条回答
  •  鱼传尺愫
    2020-12-16 20:06

    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!");
    

提交回复
热议问题