IF/ELSE statement not working properly

I’m trying to create a command that communicates with the api ONLY IF the command has an argument

I tried:

$(eval 
if($(1)!=null){
'$(urlfetch URL$(1))'
} else {
"Missing parameter."
})

$(eval $(1)==null? '$(urlfetch URL$(1))' : "Missing parameter.";)

Both of them sends “null” with the custom url and displays “Missing Parameter” as a message when the user doesnt type anything besides the command(the URL has a propper text response), I’ve tested without the if/else and the message is displayed properly but I need to make sure that the “null” does not get sent with the URL

Hiya, the null here is text, try something like this:

if('$(1)' != 'null')
// parameter set
else
// missing

Did not worked, the ELSE code was still showing in the chat correctly but the urlfetch inside the IF was still running, I ended up adding a validation to check if passed argument is null at the url endpoint

now it is something like this:

$(eval '$(urlfetch URL$(1))';)

then the code checks the value and returns “Missing parameters” if its null or else it will return the sucess message.

I did all of the steps

Hey @MateusZ3!

It’s normal the $(urlfetch) is still running, since it runs inner variables before outer variables.

The solution you got is satisfying given the amount of detail you provided. We assumed you were only fetching data.

If you’re updating a database, but you want to update it only if $(1)!=null, then using an alias command is your best bet. Simply cut the URL in half, keep the first half in the alias and send the second half from the main command only if the if statement runs.

Yes it was to update the database. I’ll keep this in mind, could you give me an example of using something like that?
Thanks!

Sure thing!

This solution comes from @RokettoJanpu! I had the same issue once and he helped me.

So let’s say you want to add a quote only if $(1)!=null:

You’d have your main command like this:

$(eval 
if(`$(1)`!=`null`){
    r=`PRIVATE_TOKEN&data=$(querystring)`;} //if true, send the token as well as the quote text
else{
    r=`Quote missing.`;} //if false, send error message
r) //r is what you send to the alias command

Then you’d have you alias command like that:

$(eval 
a=`$(urlfetch https://twitch.center/customapi/addquote?token=$(query))`; //can only add quote if token, so it won't do anything if error message
b=decodeURIComponent(`$(querystring)`);
b.includes(`PRIVATE_TOKEN`)?`${a}`:`${b}`) //if includes token, display "quote added", else display error message

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