Using Javascript with Eval

I’ve been testing a little with the eval function, however I cant get this to work:

$(eval
 var message = " "; 
 var input = decodeURIComponent("$(querystring)"); 
 count = 1; 
 while(count < 10){ 
	if(count <= 5){ 
		message = message + input;
	}
	else{ 
		message = message.substring(0, message.length - input.length); 
	} 
	count++; 
	message; 
 }
)

Could anybody please explain to me, why this doesn’t work with Nightbot? Is a Nightbot command only able to return one line, therefor only giving me the first input?

A bit more info would be nice, what are you trying to do?
It’s not giving you the first input, but the last, after the while completes (and the old $message is overwritten). You can append that message part to another var if you want to output it in one string.

1 Like

@seliasbane

Nightbot only returns the result of the last evaluated variable/method within $(eval) which means, like xgerhard said, it will only return the value of message from when it was last overwritten (from the last iteration of the while loop).


As also previously mentioned, you should therefore append message to another variable for each iteration of the while loop, then return the value of this other variable after the loop ends. This should do it:

$(eval message=` `;input=decodeURIComponent(`$(querystring)`);count=1;newMessage=``;while(count<10){if(count<=5){message+=input;}else{message=message.substring(0,message.length-input.length);}count++;newMessage+=message;})
2 Likes

That’s sad, I was trying to let Nightbot build a simple pyramid, so if you use

!mycmd Kappa

it would output this:

Kappa 
Kappa Kappa 
Kappa Kappa Kappa 
Kappa Kappa Kappa Kappa 
Kappa Kappa Kappa Kappa Kappa 
Kappa Kappa Kappa Kappa 
Kappa Kappa Kappa 
Kappa Kappa 
Kappa

You could actually do that in JS by appending a new line (\n) to newMessage in each iteration of the while loop, but Twitch and YouTube chat don’t support line breaks so the $(eval) output would instead look like a rectangular wall of spam.

1 Like

I have done something similar with urlfetch and the ‘Nightbot-Response-Url’ that Nightbot provides.

The !pyramid command calls a python script in a flask backend, that catches the Nightbot-Response-Url and then posts 5 messages to the Url to create the pyramid.

However, the limitations of the channel/send API (only 5 responses and 5 seconds between each of them) makes the Nightbot pyramids comically slow.

pyramid

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