Help with Conditional Statements

Hi,
I am just starting out and need help with conditional statements. (I setup the following to run tests on conditional statements in Nightbot commands)

I managed to get this 1st block of code to function properly
(I just changed MYUSERNAME between tests to either match or not match my own username):

!editcom !test $(eval var a = “$(user)”;
if (a === “MYUSERNAME”)
{“Correct User”;}
else
{“Wrong User”;}
)

However, when I then tried this version, using requests to the Custom Quote API
(I replaced TOKEN with my actual token):

!editcom !test $(eval var a = “$(user)”;
if (a === “MYUSERNAME”)
{
($(urlfetch http://twitch.center/customapi/addquote?token=TOKEN&data=$(querystring)); )
(“Correct User”);
}
else
{
($(urlfetch http://twitch.center/customapi/addquote?token=TOKEN&data=Wrong); )
(“Wrong User”);
}
)

… the result is that both urlfetch requests are performed
(2 entries are made with the string I enter and “Wrong”)
as well as Nightbot returning

Right-hand side of ‘instanceof’ is not an object

instead of “Correct User” or “Wrong User”.

My 1st question is why are both parts of the If/Else getting ran?
2nd question is why is Nightbot outputting this error rather than one of the text outputs?

Thanks.

This just means your code is bad. The following revised code won’t produce an instanceof error.

$(eval
var a = "$(user)";
if (a === "MYUSERNAME"){
"$(urlfetch http://twitch.center/customapi/addquote?token=TOKEN&data=$(querystring)) Correct User";
}else{
"$(urlfetch http://twitch.center/customapi/addquote?token=TOKEN&data=Wrong) Wrong User";
}
)

However, this is the glaring issue. In the context of Nightbot commands, not Javascript, inner variables are evaluated before outer ones. This means that both urlfetches will be executed before the eval is run, and the above code will not work as intended.

1 Like

Thanks so much for the response.
I figured out that, in addition to brackets and syntax, I didn’t realise that $(eval) returned a string instead of just running the code within it.

I also managed to figure out a workaround for this particular example:

!editcom !test $(urlfetch http://twitch.center/customapi/addquote?token=TOKEN&data=$(eval
var a = “$(user)”;
if (a === “MYUSERNAME”)
{
“$(querystring)”;
}
else
{
“Wrong User”;
}
))

This doesn’t solve the issue if I wanted to run 2 different commands based on am If/Else, but it does for the same command at least.

What method do most people use in Nightbot when they want to carry out a different actions based on an If/Else result? Is this outside the scope of what Nightbot can do without Custom API?
Thanks again :slight_smile:

I would do exactly as you did; nest an eval, which handles an if/else, inside other variables. You should also know that Nightbot allows up to 2 levels of variable nesting. Beyond that, variables don’t evaluate properly. If I wanted to write a command which involves logic that absolutely must go beyond 2 levels of nesting, I would use command aliases, which I think is what you must do if you still want to output different text based on the result of the if/else.

1 Like

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