Using Nightbot command to connect to aoe4 leaderboard api

Asked Nov 12, 2021·3 replies·Last activity 4 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.

Hello, i am trying to use urlfetch to retrieve information from:

https://api.ageofempires.com/api/ageiv/Leaderboard

but need to use this body with the header json as well
{
"region": 7,
"versus": "players",
"matchType": "unranked",
"teamSize": "1v1",
"searchPlayer": "AndyCiv",
"page": 1,
"count": 100
}

i have run it through Postman using a POST and get the following:

{
"count": 1,
"items": [
{
"gameId": "4",
"userId": "/steam/76561199151723601",
"rlUserId": 7040009,
"userName": "AndyCiv",
"avatarUrl": null,
"playerNumber": null,
"elo": 953,
"eloRating": 0,
"rank": 18626,
"region": "0",
"wins": 45,
"winPercent": 50.56,
"losses": 44,
"winStreak": 2
}
]
}

When i try to add this as a command through nightbot its returns unexpected identifier or error 405, could i get some help with how to add this please? the command would be "!rank username".

3 replies

Hey @Plucky Gorilla!

I would have advised using the fetch Web API, but it only works in browsers:

fetch('https://api.ageofempires.com/api/ageiv/Leaderboard', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: {
'region': 7,
'versus': 'players',
'matchType': 'unranked',
'teamSize': '1v1',
'searchPlayer': 'AndyCiv',
'page': 1,
'count': 100
}
});

I'm afraid there won't be a solution with Nightbot alone, you'll likely need to create your own API to fetch the data, and then call that one with Nightbot's $(urlfetch).

Hi Emily,

Ok then not to worry, I did see someone else has already incorporated it into their own twitch/obs plugin called castmate, I was just hoping there would be a more direct approach through nightbot. Thanks again for your speedy reply.

just as an update for anyone interested, someone has been able to make it work using another site the command is as follows:

!addcom !rank $(eval a='$(querystring)'!=''?$(urlfetch https://aoeiv.net/api/leaderboard?game=aoe4&leaderboard_id=17&search=$(querys…)&count=1):$(urlfetch https://aoeiv.net/api/leaderboard?game=aoe4&leaderboard_id=17&search=aussie_d…);try{b=a.leaderboard[0];w=b["wins"];t=b["games"];'"'+b["name"]+'" is rank: '+b["rank"]+", streak: "+b["streak"]+', total games: '+t+', w-l: '+w+'-'+b["losses"]+', '+~~(100*w/t)+'%'}catch{'No player found NotLikeThis'})