Home > Blockchain >  how to check if a message has a ping in it discord.js
how to check if a message has a ping in it discord.js

Time:05-04

I know you can use:

client.on('messageCreate', msg => {
    if(msg.content.includes("<@") && msg.content.includes(">")){
        //some code
    }
})

but this method isn't really the best. Is there a better way to check for pings in a discord message?

CodePudding user response:

If you want to check if there are any mentions in a message, you can just get the mentions in the message and check the size of them by using:

if (message.mentions.users.size !== 0) {}

You can then replace the .users with .channels to check if there were any mentioned channels and .roles to check if any role was mentioned

CodePudding user response:

You can use message.mentions.members.first() to get the first pinged user. So, using your example:

if(message.mentions.members.first()){
    //some code
}
  • Related