简体   繁体   中英

Discord.js v12 Nick command

So basically I have this nick command which changes the specified user nickname but if I will try to run that command on a person that has for example "Staff" Role and also me it'll say that it changed my name but I get an error on console. now I assume it's happening because the Staff Role is in a higher position from my bot and Idk what is the command or how to fix it.

client.on('message', async message => {
  if(message.content.toLowerCase().startsWith(prefix + "nick")) {
    if(message.content.toLowerCase() == prefix + "nick") return message.channel.send('Pls mention someone and do `.nick @user [your text]`')
    const newEmbed = new Discord.MessageEmbed()
    .setColor("RANDOM")
    if(!message.member.hasPermission("MANAGE_NICKNAMES")) {
      return message.channel.send("You do not have permission to do that since you're missing `manage_nicknames` perm")}
      if(!message.guild.me.roles.highest.position) return message.channel.send("I can't change that user nickname since my role is lower than his one.")
      if(message.author.bot) return;
      if(message.guild === null) return
      let args = message.content.slice(prefix.length).split(/ +/);
      let mentionedUser = message.mentions.members.first() || message.guild.members.cache.get(args[1])
      if(mentionedUser.hasPermission("ADMINISTRATOR")) return message.channel.send("I can't change this user nickname since he has `administrator` perm")
      let name = args.slice(2).join(" ");
      newEmbed.setDescription("**Mention a user to change his nickname.**")
      .setTimestamp()
      newEmbed.setDescription(`I have successfully changed **${mentionedUser.user.tag}** nickname to **${name}**.`);

      mentionedUser.setNickname(name)
      message.channel.send(newEmbed);
  
} else {
  if(message.content.toLowerCase() === prefix + "help nick") {
    const newEmbed = new Discord.MessageEmbed()
    .setColor('#00B2B2')
    .setTitle('**Nick Help**')
    newEmbed.setDescription('this command is changing peoples names for example `.nick @user 123`.')
    .setFooter(`Requested by ${message.author.tag}`, message.author.displayAvatarURL())
    .setTimestamp();
    message.channel.send(newEmbed)
  }
}})

As you well assumed the error is due to the bot having insufficient permissions on those staff members.

It would have been useful to have the error in your question, but I guess it's about insufficient permissions.

To resolve this the only way I know is to put the role of your bot higher in the hierarchy than the staff role in the discord guild's roles list.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM