Ban user if they mention over 5 people

Viewed 57

Heyo Axmyo here,

I want to autoban user if they mention over 5 humans, not bots

but the code I tried to use doesn't do anything..

Any help would be appreciated!

client.on("message", message => {
  if(message.mentions.members.size > 5) {
    message.author.ban();
  } 
})
1 Answers

You're in the right direction, your if statement is correct.

message.author returns a User.

but .ban() is available only on GuildMember.

So you need to convert the User to a GuildMember. Luckily the message object contains the Guild that it was sent in, so you can do the following:

client.on("message", message => {
  if(message.mentions.members.size > 5) {
    const user = message.author;
    const guildMember = message.guild.member(user);
    guildMember.ban();
  } 
})

Notice that .ban() returns a promise, so it might be a good idea to do the following, if you want to do additional things after the ban:

client.on("message", async message => {
  if(message.mentions.members.size > 5) {
    const user = message.author;
    const guildMember = message.guild.member(user);
    await guildMember.ban();

    // Do other things...
  } 
})

Edit

message object has a member property on it, thanks Itamar S

client.on("message", async message => {
  if(message.mentions.members.size <= 5) return;

  await message.member.ban();
  // Do other things... 
})
Related