LinusDropTips
LinusDropTips

Reputation: 81

Discord.js get user by ID

Hi I have this avatar command and I'm wondering instead of mentioning a user you could use their ID instead

if(command === "avatar" || command === "av") {
               const user = message.mentions.users.first() || client.users.cache.get(args[0]) || message.author
       if (!user) {
       user = message.author;
       }
      
          const member = message.guild.member(user)
          const avatar = user.avatarURL({dynamic:true})
          if (!avatar) return message.channel.send("This user doesn't have an avatar to show")
          const embed = new Discord.MessageEmbed()
             .setTitle(`${user.username}'s avatar`)
             .setColor('RANDOM')
             .setDescription(`[Click here for the image link](${avatar})`)
             .setImage(avatar)
             .setFooter(`${client.user.username} 2021`)
             .setTimestamp()
     
         message.channel.send(embed)

Upvotes: 1

Views: 2096

Answers (1)

Yankue
Yankue

Reputation: 378

instead of using client.users.cache.get(args[0]), I would instead recommend message.guild.members.cache.get(args[0]).user, so it gets a member (not user), then selects the user from that member. Also make sure you have intents enabled on the developer portal, as this will not work without them.

To enable them, just select your app, then go on the bot tab, scroll down a little, and make sure both intents are checked.

Upvotes: 1

Related Questions