Using Nightbot command to connect to aoe4 leaderboard api

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”.

Hey @andy_s_89!

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).

1 Like

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.

1 Like

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=$(querystring)&count=1):$(urlfetch https://aoeiv.net/api/leaderboard?game=aoe4&leaderboard_id=17&search=aussie_drongo&count=1);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’})

1 Like

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