Magixxz
Magixxz

Reputation: 41

How can you check if the bot is being pinged?

I am creating a command that "kills" people. I want the bot to return the message "Ha! You thought! @Author died!" if they ping the bot. (How do I get the Bot to see if it is pinged?) Answer has been updated and now fully works.

const Discord = require('discord.js');
const bot = new Discord.Client();

module.exports = {
    name: 'kill',
    description: 'kills',
    execute(message, args, bot) {
      message.delete({ timeout: 30000 });
  
      if (message.content.startsWith('-kill')) {
        const target = message.mentions.users.first();
        const memberTarget = message.guild.members.cache.get(target.id);
  
        if (message.mentions.has(bot.user)) {
          return message.channel.send(`HA! SIKE! <@${message.author.id}> died.`);
        }
        message.channel.send(`<@${memberTarget.user.id}> has died!`);
        console.log(`<@${memberTarget.user.id} died.`);
      }
    },
  };

Upvotes: 3

Views: 2427

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

You can use message.mentions.has(bot.user) to check if the bot is mentioned. Also, make sure you're passing the bot to your execute() method instead of instantiate a new one:

module.exports = {
  name: 'kill',
  description: 'kills',
  execute(message, args, bot) {
    message.delete({ timeout: 30000 });

    if (message.content.startsWith('!kill')) {
      const target = message.mentions.users.first();
      const memberTarget = message.guild.members.cache.get(target.id);

      if (message.mentions.has(bot.user)) {
        return message.channel.send(`HA! SIKE! <@${message.author.id}> died.`);
      }
      message.channel.send(`<@${memberTarget.user.id}> has died!`);
      console.log(`<@${memberTarget.user.id} died.`);
    }
  },
};

And in your main JS file pass down the bot object:

// ...
client.commands.get('kill').execute(message, args, bot) 
// ...

enter image description here enter image description here

Upvotes: 2

Related Questions