Pandicon
Pandicon

Reputation: 430

How to display mentiones users avatar in an embed discord.js

I'm trying to make a user info command, but I've run into a problem with displaying people's avatars. My command is supposed to respond to this: &profile {user ping/user ID}.

So, if you do &profile @user#0001, it will display the info of user#0001. If you do &profile 123456789123456789, it will display info about the user with ID 123456789132456. If you do &profile without a mention, it will display info about you.

Whenever I run &profile, it posts the embed without problems. But, if I mention someone or put someone's ID, it crashes. This is my code:

const { DiscordAPIError } = require("discord.js");
const Discord = require("discord.js");

module.exports = {
  name: "profile",
  description: "The bot will return the info about the user",
  execute(message, args) {
    let userinfoget =
      message.guild.member(message.mentions.members.first()) ||
      message.guild.members.cache.get(args[0]);
    //var userinfoget = message.mentions.members.first();
    //var patter = message.author;
    //var randompatemote = Math.floor(Math.random() * 3) + 1;
    if (!userinfoget) {
      userinfoget = message.author;
    }

    console.log(`User = ${userinfoget}`);

    const embed = new Discord.MessageEmbed()
      .setTitle(`User info`)
      .setColor("#DAF7A6")
      .setAuthor(`${userinfoget.tag}`, userinfoget.displayAvatarURL())
      .addFields({ name: `${userinfoget.tag}`, value: `Here will be the ID` });
    message.channel.send(embed);
  },
};

When I mention someone, it throws the error:

userinfoget.displayAvatarURL is not a function

The same thing happened if I put an ID. But if I put nothing behind the command (so it displays my info), it works. An example of what I'm trying to get is in the attachment.

Example

It displays in the embed correctly, but only if it displays mine, otherwise it crashes. Any way to fix it?

Upvotes: 0

Views: 1578

Answers (1)

Jakye
Jakye

Reputation: 6625

I'm assuming the error comes from this line:

let userinfoget = message.guild.member(message.mentions.members.first()) || message.guild.members.cache.get(args[0])

Guild.member() method converts a User object to a GuildMember object, if possible. message.mentions.members.first(), if defined, is already a GuildMember object.

All you need to do is change the aforementioned line with:

let userinfoget = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.guild.member(message.author)

You can remove the following line now:

if (!userinfoget) {
    userinfoget = message.author
};

Upvotes: 1

Related Questions