Username978
Username978

Reputation: 31

discord.js embed profile picture command with mentions

I'm making a bot in discord.js using the Visual Studio Code app. I'm trying to create a command for profile pictures so when you type -pfp it would show you your profile picture and when you type -pfp @user it would show the person's you mentioned profile picture. (- is the prefix). Though the bot only sends the message without the embed part with the picture. When I mention someone else it does the same thing but mentioning me and not the user.

This is what I have:

    if (!message.content.startsWith(prefix) || message.author.bot) return;
        if (message.content.startsWith(prefix + 'pfp')) {
            message.channel.send('Here is <@'+ message.author.id+ ">'s pfp :)")
            const avatarEmbed = new Discord.MessageEmbed()
                .setColor('#446580')
                .setAuthor('user.username')
                .setImage(message.author.displayAvatarURL());
        } else if (message.content.startsWith(prefix+ 'pfp'+ message.mentions.users)) {
            message.channel.send('Here is <@'+ message.user.id+ ">'s pfp :)")
            const avatarEmbed = new Discord.MessageEmbed()
                .setColor('#446580')
                .setAuthor('user.username')
                .setImage(message.user.displayAvatarURL());
        }
});

Upvotes: 1

Views: 2028

Answers (1)

Worthy Alpaca
Worthy Alpaca

Reputation: 1245

There are two parts here.

First, the bot is only sending the message and not the embed because you only ever send the message. You need a separate line of code to send the embeds.

message.channel.send(avatarEmbed);

Secondly, the bot only ever tags you because of this message.content.startsWith(prefix + 'pfp'). The way you devide between the author and someone else means that it will always match the first case, meaning that the message always starts with prefix + pfp regardless if you tag someone after that.

Now you have a few ways to fix this but I would do it this way.

First you define a new variable, lets call that pfpMember, and you assign that to either the first person you tag or the author of the message.

var pfpMember = message.mentions.members.first() || message.member;

Now that we have a fixed member that is either someone who gets tagged or the author we can just assign the displayAvatarURL function to that member.

.setImage(pfpMember.user.displayAvatarURL());

So your entire command should look a little something like this.

if (message.content.startsWith(prefix + 'pfp')) {
    var pfpMember = message.mentions.members.first() || message.member;
    // we can just put the member object into the string here, that will tag the person
    message.channel.send(`Here is ${pfpMember}'s pfp :)`);

    const avatarEmbed = new Discord.MessageEmbed()
        .setColor('#446580')
        .setAuthor(pfpMemer.user.username)
        .setImage(pfpMember.user.displayAvatarURL());
    
    message.channel.send(avatarEmbed);
}

Upvotes: 2

Related Questions