Change user nickname with discord.js

前端 未结 11 1392
清酒与你
清酒与你 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 19:44

    You can use the GuildMember#setNickname function like this:

    var nick = message.content.split(" ").slice(1).join(" ");
    message.member.setNickname(nick);
    
    0 讨论(0)
  • 2020-12-16 19:49

    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 讨论(0)
  • 2020-12-16 19:49

    Try to use this: Docs Guild#Me

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

    Note: bot should get MANAGE_NICKNAMES

    0 讨论(0)
  • 2020-12-16 19:51

    It should work if you use this

    message.guild.members.get("Random ID or something").setNickname("RandomName")
    
    0 讨论(0)
  • 2020-12-16 19:52

    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 => {
        if (message.content.toLowerCase().startsWith('changeNick')) {
           var newNickname = message.content.replace("changeNick ", "");
           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(message.content.toLowerCase().startsWith("changeNick")){
            if(!message.mentions.users.first()) return message.channel.send("You didn't specify a user!");
             const user = message.mentions.members.first();
             const username = message.guild.members.cache.find(userI => userI.id == user);
             var newNickname = message.content.replace("changeNick "+args[1], "");
             username.setNickname(newNickname);
        }
    });
    
    0 讨论(0)
  • 2020-12-16 19:53

    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 讨论(0)
提交回复
热议问题