NightDev Community Forums

How to make Nightbot evaluate external JS code, with $query as a parameter?

Hello there.
I’m trying to add a !test command who can be able to do this:when someone will declare it with !test [number] , that number will be taken as a parameter to execute an external JS function, and, after being processed, will return the result.
So, basically I’m looking for a kind of $(eval [url].function($(query))) sort of thing, I guess… But I need to pass $(query) as a parameter…
Any ideas on how could I do this?
Thank you in advance!

@Cicco23

Executing external JS code

Store a snippet of JS code in a Pastebin file (https://pastebin.com). The following command setup will then grab the code from Pastebin and execute it.

$(eval $(urlfetch json https://pastebin.com/raw/FOOrBARj))

Working with user input

Nightbot variables pulled via $(urlfetch) do not evaluate to the intended values. Instead use the following setup.

$(eval q=decodeURIComponent(`$(querystring)`);$(urlfetch json https://pastebin.com/raw/FOOrBARj))

The user input will be stored in the string variable “q”. Use this variable when writing your JS code snippet on Pastebin.

1 Like

Thanks for the reply…
But it still gives me the same error… “Right-hand side of ‘instanceof’ is not an object”
What does that mean?

Either what you wrote on Pastebin is not valid JS code or Nightbot is having some trouble reading your Pastebin file.

1 Like

!editcom !nbgame $(eval q=decodeURIComponent($(querystring)); $(urlfetch json https://pastebin.com/NqgHn52M))
Is something wrong with it?

Could you explain what the nbgame function is supposed to do in your JS snippet?

Make sure it outputs something.The function is never called in your example.
And like RokettoJanpu mentioned you can now use the var q inside your code.

function nbgame(s){
    return "Hello, world!";
}
nbgame(q)

Well, for now I’ve left it into the basics, because I wanted to figure out how to connect NB with external JS.
My final scope is to create a game that involves random numbers, and the user plays against Nightbot :slight_smile: :v:

In the end, I’ve made it!! ^-^ Thanks for the tips… But, in the end, I’ve done this:
NIGHTBOT:
!editcom !nbgame $(user) -> $(eval q="$(querystring)";$(urlfetch json https://pastebin.com/raw/NqgHn52M))
JS:
if(isNaN(q)) “ERROR!!!”;
else "Here is your number: "+q;

Thank you for the help! ^-^ :v:

P.S.: ALSO, I’ve realized that I can’t make comments in JS, like this:
// This is a comment
, because it seems that Nightbot doesn’t support line break and new lines (like \n), sticking the code together…
So it’s better to close the comment like this instead:
/* This is a comment */

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