Using variables I define in a urlfetch in eval?

Asked Jan 4, 2021·2 replies·Last activity 5 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

This seems like it should be easy, but has anyone used variables they define as part of the URL in urlfetch? I can't seem to get it to work.
Something like:
$(eval
const topic="12";
response = $(urlfetch json https://opentdb.com/api.php?amount=1&category=$(topic)&difficulty=easy&type=multiple&encode=url3986);
...
)

I also tried putting an eval nested in the urlfetch, but that doesn't seem to embed the string.
$(eval response =$(urlfetch json $(eval topic = '$(1)'; my_url = "https://opentdb.com/api.php?amount=1&category="+topic+"&difficulty=easy&type=multiple&encode=url3986"; my_url)); s = JSON.stringify(response); )

Is the problem here that I don't have access to $(1) from the embedded eval?

I've tried many different methods to access 'topic', but none seem to work. Help?

2 replies

Nightbot variables evaluate inner to outer and variable nesting can't exceed 2 levels, so in example 1 the urlfetch is evaluated and stored as a JS string before the eval runs the JS and in example 2 the JS string '$(1)' is parsed exactly as is.

I'd edit the command to output a URL then set the command an alias of another command that runs a urlfetch on the input.

That approach worked! Here are some details.

I have a command to fetch the content that takes a string as input.

Then, I have another command (which is what the user will call) which formats the string and calls the first command via an Alias.

Thank you @Deft Penguin !

image.pngimage.png