Home > Software design >  How do I change a users nick name using Discord.js v14
How do I change a users nick name using Discord.js v14

Time:12-26

I am using the following code to register the slash command:

const nick = new SlashCommandBuilder()
  .setName('nick')
  .setDescription('Change the nickname of a user')
  .setDefaultMemberPermissions(PermissionFlagsBits.ChangeNickname)
  .addUserOption((option) =>
    option
      .setName('user')
      .setDescription('The user to change the nickname')
      .setRequired(true),
  )
  .addStringOption((option) =>
    option.setName('nickname').setDescription('New nickname for the user'),
  );

The logic:

client.on('interactionCreate', (interaction) =>{
  if (interaction.commandName === 'nick') {
    const user = interaction.options.getUser('user');
    const name = interaction.options.getString('nickname');
    user.setNickname(name);
  }
});

Which gives me the following error:

   user.setNickname(name)
         ^

TypeError: user.setNickname is not a function

CodePudding user response:

It's because interaction.user is a User, not a GuildMember and only members have the setNickname() method.

You should get the member instead of the user. Try something like this:

const member = await interaction.options.getMember('user')
user.setNickname(name)
  • Related