dynamic api fetch with a default response

hello! i am trying to make a request to an api based on a given input from a user & also include a default response if no input is given. here is how far i was able to get, it only returns the json object for testing/simplicity for now but i do have an actual formatted response for once this is functional.

$(eval r=(`$(1)` == `null`) ? $(urlfetch json https://mcsrranked.com/api/users/nealxm) : $(urlfetch json https://mcsrranked.com/api/users/$(1)); r)

when run with an input (!command feinberg) i get an appropriate response [object Object] . my trouble comes when i run this with no input (!command) i get the response Unexpected identifier.

i started troubleshooting by trying to simplify command by removing the falsy section of the ternary:

$(eval r=(`$(1)` == `null`) ? $(urlfetch json https://mcsrranked.com/api/users/nealxm) : "some input given"; r)

and this gave expected responses. running !command returns [object Object] and running !command feinberg returns some input given. both to be expected.

this result was confusing since i don’t understand what would cause the truthy section of the ternary to only work sometimes. is anyone able to help me see what i am missing here? thanks for your time

Hey @nealxm!

Welp, I spent 20 minutes trying to debug it, but I can’t tell you why it doesn’t work either, it’s surprising.
There’s a solution tho’: using an alias command…

!addcom !rank -a=_rank $(eval '$(1)' === 'null' ? 'nealxm' : '$(1)')

!addcom _rank $(eval JSON.parse('$(urlfetch json https://mcsrranked.com/api/users/$(query))').data.nickname)

The response containing comments, we have to use JSON.parse() to filter these out, otherwise it breaks the code; I don’t know why their dev(s) thought it’d be a good idea to include comments in a supposed-to-be JSON response, that’s not good practice, if they want to store that data somewhere they should use keys in the JSON response.

Using an alias command has the advantage of reducing the number of API calls too, which should make the command more responsive.

hi @Emily thanks so much for trying!! this two command system is actually very similar to what i have currently, was just trying to clean things up. i would use this parse method but i need to access many different endpoints so here was my solution for the formatted response:

$(eval r=$(urlfetch json https://mcsrranked.com/api/users/$(1)); `stats for ${r['data']['nickname']} - elo: ${r['data']['elo_rate']} | rank: ${r['data']['elo_rank']} | record: ${r['data']['records']['2']['win']} W - ${r['data']['records']['2']['lose']} L`)

i agree it would be nice to only write the api once but if the commands in my original post worked. the api would only be called once, even though it is written twice.

thanks again so much for trying!! let me know if you think of something else that could help

You can still use the JSON.parse() method… it was just a simple example from which you could expand. Here’s how to write the second command (_rank) with all the keys you want to use:

!addcom _rank $(eval r = JSON.parse('$(urlfetch json https://mcsrranked.com/api/users/$(query))').data; `stats for ${r.nickname} - elo: ${r.elo_rate} | rank: ${r.elo_rank} | record: ${r.records['2'].win} W - ${r.records['2'].lose} L`;)

No, Nightbot replaces every $(variable) with their content before executing the JavaScript code, so the command in your first post calls the API twice and then tries to execute the code’s logic.

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