discord.jspingreply

Discord bot : How to know when a user is pinged in the message and not because it's a reply


The goal :

I am trying to get the bot to send a message when a specific user is pinged in a message.
So I tried with this :

if (message.mentions.has(bot.users.cache.get(userID)))

And it's working fine, if you ping the user, the bot sends a message. Excepted...

The problem :

The bot also react when you simply reply to the user without pinging the user in the message, and I don't want that.
The only way I can see is :

if (message.content.includes(userID))

Solution

  • You should check directly if they were pinged in the message.

    const userRegex = new RegExp(`<@!?${userID}>`)
    if (message.content.match(userRegex)) {
      //user has been mentioned and it was not because of reply
    }