Sends a Discord embed for a given gamertag's KDR on Halo Infinite. Usage: !halokdr [gamertag]
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let gamertag = context.params.event.content.replace("!halokdr ", "");
let haloData
try {
haloData = await lib.halo.infinite['@0.2.3'].stats['service-record']({
gamertag: gamertag,
experience: 'pvp-only'
});
} catch (e) {
return await lib.discord.channels['@0.2.0'].messages.create({
"channel_id": `${context.params.event.channel_id}`,
"content": "",
"tts": false,
"embeds": [
{
"type": "rich",
"title": `❌ Oh no!`,
"description": `Hey, dont be silly now. Come on, I can't search a non-existing account. Try again!`,
"color": 0xff0000
}
]
});
}
// If user hasn't played Halo Infinite yet, return an error
if (haloData.data.matches_played <= 10) {
return await lib.discord.channels['@0.2.0'].messages.create({
"channel_id": `${context.params.event.channel_id}`,
"content": "",
"tts": false,
"embeds": [
{
"type": "rich",
"title": `❌ Oh no!`,
"description": `You need to have played at least ten games for us to calculate your KDR!`,
"color": 0xff0000
}
]
});
}
// round KDR
let kdr = Math.round(haloData.data.core.kdr * 100) / 100
return await lib.discord.channels['@0.2.0'].messages.create({
"channel_id": `${context.params.event.channel_id}`,
"content": "",
"tts": false,
"embeds": [
{
"type": "rich",
"title": `${kdr >= 1 ? "🎉" : "😭"} ${haloData.additional.gamertag}'s KDR is ${kdr}`,
"description": "",
"color": kdr >= 1 ? 0x03cc00 : 0xcc0000
}
]
});