Skip to content
Advertisement

Discord Nickname Command with a specific format

I am relatively new to coding. I already coded a bot with a “nickname changer”. My problem is, that I want it to change to a specific format: “!nick @JohnDoe John” changes name to “JohnDoe [John]”

module.exports = client => {
    
    const command = require('../command')

    command(client, 'nick', (message) => {

        if (!message.member.permissions.has("CHANGE_NICKNAME")) return message.channel.send("Missing Permissions!");
        if (!message.mentions.users.first()) return message.channel.send("Tag somebody!");   
            const user = message.mentions.members.first();
            user.setNickname(message.content.replace('!nick ', '').replace(user, ''));
    })
}

It is for a private server, where everyone can see the users Gametag and their real name.

Thanks in advance for your help 🙂

PS: If there are better ways for that code pls tell me. As I said, I am a noob in programming stuff

Advertisement

Answer

Here is the answer:
command:
!test @user tag
result:
@user[tag]

var  Discord = require('discord.js');
var  client = new Discord.Client();
module.exports = {
  name: 'gamertag',
  description: "this is a gamertag command",
  
  execute(message, args){
    if (!message.member.permissions.has("CHANGE_NICKNAME")) return message.channel.send("Missing Permissions!");
        if (!message.mentions.users.first()) return message.channel.send("Tag somebody!");   
        const user = message.mentions.members.first(); //get mentioned user username
        const username = message.mentions.members.first().displayName; //get mentionde user display username(normal username)
        let  nickName = message.content.split(/s+/).slice(2); //exclude first 2 words 
        var combined = `${username}[${nickName}]`
        user.setNickname(combined);
    }
}

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement