I have created a ban command that works fine, unless you try to kick someone with a higher role than the bot. Is there a simple way of checking if the user can be kicked? I tried checking if the client has higher roles than the person, but then it fails when you try to kick the owner of the server if he has no roles. My code is here:
var User = message.guild.member(message.mentions.members.first()) || message.guild.members.cache.get(args[0]);
if (!User) return message.reply("please mention the one who should be banned")
if(User.id === message.author.id) {
return message.reply('can not allow self-harm')
};
if (message.member.roles.highest.comparePositionTo(User.roles.highest) <= 0) {
return message.reply('you can not ban a user with the same (or higher) permissions as you')
};
var banReason = args
banReason.shift()
banReason = banReason.toString()
banReason = banReason.replace(/,/g, ' ')
if (!banReason) {
banReason = "No reason provided"
};
var banReason2 = `Banned by ${message.author.tag} -` + ' ' + banReason
User.ban({reason: banReason2}).catch(error => {
message.channel.send(`Failed to ban <@${User.id}>`)
return console.log(error)
})
message.reply(`you have succesfully banned <@${User.id}> with the reason **${banReason}** <a:excitedwave:758043553573372117>`);
const embed = new Discord.MessageEmbed()
.setColor('#FF0000')
.setTitle(`You have been banned from ${message.guild.name}`)
.setDescription(`**Banned by:** <@${message.author.id}>\n**Ban reason:** ${banReason}`)
.setTimestamp()
User.user.send(embed).catch(error => {
message.channel.send(`Failed to DM <@${User.id}> the info about this action`)
console.log(error)
})
Thanks in advance