简体   繁体   中英

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

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM