Looking to make a "Days since X" command that can also be reset to zero


I only know the basics of Nightbot and would appreciate any help to make this a reality for the chat!

The joke is essentially to have a “Days since streamer said xyz” command that automatically counts up once a day. I originally found this post, using nightbot to count how many days it has been since OP’s wedding… but as you can imagine they didn’t ask if they could have mods reset it.

This is the OG solution by Ritik_Ranjan

!addcom -cd=5 !married It has been days since our wedding: $(eval date = new Date(); Math.floor((date.getTime() - 1596825000000 ) / ( 1000 * 3600 * 24)))

Does anyone have any suggestions for the reset command? Ideally we would have !howlong (available to all users), and !reset (available to mods only), something like this;

Nightbot: It has been (value) since streamer said xyz

Also, this is more of the cherry on top, and I could do it manually if it’s too much work. But is there a way for !howlong to not only count up and reset… but also keep a high score? Like I said I can do this manually by looking at the command before resetting and keeping track with another command, but I know there has to be a better way.

and if somehow your brain is large enough to also get a highscore in there then I would be looking for something like

Nightbot: It has been (value) since streamer said xyz / HIGHSCORE: (value)

Many thanks in advance.

Hey @JenKnee!

There’s no simple way to do this, the simplest and cheapest solution to do this is to have the mods manually edit the command in the dashboard.
Or you could also use the Quote API to store the latest date for the $(countup), and use that to display the amount of time:

You’ll get your TOKENS by clicking on the second link in the post, in the Manual Instalation part, 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.

For the command to set and reset the date, we’ll add a quote with the date in the correct format using $(time):

$(urlfetch https://twitch.center/customapi/addquote?token=PRIVATE_TOKEN&data=$(time TIMEZONE "MM DD YYYY HH:mm:ss TIMEZONE"))

You have to replace both TIMEZONE with the same value, you can find a list of supported timezones here. Add it from the dashboard, make sure it’s a moderator command.

And next, for the command to display the amount of elapsed time, we’ll use the last quote in $(countup):

!addcom !howlong It has been $(countup $(urlfetch https://twitch.center/customapi/quote?token=PUBLIC_TOKEN&data=-1)) since $(channel) said SOMETHING

Don’t forget to replace PRIVATE_TOKEN and PUBLIC_TOKEN in the commands’ code, and feel free to rename them to your liking.

Regarding the highscore, there would be a solution since we store all the dates in the Quote API, it’d be to take two consecutive dates, measure the amount of time between the two, repeat that for every set of dates, and then take the highest time difference, however, there’s not really a compact solution for this, so for Nightbot that’ll be a bit much to compute, the Quote API is already quite expensive for this use, let’s not add more.

1 Like

If instead of storing the date you stored time stamps that would make the calculations a lot easier to add a highscore command to this. If you want me to try and write that up it should be pretty simple.

1 Like

I thought about doing that, but I haven’t tested if $(countup) works with timestamps, and I doubt it does, did you test it?

Edit: from my tests it doesn’t work.

1 Like

Yes but you can pretty easily convert from time stamp back into a date format with the eval I haven’t tested it though I would imagine it would work.

This will convert the Unix Milliseconds Timestamp to a human-readable date:

$(eval d=new Date(nnnnnnnnnnnnn); d.getUTCFullYear()+"/"+(d.getUTCMonth()+1)+"/"+d.getUTCDate()+" "+d.getUTCHours()+":"+d.getUTCMinutes()+":"+d.getUTCSeconds())

To get the Unix Milliseconds Timestamp, use x in $(time): $(time "x")
So basically:

$(eval d=new Date($(time "x")); d.getUTCFullYear()+"/"+(d.getUTCMonth()+1)+"/"+d.getUTCDate()+" "+d.getUTCHours()+":"+d.getUTCMinutes()+":"+d.getUTCSeconds())

Now regarding the computing of the high score, the fact that you can do it doesn’t always mean that you should, as I mentioned in my first reply, I’m worried that it’ll be too expensive. Sure at the beginning it’ll be fine, but as the quote list grows larger the more work it’ll be for Nightbot to compute it, as it’s similar to a bubble sort, and that’s very expensive, therefore I think it’s a bad idea. Just thinking in terms of resources here. And I think that’s one of the reason why Night has limited the script time execution to 25ms. I can’t stress enough that I think it’s a terrible idea.

A good way to do it would be to compute it only between the last two timestamps in the list: you’d substract the former to the current one, then you’d probably need to do some left padding to be able to compute the number of days; and finally compare it with the value of the current high score which would be stored in a variable, if the new number is higher, the value of the variable is updated, otherwise it’s not. But given how Nightbot works—that it’s difficult to store variable values and update them—it’s too much work and hardly doable.

1 Like

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