Home > Software design >  How to detect how many pings are in a message DISCORD JS
How to detect how many pings are in a message DISCORD JS

Time:12-22

How would I detect how many pings are in a message I'm not sure where to start for this. I'm trying to create a anti-raid bot and I want to detect if someone pings 5 people per message

CodePudding user response:

Simple code that counts unique user mentions in the message.

let count = 0;
for(const user of msg.mentions.users) {
    count  ;
}
if(count>4) {
    msg.delete();
}

CodePudding user response:

You can see how many people were pinged using message.mentions.users Like the following:

Array.from(message.mentions.users).length

It may be 1 over what you need, because Javascript strings start at 0. If so, just add a -1.

Array.from(message.mentions.users).length-1

And please, search this stuff up. I was able to find this on the Discord.js documentation in a matter of seconds.

CodePudding user response:

MessageMentions#user returns a discord.js collection, you can get the length of the collection by simply using message.mentions.users.size, this works with any mentions like

  • .roles
  • .members
  • .channels

and you can just detect using

if(message.mentions.members.size > n) {
  message.delete();
}
  • Related