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.');
}
});
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');
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.