0

I'm fairly new to javascript and have been using Discord.js to make one or two Discord Bots. I'm working on a feature that sends a message when I'm pinged in my discord server. I've tried a few things and none have worked.

I have this so far, it detects when any user is pinged, not just me.

client.on('message', (message) => {
 if (message.mentions.members.first()) {
  message.channel.send('Do not ping this user.');
 }
});
Lioness100
  • 8,260
  • 6
  • 18
  • 49

1 Answers1

3

You can compare User IDs. How to get User IDs.

if (message.mentions.users.first().id === 'Your ID') // if the person mentioned was you
 return message.channel.send('Do not mention this user');

Furthermore, as the name suggests, Collection.first() will fetch the first element of a collection. This means that the if statement will only return true if the first mention was you. For example:

User: 'Hello @you' // detected
User: 'Hello @notYou and @you' // not detected

To circumvent this, you can use Collection.has():

// will return true if *any* of the mentions were you
if (message.mentions.users.has('Your ID'))
 return message.channel.send('Do not mention this user'); 
Lioness100
  • 8,260
  • 6
  • 18
  • 49