Trueace004
Trueace004

Reputation: 11

How to get Username & Avatar of that User using UserName

Requirement: I have to get the username and avatar of a given user in the message

if (message.content.startsWith(`${prefix}avatar `)) {
        message.channel.send(`${message.mentions.username}'s avatar: ${message.mentions.avatarURL}`)
}

I got an error:

undefined's avatar: undefined

Upvotes: 0

Views: 231

Answers (1)

Patrick Bell
Patrick Bell

Reputation: 769

The mentions property on the message event object returns a MessageMentions object, not a user object, which is what you seem to be expecting.

The reason for this is you may be mentioning more than 1 user in a message, so this object passes you an array of the relevant users. The following code will fetch the first user mentioned.

if (message.content.startsWith(`${prefix}avatar `)) {
    let firstUser = message.mentions.users.first();
    message.channel.send(`${firstUser.username}'s avatar: ${firstUser.avatarURL}`) 
}

Upvotes: 1

Related Questions