0
votes

i need some help with this code from my friend using Discord.js. I can't quite seem to code this, what i wanted is :

When the person / member role is higher than the bot, the bot will send a message that the bot is unable to kick the member with the higher role, but when the person below the bot's role will be kicked

The error is : The bot is unable to kick all members above or below the bot's role

Any solution to this? Maybe i'm missing something

        const rolepositionerror = new Discord.MessageEmbed()
        .setDescription('You can\'t kick members with equal or higher position!')
        .setColor("FF0000")

            const targetMember = message.mentions.members.first() || message.guild.members.cache.get(args[0])
 
            const authorHighestRole = message.member.roles.highest
            const targetHighestRole = targetMember.roles.highest
        if(targetHighestRole = authorHighestRole) return message.channel.send(rolepositionerror)

        if(targetMember) {
                targetMember.kick()
                message.channel.send(userkicked)
        }

1

1 Answers

1
votes

Discord.js's GuildMember has a property called GuildMember#kickable which will return true if the user can be kicked, what you want to do is add it to your code:


const targetMember = message.mentions.members.first()
  || message.guild.members.cache.get(args[0]);

if(targetMember) {
  if (targetMember.kickable) {
    // If you can kick the member, then...

    targetMember.kick()
    message.channel.send(userkicked)
  } else {
    // If you can't kick the member, then...

    const roleError = new Discord.MessageEmbed()
      .setDescription(
        "This member can't be kicked!\n" +
        "Check if they have a higher role than the bot and if the bot has permissions!"
      )
      .setColor("FF0000");
    return message.channel.send(roleError)
  }
}

For more info, check out these links: