I'm trying to make a command that can change a user's nickname but I can't make it work. I have tried to find solutions but I can't.
Here is my code:
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName("nickname")
.setDescription("Change your nickname")
.addStringOption(option =>
option.setName("nickname")
.setDescription("New Nickname")
.setRequired(true)
),
async execute(interaction) {
const { options } = interaction;
const nick = options.getString("nickname");
const member = interaction.author;
const embed = new EmbedBuilder()
.setDescription(`Succesfully changed your nickname to ${nick}`)
.setColor(0xFFFFFE)
.setTimestamp();
await member.setNickname(`${nick}`)
await interaction.reply({
embeds: [embed],
ephemeral: true
});
}
}
I think the problem is with this but I'm not sure
await member.setNickname(`${nick}`)
It's because interaction.author
is undefined
. You probably thought that it's like message.author
but that's still a User
, not a GuildMember
and only members have the setNickname()
method.
You should get the member instead of the user though. Try something like this:
const member = await interaction.guild.members.fetch(interaction.user.id)
You could also use options.getMember("nickname")
if you used addUserOption()
instead of addStringOption()
.
I was just making a similar command and ran into the same issue as you. I realized the member that I'm trying to change the nickname of, had another role assigned to them, which was higher than the role of the bot. Give this a look and let me know. Also, make sure you have GatewayIntentBits.Guilds
and GatewayIntentBits.GuildMembers
passed in your intents.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.