Search code examples
javascriptdiscorddiscord.jsbots

Command that can work with and without tagging someone


I'm looking for a way to have a command which will give different responses depending on whether you have tagged someone along with the command.

Here's what I got so far:

module.exports = {
    name: 'attack',
    description: "uses attack embed",
    execute(message, args, Discord) {

var newEmbed = new Discord.MessageEmbed()
        .setColor('#A5775C')
        .setDescription(message.author.username + ' *attacked* '+ message.mentions.members.first().username);

var newEmbed1 = new Discord.MessageEmbed()
        .setColor('#A5775C')
        .setDescription(message.author.username + ' *attacked the enemy!*');

if (message.mentions.members.first()) {
       message.channel.send({embeds: [newEmbed]});
     }else{
       message.channel.send({embeds: [newEmbed1]});
     }
  }
} 

When I try this format, the command where you ping someone works, but when you don't ping someone, nothing happens.

Thanks for the help.


Solution

  • Give this code a try it requires less code and will check for a target.

    let target
    if (message.mentions.members.first() === undefined) {
        target = 'the enemy.'
    } else {
        target = message.mentions.members.first().username
    }
    const newEmbed = new Discord.MessageEmbed()
        .setColor('#A5775C')
        .setDescription(`${message.author} attacked ${target}`)
    
    message.channel.send({
        embeds: [newEmbed]
    })