1
const member = message.mentions.members.first()
if (!message.mentions.users.size) {
  return message.channel.send('You have to mention a user to kick him!');
} else if (member === message.author) {
  return message.channel.send('You can\'t kick yourself!');
} else if (!member.kickable) {
  return message.channel.send('You cannot kick this user!');
} else {
  return member
    .kick()
    .then(() => message.message.send(`${member.user.tag} was kicked.`))
    .catch(error => message.message.send(`Sorry, an error occured.`))
}

I'm trying to create a code that kicks a user, but in some situations I don't want the user to be able to kick another user. One of these situations is when the user to be kicked is the same one who writes the command. The problem is that i can't do this piece of code and every time it tells me: 'You cannot kick this user!' when it should say: 'You can't kick myself'. How can i display that message when i try to kick myself? P.S. Sorry for my english, i'm italian

2 Answers2

0

Replace } else if (member === message.author) { with } else if (member.id === message.author.id) {

That way it checks for the ID instead of the member / message author object, and the ID will definetly be identical.

Aci
  • 546
  • 5
  • 22
0

Your if block is not triggering for two reasons. First of all, MessageMentions.members returns a collection of GuildMembers, while Message.author returns a User object. You can learn about the difference here.

Second of all, even if the two objects were the same type, Objects in JavaScript can fundamentally never be equal.

console.log({} === {}); // false
console.log({ property: true } == { property: true }); // false
console.log({ string: 'something' } === { string: 'something' }); // false

Luckily, you can use the Collection.prototype.equals method, which takes this into account. So if you wanted to, you could use this:

// const member = message.mentions.members.first()
if (member.equals(message.member))

However, there's another method that's easier, which is simply comparing the id property of both objects. The id is the same in both the User and GuildMember objects, and it is completely unique to someone's Discord profile.

// const member = message.mentions.members.first()
if (member.id === message.author.id)
Lioness100
  • 8,260
  • 6
  • 18
  • 49