Ask coding questions

← Back to all posts
Node.js ReplDB Leaderboard
ICUP3339 (0)

I'm just wondering if you can create a ReplDB Leaderoard in Node.js.

I understand how to make use the ReplDB but don't know how to make a Leaderboard.

Can anyone help me?

Comments
hotnewtop
EpicGamer007 (1765)

yes, i know how . basically, what you need to do is whenever a specific event happens, you set the guild id to the score, and every time it changes, you update it.

ICUP3339 (0)

@EpicGamer007 That wouldn't really work for a Bot that is in multiple guilds?

EpicGamer007 (1765)

@ICUP3339 u should set on a key in the db to the guild id, then based on which guild the message came from, you change that key in the db.

ICUP3339 (0)

@EpicGamer007 Would this really work for a bot that is being used in multiple guilds a lot and wouldn't this take up more time? I am looking for a more suitable solution using code?

EpicGamer007 (1765)

@ICUP3339 can i clarify something? For your leaderboard, is the score gonna be for the entire guild or will it be a competition between the people in a guild.

ICUP3339 (0)

@EpicGamer007 It will be a global leaderboard around many different guilds.

EpicGamer007 (1765)

@ICUP3339 so each guild is like a "team", right?

ICUP3339 (0)

@EpicGamer007 No every account has their own balance/bank.

EpicGamer007 (1765)

@ICUP3339 then what I think you could do is set username={score} and then whenever their score changes, update it in the db.

ICUP3339 (0)

@EpicGamer007 I don't you are understanding. If my bot is in multiple servers and is being used quite frequently then how would I change the score that quickly? Also I do require sleep (as an almost functioning human being) so I won't be able to change it 100% of the time.

EpicGamer007 (1765)

@ICUP3339 true, frequency is a problem. I think for me, I am getting confused because I am not sure what your bot does. So my questions are What is your bot supposed to do? and Why is a leaderboard important?

Also, you said:

so I won't be able to change it 100% of the time.

I never said you were the one changing the db, the code itself updates the db.

ICUP3339 (0)

@EpicGamer007 What is your bot supposed to do? My bot is supposed to be an economy/fun bot it has commands such as .grab .bal and .roll
Why is a leader board important? A leader board is important for encouraging the use statistics to try to catch up to the #1 person.

EpicGamer007 (1765)

@ICUP3339 This is an example leaderboard:

RandomUser#0001=310
RandomUser#0002=994
RandomUser#0003=58
RandomUser#0004=411
etc.

Or if you use the db for more than the leaderboard, you can make it something like

leaderboard={"RandomUser#0002": 914, "RandomUser#0001": 414, [etc]}

For the first example, you can do something like:

bot.on("message", (message) => {
  if(message.text == botPrefix + "gainMoney") {

     if(db.get(message.sender) {
        db.set(message.sender, db.get(message.sender) + 1)
     } else {
        db.set(message.sender, 1)
     }

  }
});

// This aint perfect code obv

and for the second way

bot.on("message", (message) => {
  if(message.text == botPrefix + "gainMoney") {

     if(JSON.parse(db.get("leaderboard")).containsKey(message.sender)) {
      const thing = JSON.parse(db.get("leaderboard"));
      thing[message.sender] = thing[message.sender] + 1;
      db.set("leaderboard", JSON.stringify(thing));
    } else {
       const thing = JSON.parse(db.get("leaderboard"));
       thing[message.sender] = 1;
       db.set("leaderboard", JSON.stringify(thing));
    }

  }
});

// This aint perfect code either

this is not perfect code, but you should be able to get the concept of what to do.

ICUP3339 (0)

@EpicGamer007 Bro I know how to to that stuff... And I know how to get all users but don't know how to display their names and their money,

EpicGamer007 (1765)

@ICUP3339 ohhh, I am sorry, i didnt know that was what you meant. For that, you can probably order them by the score and then the top ten people with the highest score, you just put their name and then their score.

client.on('message', message => {
  if (message.content === botPrefix + 'leaderboard') {
    const embed = new MessageEmbed()
      .setTitle('Leaderboard')
      .setColor(0xff0000)
      .setDescription(getData());
    message.channel.send(embed);
  }
});

function getData() {

  const stats = JSON.stringify(db.get("leaderboard")) // if you will use json for storing leaderboard, if you have different pairs for different users, then just do for(key in client.list("").split("\n"){ client.get("key") }

  stats.orderByValuesDescending() // not a real function, just for showing
  let result = "";
  for(let i = stats.length - 1; i > stats.length - 11; i--) {
    result += stats.keyAt(i) +": " + stats[i];
  }

  return result;
  

}

something like this

ICUP3339 (0)

@EpicGamer007 I use the repl.it database