0

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?

Mr Brickstar
  • 188
  • 1
  • 5
  • 17

1 Answers1

1

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")}\`\`\``);
Fowled
  • 286
  • 4
  • 11
  • It shows `message.channel.send(`\`\`\`markdown\n# Leaderboard of the server \n${levels.join("\n")}\`\`\``); ^ TypeError: levels.join is not a function` – Mr Brickstar Apr 16 '20 at 15:32
  • In fact, "levels" was an example array name - just insert in your code ```[your array name].join(code)``` – Fowled Apr 16 '20 at 15:48
  • Now got this as an output: [Have no space to upload it here xD](https://www.memebotdc.de/Pics/example1.jpg) by [this](https://www.memebotdc.de/Pics/example2.jpg) code – Mr Brickstar Apr 16 '20 at 15:57
  • You closed the ```}``` before the ```.join()```. Here is the correct line: ```${levles[message.guild.id].join(" ")}``` If my post helped, don't forget to upvote it ;) – Fowled Apr 16 '20 at 16:37
  • Now got this ` message.channel.send(`\`\`\`markdown\n# Leaderboard of the server \n${levles[message.guild.id].join("\n")}\`\`\``); ^ TypeError: levles[message.guild.id].join is not a function` sorry if im amnoying xD – Mr Brickstar Apr 16 '20 at 16:45
  • You can check [this file](https://github.com/Ma15fo43/Mango/blob/master/src/commands/levelboard.ts) if you still need help. – Fowled Apr 16 '20 at 17:05
  • I dont think that this is discord.js... But thanks for your Help – Mr Brickstar Apr 16 '20 at 17:26
  • No problem - if you still need help, just reply to this comment, I'm here to help. – Fowled Apr 16 '20 at 17:49