1

I want to make a simple kick function that kicks a player with mention. But I first want to check if a mention is a user. which I have no idea how.

let member = message.mentions.members.first();
if ("member.something") { // if member is a user in the server.
  member.kick().then((member) => {
    channel.send(`${member} has been kicked! :wave:`)
  });
} else {
  channel.send(`Error: ${member} can't be kicked!`)
}
Destinations
  • 11
  • 1
  • 5

2 Answers2

1

Here are the options that I can think of:

User.bot Documentation

if (!member.user.bot)

To check if the user is not a bot

GuildMember.kickable Documentation

if (member.kickable)

To check if the member is kickable

To check if a member exists first, check out: https://stackoverflow.com/a/53284678/11425141

Phillip
  • 631
  • 6
  • 15
  • I have tried member.kickable, but when I try for example to kick a unknown name (a player that doesn't exist in the server), it wouldn't send the error message I did, it'd just give a TypeError message and shut down the bot: ```if (member.kickable) { ^ TypeError: Cannot read property 'kickable' of undefined``` – Destinations Oct 12 '19 at 09:20
  • Also I just realized, when I kick an existing player, it successfully kicks them, but the kick message is not sent. it sends out this error:```(node:4328) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined at member.kick.then (C:\Users\User\projects\bot\index.js:17:29)``` – Destinations Oct 12 '19 at 09:26
  • @Destinations I added a link to a question that can help you determine if the user exists first. The second problem is stating that the variable `channel` is undefined, have you been able to successfully user channel.send()? – Phillip Oct 13 '19 at 23:11
1

You can do this in multiple ways. You can either check for which permissions have in order to disallow other's from kicking them (For instance, KICK_MEMBERS). That would look something like this:

let member = message.mentions.members.first();
if (member.hasPermission("KICK_MEMBERS)) return message.channel.send("That member can also kick!")

You can also check if they got a certain role which disallows them to be kicked. (Could be moderator role, could be a protected role)

//Get role by ID (Allows you to later change its name, ID will remain the same)
let modRole = message.guild.roles.get("MODROLE_ID");
if (member.role.has(modRole.id)) return message.channel.send("This member is a moderator")

//Find role by name. Means if you change the name of this, you need to change code too.
let protectedRole = message.guild.roles.find(r => r.name === "Protected from kicking")
if (member.role.has(protectedRole.id)) return message.channel.send("This member is protected")

Lastly (that I know of), you can check if they're kickable. But all that does is, say if someone above them is trying to kick them, it will do it. So if, say an admin, is testing or something, it will just kick the user if kickable = true

if (member.kickable) {
member.kick()
} else {
message.channel.send("This member is above you!)"
}

If you just want to check if they're an actual user, throw this line in at the top:

if (!member.bot) {
//If they're a user
} else {
//If they're a bot
}

There are obviously a lot of fun things you can do with this. But these are the basics. Hope I helped a bit, and sorry for this late response, I was pretty much just scrolling through the forum and found this unanswered question.

Mr.Dobby
  • 79
  • 6