Irtiza Khan Labib
Irtiza Khan Labib

Reputation: 75

I want to set when someone mention me bot will respond

I'm trying to make my discord.js bot send messages when someone pings me. How can I do that??

I was trying with that code:

if(message.content === "<@723821826291138611>") {
message.channel.send("Hello, sup? ")
}

but that doesn't work. how can I do that?

Upvotes: 1

Views: 624

Answers (2)

Elitezen
Elitezen

Reputation: 6730

The best - and most conventional - way to do this is to check the MessageMentions#(users|members) collection.

const { 
   mentions:{ 
      users,
      repliedUser
   }
} = message;

if (users.has("723821826291138611") && !repliedUser) {
   // Your code
}

This will return true if the mentioned is found in any order not only first, it's possible for the API to emit the mentions in different orders if multiple mentions were given. I wouldn't recommend searching the message.content string either.

Upvotes: 3

MegaMix_Craft
MegaMix_Craft

Reputation: 2208

You have to use if(message.content.includes("<@723821826291138611>")) instead of if(message.content === "<@723821826291138611>") to make it work!

But also you can use this code to do it:

let mentioned = message.mentions.members.first();
if(mentioned && mentioned.id == "723821826291138611") {
if(message.mentions.repliedUser) return;
message.channel.send("YOUR_TEXT")
}

Upvotes: 3

Related Questions