Issue running nightbot command get API

Asked Sep 12, 2020·4 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.

Hi,

I am having issues running the following command to my twitch nightbot.

$(eval a = $(urlfetch json https://cnc.community/api/leaderboard/tiberian-dawn/players/search?search=$(q…)); js = JSON.parse(a); Player Name: ${js.player_name} | Rank: ${js.rank} | Wins: ${js.wins} | Losses: ${js.losses} | Points: ${js.points})

Result is

Player Name: undefined | Rank: undefined | Wins: undefined | Losses: undefined | Points: undefined

Why is this undefined?

4 replies

Hey @Sandy Shark!

Can you give us your username in the game so we can have a look at what the API looks like.
We can't be of much help otherwise.

@Polar Cat sorry i forgot to add that, try user WarMachine which should return


[{"id":116224,"player_name":"WarMachine","rank":37,"wins":177,"losses":89,"points":1400.6}]

Hiya. The API returns an array, with possibly multiple players.. but js[0].player_name should work.

Thanks @Gerhard this seems to work, I totally forgot that it returns a list object. So if I wanted to iterate through the results and display ALL, what is the nightbot command to do a foreach loop?

Eg.

$(eval a = $(urlfetch json https://cnc.community/api/leaderboard/tiberian-dawn/players/search?search=$(query)); js = JSON.parse(a); for i in js: Player Name: ${i.player_name} | Rank: ${i.rank} | Wins: ${i.wins} | Losses: ${i.losses} | Points: ${i.points})