If/else run Commercial depending of Game with Timers

Is there any way to use a command inside eval, such as commercial.
I wish to automate ads to run only for certain games/category with if/else, for the rest a message to subscribe would be displayed instead

1 Like

Hey @John_Doe!

I assume you want to use the !commercial command inside a Timer since you want to automate the response whether it is to run a commercial or the subscribe message.

Sadly this won’t be possible without some concessions. As you can’t edit Timers from the chat, you can’t change the alias of it depending of the game: my first idea was to create a Timer that would be triggered a bit before the commercial Timer to check the game and update the commercial Timer alias if needed, in which case it’d run either the !commercial command or a !sub command. This would have been great.

But we can’t do that, the only solution I came up with is the following:

$(eval g=`$(twitch $(channel) "{{game}}")`; b=[`SPECIFIC_GAME1`,`SPECIFIC_GAME2`];b.includes(g)?`COMMERCIAL_LENGHT OPTIONAL_SILENT`:`0`)

Replace the SPECIFIC_GAMES field and make sure they’re capitalized like Twitch does.
Replace COMMERCIAL_LENGHT and OPTIONAL_SILENT with valid values: documentation
And then set up !commercial as the alias of the Timer.

The issue with this is that you’ll get an error message when no commercials are supposed to run and you won’t get the subscribe message.
In case it’s too big of an issue just turn on the timer when you play the game.
Sorry I couldn’t offer you a better solution.

And to reply to this:

Is there any way to use a command inside eval?

No, Nightbot doesn’t even read its own messages to prevent it from looping, the closest way you can go about this is by using an alias, like I did.

1 Like

Thanks for the hack @Emily , I might use the eval timer you suggested as it is.
I was hopping for a solution that wouldn’t throw an error or have to toggle timer on/off every time

I understand, I wish it was possible too.
I tried to mute the error by testing !commercial 0 silent, with no success sadly…

Hmm, after further testing, it looks like what I suggested doesn’t even work…
The code works just fine, but apparently timers don’t support the $(eval) variable, which I’m not sure why. It would have been nice if it was specified in the documentation, but it’s fine, sometimes you have to figure out things by yourself, it would just have saved me some time.
I tested other solutions and sadly nothing worked, I’m sorry, it looks like there’s no solution in the end.

Timers do support eval, but when alias is provided on a timer the message is not parsed for variables and is just passed as arguments to the alias raw. It looks like commands actually do parse the message for variables for aliases though, so that’s unintentionally inconsistent behavior. I’ve pushed an update tonight which should allow variables to be processed for timer messages before being passed to the alias.

Oh, so that was unintentional. I thought it was a limitation to prevent some issues/exploits.
I was wondering why the $(eval) didn’t work, because I seemed to remember timers with it that were working just fine, so that was a bit odd to me.
Glad to hear about the update!

Yey, thank you.

Small suggestion: If we can’t use alias inside the eval, maybe a way to surpress the error message in chat would be great.

1 Like

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