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

156
Views
displays as undefined if person dosen't chat discord js v12

Image of users displaying as undefined I wan't to display usernames instead of pinging user in the leaderboard embed? At this point i have no idea what to do i asked people to help and couldn't figure it out. Image it says undefined until member chats.

Leaberboard command file

         let UserJSON = JSON.parse(Fs.readFileSync("./DataBase/users.json"));
        var Sorted = Object.entries(UserJSON).sort((a, b) => b[1].money- a[1].money);
        if (Sorted.length > 10) Sorted = Sorted.slice(0, 10);

        var LBString = "";
        Sorted.forEach(user => {
            LBString += `${client.users.cache.find(u => u.id == user[0])} - ${user[1].money}\n`;
        });
        var LBEmbed = new Discord.MessageEmbed()
            .setColor('#FFFFFF')
            .setTitle("**Coins Leaderboard**")
            .setDescription(LBString);
        message.channel.send(LBEmbed);
        client.channels.cache.get('941016208940077086').send(`${message.author.username} has used $coinslb`)


//users.json file
{"599675959888707594":{"money":492,"lastbeg":1644580691540,"lastwork":1644580757380},"617797297924866093":{"money":0,"lastbeg":0,"lastwork":0}}

I want the leaderboard to display usernames not @ them in the embed

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

After user[0]), put .username to get only the username, like this:

LBString += `${client.users.cache.find(u => u.id == user[0]).username} - ${user[1].money}\n`;

Also, it shows undefined when they haven't chatted before because they aren't in your database. You could get the guild (server) you're in and fetch the member by ID, instead of from cache:

guild.members.fetch('user_id_here').username;
about 4 years ago · Juan Pablo Isaza Report

0

I noticed that if you add it AFTER there is anything in users.json it will be displayed as undefined so when you add the .username you need to reset ur users.json

about 4 years ago · Juan Pablo Isaza 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!