Discord.js reply if mentioned only without strings

I want that if someone mentions my bot without saying anything else in the same message, that the bot replies to it.

Example: @bot And not: @bot hi

This is my current code:

client.on('message', message => {
    if (message.isMemberMentioned(client.user) && message.author.id === UserId) {
        message.channel.send('message');
    }
});

Answer

Version 12 solution here.

You could get a collection of the mentioned users by using message.mentions.users and first check if there are any mentioned user, and if there is one, check if the first user’s ID is the same as the bot’s ID.

Once you checked if the bot is mentioned, you can see if there is any other content:

client.on('message', (message) => {
  if (message.author.bot) return;

  const args = message.content.split(/ +/);
  const mentionedUsers = message.mentions.users;

  const isBotMentioned = mentionedUsers.size
    ? mentionedUsers.first().id === client.user.id
    : false;

  if (isBotMentioned && args.length === 1) 
    return message.channel.send(`What now, ${message.author}?`);
});

enter image description here