Auto-Updating Leaderboard

I have a command that generates a random number. Is it possible to create a second command that displays the highest number ever returned from that first command and which person/people got that number, and automatically updates whenever someone gets an equal or greater number on the first command?

Hey @qwertyuiop!

Yes, you can have something like this by using the Quote API. You’ll get your tokens by clicking on the second link in the post, use these in the commands I wrote. To know where to look for your PUBLIC_TOKEN and PRIVATE_TOKEN, look at the field bellow:

$(urlfetch https://twitch.center/customapi/quote?token=PUBLIC_TOKEN&data=$(querystring))
$(urlfetch https://twitch.center/customapi/addquote?token=PRIVATE_TOKEN&data=$(querystring))
$(urlfetch https://twitch.center/customapi/delquote?token=PRIVATE_TOKEN&data=$(querystring))

Keep your PRIVATE_TOKEN secret, don’t share it anywhere. To add a command containing your PRIVATE_TOKEN do it from the dashboard. Don’t use the chat as anyone could monitor it and copy the token, they could then edit your data.


First you’ll have to update your RNG command:

Check the update bellow.

The command generates a random number between 0 and 999,999,999 (you can update this part with yours), then fetches the last maximum number that has been drawn, compare the new number to the current maximum number, if the new number is equal or greater than the current maximum number, it stores the new number as the new maximum number along with the user who drawn it, otherwise it simply replies the user which number they’ve drawn.

Now, for your !leaderboard command, here’s how to do it:

$(eval a=`$(urlfetch https://twitch.center/customapi/quote?token=PUBLIC_TOKEN&data=-1&no_id=1)`; b=a.split(` `).shift(); b==`There`?a=0:``; `The current maximum amount of points drawn is ${a}. You can consult the evolution of the leaderboard here: https://twitch.center/customapi/quote/list?token=PUBLIC_TOKEN`)

If you wish to start the leaderboard over because it reached its maximum, you can use !resetleaderboard, make sure this command is for mods or owner only:

$(urlfetch https://twitch.center/customapi/delquote?token=PRIVATE_TOKEN&clear=1)

First of all, thank you so much for your help! However, there are a few bugs that I’m not sure how to go about fixing:

  1. ${r} does not actually put the value of r in the API; it just puts the string “${r}”
  2. Nightbot simply outputs “Right-hand side of ‘instanceof’ is not an object”. I’m not sure where this bug is even coming from.

If you or anyone else could help fix these bugs, that would be much appreciated!

Hmm, that’s what I get for not testing it before sending it…

So, the second issue was fixed easily, I forgot to assign a variable to $(urlfetch), stupid but easy mistake really. However, for the first issue, it was a little bit more tricky… But I managed to come up with a solution! The issue is that we can’t put a variable inside an $(urlfetch) that isn’t a default Nightbot variable, so the solution is to use an alias command to send the random number and grab it with a $(query), which is a default variable, to add it inside the $(urlfetch). So to bypass this we’ll use an alias command, meaning a second command.

Here’s the code of the main command:

$(eval r=Math.floor(Math.random()*1000000000);t=`$(urlfetch https://twitch.center/customapi/quote?token=PUBLIC_TOKEN&data=-1&no_id=1)`.split(` `);`PRIVATE_TOKEN`+(r>=parseInt(t[0])||t[0]==`There`?`&data=${r} by $(user)`:`$(user) gets ${r} points.`))

Make sure to fill the alias field of the main command with the alias command, I recommend this formatting: if your first command is !command, name the alias _command.

Here’s the code of the alias command:

$(eval a=`$(urlfetch https://twitch.center/customapi/addquote?token=$(query))`;b=decodeURIComponent(`$(querystring)`);if(b.includes(`PRIVATE_TOKEN`)){a.includes(`Successfully added entry`)?(c=b.slice(22).split(` `),`We have a new leader: ${c[2]} with ${c[0]} points!`):b.slice(16);}else{` `;})

Thank you so much @RokettoJanpu for your help with the $(urlfetch)! :heart:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.