Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
How to fetch user's tag with id discord.js in variable

I am making a leaderboard system for my bot, and want to display the user's tag along with their amount of money.

Here is the code

        const lb = users
                  .slice(0)
                  .sort(({ Bobux: a }, { Bobux: b }) => b - a)
                  .map(
                    
                    ({ User, Bobux }, pos) => `${pos + 1}. ${(client.users.cache.get(User)).tag} - ${commaNumber(Bobux)} Bobux`,
                  );
    
              const newnew =     lb.slice(0, 30)
                  const embed = new Discord.MessageEmbed()
                  .setTitle('Global Leaderboard For Most Bobux - Top 30')
                  .setDescription(newnew)

Over here, I map it, and attempt to get the user's tag with their id (User is the user's id in the database)

User looks like this '43908439084329084'

                  .map(
                    
                    ({ User, Bobux }, pos) => `${pos + 1}. ${(client.users.cache.get(User)).tag} - ${commaNumber(Bobux)} Bobux`,
                  );

When I do this, I get the error

0|index  | TypeError: Cannot read properties of undefined (reading 'tag')

How do I fetch the user's tag with their id.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can fetch the user via UserManager#fetch method. The following code will check if the user is cached, if not it will fetch that user from Discord.

const user = await client.users.fetch(UserId).catch(() => null);
if (!user) console.log("That user is not available");
else console.log(user.tag);

Documentation: https://discord.js.org/#/docs/discord.js/stable/class/UserManager

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!