How do I create a command with a running average

I would like to create a couple of commands, for example !addnumber & !average, where mods would be able to use !addnumber and the $(touser) information would be added to an existing number. This existing number will accumulate every time and keep adding the numbers we feed to it. I then plan on using !average to take the average number of all the numbers that the !addnumber command has taken. I have no clue how to or even if Nightbot can do something like this, so any help would be appreciated

Yes firstly generate your two tokens here:
They come between token= and &data
The public token is 8 digits and the private token is 16 digits

!addnumber command:

$(eval a=`$(urlfetch$(1),)`;`Added $(1) to the total average.`)

Add through the dashboard so no one sees your private token.
Set user level to mod

!average command:

$(eval N=`$(urlfetch json`.split(`,`);x=0;for(i=0;i<N.length-1;i++){x=x+parseInt(N[i])};`The average is ${x/(N.length-1)}.`)

This can be added through chat or any other means.
No other special instructions

Note: I didn’t test this but it should work let me know if there are any issues.

Everything works fine except I have you’re code for the !addnumber but with my pivate token in and it keeps coming up with an unexpected identifier. Do you know what that could be?

I forgot a “ ` ” in the command. I edited the command to fix it. It’s supposed to come before and after the “;” in the first command. I accidentally only put it after before.

It all works now, except when I put in multiple numbers the !average result would change randomly and I also don’t know how to clear my tokens, if that’s a thing that can happen.

The average result shouldn’t be changing randomly it should be the average of all the numbers.

You can delete qoutes with this link

Or you can delete them all with this link

I’m assuming that’s what you mean when you say clear your tokens.

That’s the link I was using when I ran !reset and then I input !addnumber 24 and then !average was 12 and then I !addnumber 0 and the !average was zero. so either it isn’t resetting or there is a problem with the code but I don’t see one when I look. P.S. I consistently get those results.

The message of the second command should have this “${x/(N.length-)}” instead of “${x/N.length}”.
From my testing there is no issue with the 0.

For my average command I have
$(eval N=$(urlfetch,);x=0;for(i=0;i<N.length-1;i++){x=x+parseInt(N[i])};The average is ${x/(N.length)}.)
and my addnumber command is
$(eval a=$(urlfetch$(touser),);Added $(touser) to the total.)
Is there an issuse with what I have written?

Why are you using $(touser) in the second command?

Also the message part of the average should be changed to

`The average is ${x/(N.length-1)}.`

I am using @(touser) to take the next numbers that are typed or does the ‘1’ do that originally?

Yeah $(touser) and $(1) do the same thing to my knowledge.

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