So I'm currently creating a kick command
and I ran into a problem:
All members are able to kick other members, even if they have a higher role, eg a "normal"
user can kick a admin
or mod
. But I want the bot to return a message
like
"You can't kick members with equal or higher roles!"
This is my current code:
const { MessageEmbed, User } = require('discord.js');
const config = require('../../configs/config.json');
module.exports = {
config: {
name: "kick",
},
run: async (client, message, args) => {
if (!message.member.hasPermission('KICK_MEMBERS', 'ADMINISTRATOR')) {
return message.channel.send({
embed: {
title: 'You are unable to kick members',
color: config.embedcolor
}
})
}
if (!args[0]) {
return message.channel.send({
embed: {
title: 'PLease mention a user',
color: config.embedcolor
}
})
}
if (message.member.permissions.has("MANAGE-MESSAGES", "ADMINISTRATOR"))
return message.channel.send({
embed: {
title: "I can't kick staff members!",
color: config.embedcolor
}
})
const member = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
try {
await member.kick();
await message.channel.send({
embed: {
title: `${member} has been kicked!`,
color: config.embedcolor
}
})
} catch (e) {
return message.channel.send({
embed: {
title: `User isn't in this server!`,
color: config.embedcolor
}
})
}
}
}
Try this method:
if(message.member.roles.highest.comparePositionTo(message.mentions.members.first().roles.highest) > 0){
//member has higher role then first mentioned member
if(member.kickable){ //Just in case
message.mentions.members.first().kick();
}
}else{
message.reply("You can't kick your senpai °^°");
}
Edit: In latest discord.js version highestRole
has been replaced with roles.highest
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.