Destinations
Destinations

Reputation: 11

Check if a mention is a user - discord

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!`)
}

Upvotes: 1

Views: 2635

Answers (2)

Mr.Dobby
Mr.Dobby

Reputation: 79

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.

Upvotes: 1

Phillip
Phillip

Reputation: 660

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

Upvotes: 1

Related Questions