Files
blitzkit/src/utilities/getBlitzStarsAccount.ts
T
2023-04-27 01:08:19 +00:00

43 lines
1.1 KiB
TypeScript

import {
CacheType,
ChatInputCommandInteraction,
EmbedBuilder,
} from 'discord.js';
import fetch from 'node-fetch';
import { NEGATIVE_COLOR } from '../constants/colors.js';
import { PlayerStatistics } from '../types/statistics.js';
export default async function getBlitzStarsAccount(
interaction: ChatInputCommandInteraction<CacheType>,
accountId: number,
name: string,
callback: (account: PlayerStatistics) => void,
) {
await interaction.deferReply();
async function notTracked() {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setTitle('No data to display')
.setDescription(`${name} is not tracked by BlitzStars.`)
.setColor(NEGATIVE_COLOR),
],
});
console.log(`${name} is not tracked by BlitzStars.`);
}
fetch(`https://www.blitzstars.com/api/top/player/${accountId}`)
.then(async (response) => {
const data = (await response.json()) as PlayerStatistics;
if (data.statistics) {
callback(data);
} else {
notTracked();
}
})
.catch(notTracked);
}