Default value for argument when null (without eval)

Is it possible to pass a default value to an argument if one is not used? For instance:

Command response:
You entered $(1)

Usage is !command 3
Bot responds with “You entered 3”
But if the user just uses !command , without the 3, can I provide a default so that the bot responds with
“You entered 1”

I know this can be done with eval, I am not familiar with JS and trying to avoid that if I can.

Streamelements has this capability but I don’t think it was in their docs either, hoping Nightbot has it ${1|1} was for argument #1 or default of 1

Hiya, that won’t be possible without eval. But if you can explain what you want, I’m sure someone can help you out with the eval code. Example:

$(eval '$(touser)' == '$(user)' ? 'You entered 1' : 'You entered $(1)')

To check if a message was provided you can check if the $(touser) matches $(user), or you would need to check if $(1) == ‘null’.

Thank you for the response @xgerhard
I ended up getting it to work with eval which was a bit of a challenge more so because of the long command and trying to keep the character limit in check as I need the same eval in the command twice along with lengthy urlfetch variables.

I am sending data to an API endpoint and trying to pass along a number from a second (optional) argument in the command. The number is essentially the number of times a thing has happened. If the command is used without entering a number, the assumption is that we should pass along a 1.

The command is used in one of two ways:
!thething CLIPURL (no number which should default/assume a 1) or
!thething CLIPURL 4 (with number and should use the number provided)
The eval working as I need is:

$(eval if("$(2)">1){"$(2)";}else{"1";})

I tried several iterations of evaluating if $(2) was null or undefined and if so, pass in a 1, but I kept getting null as a result. I am guessing this may be due to $(2) not existing at all? So I figured out that if I check to see if it’s greater than 1, even if it doesn’t exist, I suppose that returns the appropriate result.

You can use '$(2)' == 'null' to check if it’s set.
However if you are already sending the data to an endpoint, I personally wouldn’t bother with eval checks, but just send the whole $(querystring) as a paremeter. Then parse and check the input on the server side.

Thanks. Not an option in this case as it’s a public API that I don’t have control over.

I appreciate your help!

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