Using eval and urlfetch to print result from OWAPI

I want to retrieve the competitive rank using the Overwatch API.

This is how I’m editing the command:

!editcom !rank $(eval response =$(urlfetch https://owapi.net/api/v3/u/Dad-12262/stats); response.us.stats.competitive.overall_stats.comprank)

For some reason, I keep getting the following response from NightBot when I try the command.

Nightbot: Cannot read property 'stats' of undefined

When I navigate to https://owapi.net/api/v3/u/Dad-12262/stats in my browser, I correctly get data in the .us attribute of the object, so that’s why I’m confused about the error message from the Nightbot.

Can anyone let me know what I’m doing wrong?

The problem is that $(urlfetch https://owapi.net/api/v3/u/Dad-12262/stats) is being replaced with [Response must be less than 400 characters]. This is because Nightbot doesn’t allow the standard $(urlfetch ...) to fetch more than 400 characters. You can, however, use $(urlfetch json ...) to fetch any amount of characters.

It is also good practice, but not necessary, to use JSON.parse on a JSON string rather than inserting JSON directly into your JavaScript code.

Also, I visited that URL, and us.stats.competitive.overall_stats.comprank was null, so I made the command check for that and say “None” instead of “null”.

Fixed version:

!editcom !rank $(eval response = `$(urlfetch json https://owapi.net/api/v3/u/Dad-12262/stats)`; try {json = JSON.parse(response); compRank = json.us.stats.competitive.overall_stats.comprank; compRank == null ? "None" : compRank} catch(e){`${e}: ${response}`.substr(0, 400)})

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