If else if else statement including empty string

Hi. Basically i want to an !elo command with random responses, whenever anyone enters their username afterwards. If however they type in my username, i want a specific response to show up. this works fine with the following code.

$(eval 
	const responses = 
		['Lvl: 1 Elo: 134', 
		'Lvl: 1 Elo: 134', 
		'Lvl: 2 Elo: 853', 
		'Lvl: 4 Elo: 1212', 
		'Lvl: 3 Elo: 998', 
		'Lvl: 3 Elo: 1043', 
		'Lvl: 1 Elo: 403', 
		'Lvl: 1 Elo: 754', 
		'Lvl: 2 Elo: 924', 
		'Lvl: 3 Elo: 952', 
		'Lvl: 1 Elo: 69', ]; 
	if ( "$query" == "_de4d") 
		{"Lvl: 11 Elo: 9001";} 
	else responses[Math.floor(Math.random() * responses.length)];
)

However, i want it to show the same response if the query is empty. I thought i could realise it this way:

$(eval 
	const responses = 
		['Lvl: 1 Elo: 134', 
		'Lvl: 1 Elo: 134', 
		'Lvl: 2 Elo: 853', 
		'Lvl: 4 Elo: 1212', 
		'Lvl: 3 Elo: 998', 
		'Lvl: 3 Elo: 1043', 
		'Lvl: 1 Elo: 403', 
		'Lvl: 1 Elo: 754', 
		'Lvl: 2 Elo: 924', 
		'Lvl: 3 Elo: 952', 
		'Lvl: 1 Elo: 69', ]; 
	if ( "$query" == "_de4d") 
		{"Lvl: 11 Elo: 9001";}
	else if ( "$query" = null ) 
        {"Lvl: 11 Elo: 9001";}
	else responses[Math.floor(Math.random() * responses.length)];
)

in chat i get the response: Invalid left-hand side in assignment. not quite sure what im doing wrong. if anyone has an idea, i would greatly appreciate it.

greetings,
de4d

Hey @de4d_tv!

I see three big issues with your code:

  1. it’s $(query), not $query, like every other Nightbot variables,
  2. in if("$query" = null) you reassign $query because you use a single equal sign, this will cause the test to be ignored, and could throw an error (note: you try to compare a string with null, which is its own object, this could work if you used the double equal sign ==, but not the triple ===),
  3. $(query) won’t give you null or undefined, the way to test if there’s one is to do a if($(query)){}, so if you want to test its absence, do if(!$(query)){} (note: $(1) will give you null if empty, just like other arguments).

I see one minor issue that could throw you an error every once in a while:

  • the last comma in your array, remove it, you don’t need it here.

And finally, I see six ways to optimize your code for Nightbot, since you’re limited to 400 characters per command, it’s better to save as most as you can, by:

  1. combining the tests for the empty query or for the query equal to your username, given that it’s the same output: if(!$(query) || '$(query)' === '_de4d'){'Lvl: 11 Elo: 9001'}, it’s also a good practice in general,
  2. removing const and let, proper JS requires you to use them, but it’s overkill for Nightbot,
  3. shortening your variables names to a single letter,
  4. removing most spaces and tabs, as well as some semicolons,
  5. using Pastebin to store your array of various responses,
  6. storing the value of Nightbot’s variables in other variables if you’re going to use them multiple times.

In the end, this gives us the following code:

$(eval q='$(query)';r=$(urlfetch json https://pastebin.com/raw/XXXXXXXX);if(!q||q=='_de4d'){'Lvl: 11 Elo: 9001'}else{r[Math.floor(Math.random()*r.length)]})

We could do even better by using the ternary operator:

$(eval q='$(query)';r=$(urlfetch json https://pastebin.com/raw/XXXXXXXX);!q||q=='_de4d'?'Lvl: 11 Elo: 9001':r[Math.floor(Math.random()*r.length)])

And your Pastebin should look like this, following the JSON syntax:

[
  "Lvl: 1 Elo: 134", 
  "Lvl: 1 Elo: 134", 
  "Lvl: 2 Elo: 853", 
  "Lvl: 4 Elo: 1212", 
  "Lvl: 3 Elo: 998", 
  "Lvl: 3 Elo: 1043", 
  "Lvl: 1 Elo: 403", 
  "Lvl: 1 Elo: 754", 
  "Lvl: 2 Elo: 924", 
  "Lvl: 3 Elo: 952", 
  "Lvl: 1 Elo: 69"
]

Don’t forget to replace XXXXXXXX by the paste ID, make sure the paste isn’t private, unlisted is fine, and that its expiration date is set to never, if you plan on editing it later, consider creating an account.

2 Likes

much love for the very detailed answer! greatly appreciate you! will go through these and hopefully wont have to come back here as often :smiley:

1 Like

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