Mr Brickstar
Mr Brickstar

Reputation: 308

Discord.js Leaderboard for XP

In the last days I have developed a Leveling system. Im storing the Data in an JSON file, which is looking like this:

    {
      serverID{
         userID1{
          xp: 10;
          Level: 1
         },
         userID2{
          xp: 10;
          Level: 1
         }
      }
    }

Now I would like to make a leaderboard which is reading from this file. I have looked around in the Internet, but nothing worked. Can someone help me?

Upvotes: 0

Views: 1044

Answers (1)

fowled
fowled

Reputation: 296

In order to make a simple server XP leaderboard, you have to read your XP data file with the fs library and parse it with JSON.

let data = Fs.readFileSync("path-to-json-file", "utf8");
data = JSON.parse(data);

Then, just iterate the keys of your ServerID object, and inside the loop, sort them with the following function:

function sortMyArray() {
    arrayName.sort(function(a, b) {
        return b-a;
    });
}

Finally, just join the array's elements, send the message containing a bit of markdown and you'll be good to go!

message.channel.send(`\`\`\`markdown\n# Leaderboard of the server \n${levels.join("\n")}\`\`\``);

Upvotes: 1

Related Questions