How could I create/modify a global / static parameter in Nightbot?

My request may be kinda hard, so I’m gonna explain it.
Considering a !test command, I’m trying to create some variables, e.g. win, loss, that would change when the code is used. These global variables (integers starting by 0) will change, as the player will win/lose against Nightbot, thanks for a flag variable, like:
if(win==true) win++; else loss++;
But, when another user will call !test again, those variables won’t be resetted, keeping their values, like other global Nightbot values, such as $(count) do… Is there a way to do this?


$(count) is the only Nightbot variable that keeps its value between command uses. For anything more complex you would need a workaround, for example a $(urlfetch) request to some custom API.

This workaround is possible using ehsankia’s quote custom API - read more about it here.

Click this link. It will generate three links and two tokens, a public token (8 characters long) and a private token (16 characters long). The public token is located within the first generated link. The private token is located within both the second and third generated links. The tokens are found after token= and before &data=$(querystring) Copy them and keep them somewhere safe!

I’ve set up 3 commands below:
!test and _test which work in tandem to add a win or loss depending on some condition
!score which displays the numbers of wins and losses

Either copy and paste these commands into chat or add them through the Nightbot dashboard. Add the commands through chat only if you are sure no one else is there, otherwise they might see your private token (which is used to edit the list)! Replace PUBLIC_TOKEN with your public token and PRIVATE_TOKEN with your private token. In the command response for !test you will have to write some code and a conditional that determines whether the player wins or loses against Nightbot.

!addcom -cd=5 !test -a=_test $(eval // Write some code here... if(CONDITION){`PRIVATE_TOKEN&data=win`;}else{`PRIVATE_TOKEN&data=loss`;})

!addcom -cd=5 _test $(eval a=`$(urlfetch$(query))`;b=decodeURIComponent(`$(querystring)`);b.includes(`PRIVATE_TOKEN`)?(b.includes(`win`)?`The player won against Nightbot!`:`The player lost to Nightbot...`):` `)

!addcom -cd=5 !score $(eval a=`$(urlfetch json`;w=a.match(/win/g);w?w.length:0;l=a.match(/loss/g);l?l.length:0;`${w} wins and ${l} losses.`;)
1 Like

Ok that sounds kinda complicated… But will check it out! Thank you! :slight_smile: :v:
I’ve got another question: Is it possible to make Nightbot manipulate the $(count) ? Something like: if(win==false) $(count) = $(count) - 1; So it will just count how many times Nightbot got “defeated”?

Nightbot variables don’t work the same way as JS variables do.

The $(count) variable increments itself whenever it is called. It is not possible to make $(count) variable decrement itself. Also remember that inner Nightbot variables always evaluate before outer variables. For example, if you have the following command response:

$(eval $(count) = $(count) - 1)

When you run the command, the inner $(count) variables evaluate first. Let’s say the initial value in $(count) is 0. For each time $(count) is called, it increments itself. Therefore the code that $(eval) actually executes becomes:

$(eval 1 = 2 - 1)

This is not valid JS code.

1 Like

I see… So basically it’s impossible even to edit it externally, like:
!addcom !test $(eval var ct=$count; var x = Math.random(); if(x<0.5) {ct–; /*… missing command to re-set $(count) into the new value */}) ?
So the only way to edit it is the dashboard, and only the owner can edit it, right? Or is there a way to set the new count within a condition, without going to dashboard?

You can edit a count. It’s done through !editcom

!editcom !command -c=NEW_VALUE
1 Like

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