A AWS Lambda powered Discord bot which tracks players on the leaderboard. With AWS CloudWatch this function can be configured to repeatedly (e.g. every 4 hours) fetch rift leaderboards and track your "Heroes" positions.
E.g.:
Leaderboard stats from Mon, 28 Jan 2019 09:36:17 GMT
Leaderboard for rift barbarian:
* Anon#1234 at rank 140
* Anon#1244 not in list
NOTE: At the moment the leadboard is only searched by players' BattleTags. However it is possible to have multiple "Heros" in one rift leaderboard with the same BattleTags. This would cause only the first "Hero" to be found.
Since this function requests the Battle.net API the runtime highly depends on the response time of the API. You should definitely set a timout for your Lambda function (e.g. 3 seconds).
To be precise: 1 request is made to obtain an access_token
. Additionally for each rift another request is made.
The response JSON has a size of about ~500kb per leaderboard and needs to be parsed internally.
Monitoring stats using 1 leaderboard for 1 player:
- execution time: ~1500 ms
- memory usage: ~50 MB
- Follow the guide at https://develop.battle.net/documentation/guides/getting-started to create a Battle.net API Client.
- Obtain the Client ID and Client Secret from your newly created client
- Create a Webbhook for a channel in your Discord server
- Edit a channel you wish to be notified in
- Create a new "Webhook"
- Copy the URL
- Create a new Lambda function either through the AWS Console or using the AWS CLI:
$ aws cli lambda create-function \ --function-name YOUR-FUNCTION-NAME \ --runtime nodejs8.10 \ --role YOUR-ROLE \ --handler index.trackLeaderboard \ --environment 'Variables={BATTLENET_CLIENT_ID=<YOUR-CLIENT-ID>,BATTLENET_CLIENT_SECRET=<YOUR-CLIENT-SECRET>,DISCORD_BOT_WEBHOOK_URL=<YOUR-WEBHOOK-URL>}'
- Archive the lambda function and publish it:
$ zip pkg.zip index.js $ aws lambda update-function-code \ --function-name YOUR-FUNCTION-NAME \ --zip-file pkg.zip
- Create a CloudWatch Event Rule, add the lambda function as target and select "Constant (JSON text)" under "Configure input":
WARNING: Please note that Battle.net API Clients are limited to 36,000 requests per hour at a rate of 100 requests per second (see Throttling).
{ "rifts": { "barbarian": [ "Anon#1234" ] } }
The lambda function uses the following environment variables which can be set in the "Environment variables" section:
BATTLENET_CLIENT_ID
Battle.net API Client IDBATTLENET_CLIENT_SECRET
Battle.net API Client IDBATTLENET_REGION
Battle.net region (default:eu
)BATTLENET_SEASON
Current season to track (default:16
)DISCORD_BOT_WEBHOOK_URL
The Discord Webhook URL to post leaderboard messages toDISCORD_BOT_NAME
Name of the Discord bot, displayed in the channel (default:Leaderboard BOT
)
The lambda function accepts the following input from an event payload:
interface payload {
rifts: { [riftName: string]: string[] }
}
For each riftName
all given player BattleTags will be searched in the rift leaderboard.
E.g:
{
"rifts": {
"barbarian": [
"Anon#1234",
"Anon#4321"
],
"wizard": [
"Anon#1237"
]
}
}