NightDev Community Forums

JS variables in urlfetch

So I am writing a command for fetching some osu! player stats using the urlfetch. And I’d like to specify for which game mode I need the data. And I would like to do it with words and not numbers(and that’s how API accepts it, via the numbers). I can do the “converting words to corresponding numbers” part(but without switch case it feels wrong) but I don’t know how to add those numbers to url in urlfetch. Can someone help me with that?

@Kriper1111

You probably need to nest an $(eval) inside a $(urlfetch) if you want to edit the API URL before calling it.

$(urlfetch json $(eval // Some logic here to edit how the API URL looks like...)) 

But you probably will use a $(query) inside $(eval) so that you can edit the URL based on user input, and because the API response is probably a JSON payload you may want to parse the $(urlfetch) result with another $(eval)…

$(eval const api = JSON.parse("$(urlfetch json $(eval var input = "$(query)"; // Some logic here to edit how the API URL looks like...))"); // Some logic here to parse the API response...)

But this won’t work because Nightbot doesn’t allow for more than 2 levels of variable nesting. So I would set up two commands and set one as an alias of the other. For example, an !osu command that is an alias of _!osu

!addcom -cd=5 !osu -a=_!osu $(urlfetch json $(eval var input = "$(query)"; // Some logic here to edit how the API URL looks like...))

!addcom -cd=5 _!osu $(eval const api = JSON.parse("$(query)"); // Some logic here to parse the API response...)

Thanks. I should have understood earlier that query for aliased command is the message from “main”. Because I did set up an alias for that command when it was only for one mode. (but unfortunately I can’t use this alias anymore, because it’s double alias.) maybe its not late to switch to the streamlabs chatbot And for someone who will encounter same(or similar trouble) in the future, you can check for API’s respond to null $(query) if you want to make a “Query is empty” response.

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