Combined Timer Command

Asked Mar 22, 2021·27 replies·Last activity 5 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

I have MULTIPLE timer commands. !timer1 starts a 1 minute timer that uses a 3rd party PHP, and so on. Because the script used is simple, I'm sure I can automate it using a single command. I made an attempt but it isn't working. Can you assist with understanding what is wrong with it?

ONE MINUTE TIMER (works)
$(urlfetch https://rokbot.xyz/smm.php?msg=One Minute timer started48 seconds36 seconds24 seconds12 seconds`Time's up&i=12&d=1)

ANY MINUTE TIMER (up to one hour)
$(eval i=$(querystring);
if(i < 1){
if(i <= 60){
j=i*12;
"$(urlfetch https://rokbot.xyz/smm.php?msg="+i+" minute timer started"+(j*4)+" seconds"+(j*3)+" seconds"+(j*2)+" seconds"+j+" seconds`Time's up&i="+j+"&d=1)";
}
else {
"The requested timer is over an hour and not started.";
}
else{ "Invalid Timer Requested";
}

27 replies

You’ll wanna do the eval inside of the urlfetch the urlfetch is called before the eval even starts otherwise you’ll probably have to use an alias to send the final urlfetch to a second command that can detect whether or not a timer started

can you please show me an example? Maybe some other thread that uses something like this?

As far as I know of this is the first like this but I have made a command before that uses a similar idea /t/customapi-blackjack-command/1534589348744269994 it take the input in the first one structures the output then that out put gets given to a second command that then outputs the message.

Hey @Quiet Flamingo!

Timers using such API won't work above 5min of total runtime as it's the lifetime of the Nightbot token.

Good to know. Once I figure out how to get it working, I'll limit it from 1 to 5 minutes then. Thanks for that!

I don't understand how this post helps me.

I changed it to 5 minutes as the max and attempted to swap so that urlfetch goes first and eval goes inside but it still doesn't work for me. Here is the code:

$(urlfetch $(eval i=$(querystring);
if(i < 1){
if(i <= 5){
j=i*12;
"https://rokbot.xyz/smm.php?msg="+i+" minute timer started"+(j*4)+" seconds"+(j*3)+" seconds"+(j*2)+" seconds"+j+" seconds`Time's up&i="+j+"&d=1)";
}
else {
"The requested timer is over an hour and not started.";
}
else{"Invalid Timer Requested";
}

image.png

Yes @Polar Cat but I don't care about the ID of the person and I want to limit the timer from 1 minute to 5 minutes--hence the extra code.

It's not more or less code, there's only a few changes to do, it's not that much:

!addcom !timer -a=_timer $(eval q=`$(1)`; isNaN(q)||q<1?i=60000:q>5?i=300000:i=q*60000;i)
!addcom _timer $(urlfetch https://vxrl.xyz/smm/$(eval `${$(query)/60000} minutes timer starting...`)/The timer ended.?i=$(query)&d=1)

why two commands? what does each do?

The first one converts the amount of minutes to the format the multiple message API understands, it also makes sure that it's a number of minutes that is fed into the command, and that it's between 1 and 5 minutes.
The second command is an alias of the first one, which means it's called right after the first one and that the output of the first command is fed into it, it contains the API that makes the timer possible.
We need two commands because of how Nightbot processes variables, or basically how JS works.
So you just need to call the first command with the amount of minutes you wish your timer to be: !timer xx being the amount of minutes.

Thank you Emily... how can I learn more about the coding language being used here since I feel like most responses are given with the assumption that users know the language inside out?

The language we write commands in is JavaScript as it's the one Nightbot understands. We also know the documentation.
We don't write the commands with the assumption people know how it works. Most people never ask and only care if it the commands give the results they want or not, and it would take us too much time to explain the commands in detail every time we write code for people.
We're all volunteers, we have other things to do, so we can't afford to waste time on explaining things people don't care about. But if someone ask we gladly explain, and we try to make it as beginner-friendly as possible.
That's said, I forgot to tell you that you only need to call the first command when I gave you the commands, that's my bad.

Oh, I do understand and didn't mean to offend but I've written a little bit in C and can't seem to be able to write code for Nightbot in such way where it's clean (or readable for me), like other languages, and easy to follow. I don't want to be given commands and move on--I legitimately want to learn to create them myself--hence my question.

Sorry if it came out as if I was offended, I wasn't.
I also learned a bit of C before jumping into JS, I believe knowing C helped me greatly—and still does—when it came to understanding it as a new language.
That said, when we write commands for Nightbot, it's not the cleanest, because we have to save on characters as much as possible, so it's not the best way to learn JS. I never write code this way outside of Nightbot commands.
If I can recommend a few good resources to learn JS, there's W3Schools for beginners, and MDN for more advanced stuff. I started with W3Schools, it was great, until I found it didn't go deep enough, but I couldn't understand the MDN documentation until I had a good grasp of the language.

I added the code but never really tested it until a few days ago and forgot to mention here what the results were. When I call !timer #, I get the following:

chykityta: !timer 1
Nightbot: -a=_timer 60000

Also, nothing happened after a minute--as if the timer didn't run. Please advise.

Sounds like you added the commands through the dashboard and copy/pasted the -a=_timer part in the response field. So remove it, and put _timer in the alias field.
The code I gave was to add the commands through the chat, if you add it through the dashboard, you need to dispatch the elements as the !addcom command would.

duhhh... l feel... ughhh; Thank you!

How different would it be if I wanted to also use seconds? like "!timer s 90" or "!timer m 2"

I understand that !timer currently checks that it is a number between 1 and 5 so would need different logic . I still don't quite understand the 60000 so not too sure how to change !timer

if the first character is m, allow from 1-5, then use _timer
else character is s, allow from 30 to 600, then use _timers

Feel free to tell me to shush 😃

The 60000 is in milliseconds, because the API works with milliseconds, so it's 1 minute.
Therefore, if you want to use seconds, there isn't much to edit:

!addcom !timer -a=_timer $(eval q=`$(1)`; isNaN(q)||q<5?i=5000:q>300?i=300000:i=q*1000;i)
!addcom _timer $(urlfetch https://vxrl.xyz/smm/$(eval `${$(query)/1000} seconds timer starting...`)/The timer ended.?i=$(query)&d=1)

is this a second command? How is this used?

Yes, and same as before, just with the number of seconds, so for 1min30s for example, it's !timer 90, just like you asked.

Oh, sorry, I completely misread your message...
Well, okay, if you want to start including m and s as extra variables, it's gonna be a bit different.

So I'll make the command default to seconds, therefore if you don't specify m it'll be seconds, and whether you specify m before or after the number won't matter either:

!addcom !timer -a=_timer $(eval q=`$(query)`.toLowerCase(); n=q.match(/\s*\d+\s*/i); n?n=n[0]:n=0; if(q.includes(`m`)){n*=60} n<5?i=5*1000:n>290?i=290*1000:i=n*1000;i)
!addcom _timer $(urlfetch https://vxrl.xyz/smm/$(eval `${Math.floor($(query)/(60*1000))} minute(s) and ${($(query)/1000)%60} seconds timer starting...`)/The timer ended.?i=$(query)&d=1)

Edit: it appears the maximum amount of time allowed by the API is 4 minutes 50 seconds, and not 5 minutes, so I updated the code accordingly.

Polar Cat said:
$(urlfetch https://vxrl.xyz/smm/$(eval ${Math.floor($(query)/(60*1000))} minute(s) and ${($(query)/1000)%60} seconds timer starting...)/The timer ended.?i=$(query)&d=1)

@Polar Cat YOU'RE NUTS... I finally got a chance to add it and I love it!!! I wish I knew how to split up code logic in my brain like you do!

Glad to hear! And it's just practice, I was a beginner and didn't know much a few years ago. ;)

Didn't find your answer? The community is on Discord.

Ask on Discord