Why isn't my Discord bot displaying the mentioned user's name?

I’m having trouble with a command for my Discord bot. I want it to show the name of the person I mention in an embed, but it’s not working. Here’s my code:

bot.on('message', msg => {
  if (msg.content === '!eliminate') {
    const embed = new Discord.MessageEmbed();
    const sender = msg.author.username;
    const victim = msg.mentions.members.first();
    embed.setTitle(`Time to eliminate ${victim}`);
    embed.setImage('https://example.com/image.jpg');
    embed.setColor('RANDOM');
    embed.setFooter('Target neutralized');
    msg.channel.send(embed);
  }
});

I’ve tried changing victim to msg.mentions.members.first().username, but it still doesn’t work. Any ideas what I’m doing wrong? I’m really stuck and could use some help figuring this out.

I’ve encountered a similar issue before. The problem is likely that you’re trying to use the victim variable directly in the embed title, but it’s not a string. Instead, try accessing the user’s name like this:

const victim = msg.mentions.members.first();
embed.setTitle(`Time to eliminate ${victim.user.username}`);

This should correctly display the mentioned user’s name in your embed. Also, make sure you’re actually mentioning someone when using the command, or it might throw an error. If you want to handle cases where no one is mentioned, you could add a check:

const victim = msg.mentions.members.first();
if (victim) {
    embed.setTitle(`Time to eliminate ${victim.user.username}`);
} else {
    embed.setTitle('No target specified');
}

Hope this helps resolve your issue!

yo dude, i think i know wats up. ur using victim straight up, but its an object. try ${victim.user.username} in ur embed title. that should do the trick! also make sure ur actually mentioning someone when u use the command or itll probs break lol

I ran into this exact problem when I was building my first Discord bot. The issue is that msg.mentions.members.first() returns a GuildMember object, not just the username. To fix it, you need to access the username property like this:

const victim = msg.mentions.members.first();
embed.setTitle(`Time to eliminate ${victim.user.username}`);

Also, a pro tip: add some error handling. Sometimes users might forget to mention someone, which could crash your bot. Something like this works well:

const victim = msg.mentions.members.first();
if (!victim) return msg.reply('You need to mention a user to eliminate!');

This way, your bot stays stable even if users make mistakes. Trust me, it saves a lot of headaches down the line!