NightDev Community Forums

Issue running nightbot command get API

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=$(query)); 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?

Hey @mastermick888!

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.

@EmilyPerrimon 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 @xgerhard 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})

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