Skip to content

Discord.js user isnt mentioned

I have a problem with sending messages when a user is tagged or not, I’m working on the pp command (as in dankmemer) but after adding the code so that I can tag a person the bot crashes, what I mean is that after tagging a person it will send a message in the title it will say the user’s penis and if he is not tagged it will say the author, code below 🙂

if (message.content === `${prefix}4fpp`) {
var facts = ["<====8", "<=====8","<=====8","<=======8",] 
var fact = Math.floor(Math.random() * facts.length);
const pp = new Discord.MessageEmbed()
.setTitle(`${message.author.username} , penis:`)
.setDescription(facts[fact])
.setColor(`${GREEN}`)
.setFooter(`Commmand executed by: ${message.author.tag}`, `${message.author.avatarURL()}`)
.setTimestamp()

message.channel.send(pp);
}

Answer

You can get a mentioned user by using message.mentions.users.first()(read more about it in the official documentation). You can use that to format the embed properly by doing something like:

    const facts = ["<====8", "<=====8", "<=====8", "<=======8"];

    const fact = Math.floor(Math.random() * facts.length);

    //Gets the first mentioned user
    const mention = message.mentions.users.first();

    const pp = new Discord.MessageEmbed()
        .setDescription(facts[fact])
        .setColor(GREEN)
        .setFooter(`Commmand executed by: ${message.author.tag}`, message.author.avatarURL())
        .setTimestamp();

    if (mention) { //Set the title to the mentioned user's username if applicable
        pp.setTitle(`${mention.username} , penis:`);
    } else { //Otherwise set it to the message author
        pp.setTitle(`${message.author.username} , penis:`);
    };

    message.channel.send(pp);