From ab88be7c948700e956a68a310c1eeafac7db4191 Mon Sep 17 00:00:00 2001 From: tresabhi Date: Mon, 24 Apr 2023 00:42:37 +0000 Subject: [PATCH] feat: better typing --- src/commands/eligible.ts | 2 ++ src/commands/inactive.ts | 12 ++++++------ src/commands/stats.ts | 2 ++ src/commands/verify.ts | 2 ++ src/types/accountList.ts | 12 ------------ src/types/{clanInfo.ts => clanDetails.ts} | 4 +++- src/types/{accountInfo.ts => playerPersonalData.ts} | 4 +++- src/types/players.ts | 10 ++++++++++ src/types/wargamingResponse.ts | 3 +++ src/utilities/getBlitzAccount.ts | 12 ++++++++---- 10 files changed, 39 insertions(+), 24 deletions(-) delete mode 100644 src/types/accountList.ts rename src/types/{clanInfo.ts => clanDetails.ts} (86%) rename src/types/{accountInfo.ts => playerPersonalData.ts} (92%) create mode 100644 src/types/players.ts create mode 100644 src/types/wargamingResponse.ts diff --git a/src/commands/eligible.ts b/src/commands/eligible.ts index 0d9cd5860..a5d4e84e7 100644 --- a/src/commands/eligible.ts +++ b/src/commands/eligible.ts @@ -11,6 +11,8 @@ import getBlitzAccount from '../utilities/getBlitzAccount.js'; import getBlitzStarsAccount from '../utilities/getBlitzStarsAccount.js'; import poweredByBlitzStars from '../utilities/poweredByBlitzStars.js'; +export const inDev = true; + export const CLANS = { sklld: { name: 'Skilled', diff --git a/src/commands/inactive.ts b/src/commands/inactive.ts index 615bb8b53..da839ffc4 100644 --- a/src/commands/inactive.ts +++ b/src/commands/inactive.ts @@ -6,14 +6,14 @@ import { } from 'discord.js'; import fetch from 'node-fetch'; import { NEGATIVE_COLOR, POSITIVE_COLOR } from '../constants/colors.js'; -import { AccountInfo } from '../types/accountInfo.js'; -import { ClanInfo } from '../types/clanInfo.js'; +import { ClanDetails } from '../types/clanDetails.js'; +import { PlayerPersonalData } from '../types/playerPersonalData.js'; import { CLANS } from './eligible.js'; -const DEFAULT_THRESHOLD = 7; - export const inDev = true; +const DEFAULT_THRESHOLD = 7; + export async function execute( interaction: ChatInputCommandInteraction, ) { @@ -25,7 +25,7 @@ export async function execute( await fetch( `https://api.wotblitz.com/wotb/clans/info/?application_id=${process.env.WARGAMING_APPLICATION_ID}&clan_id=${CLANS[clanName].id}`, ) - ).json()) as ClanInfo; + ).json()) as ClanDetails; const clan = clanData.data[CLANS[clanName].id]; const memberIds = clan.members_ids; const accountInfo = (await ( @@ -34,7 +34,7 @@ export async function execute( process.env.WARGAMING_APPLICATION_ID }&account_id=${memberIds.join(',')}`, ) - ).json()) as AccountInfo; + ).json()) as PlayerPersonalData; const inactiveInfo = memberIds .map((memberId) => { diff --git a/src/commands/stats.ts b/src/commands/stats.ts index 6e564dc15..7e8a3009d 100644 --- a/src/commands/stats.ts +++ b/src/commands/stats.ts @@ -11,6 +11,8 @@ import getBlitzAccount from '../utilities/getBlitzAccount.js'; import getBlitzStarsAccount from '../utilities/getBlitzStarsAccount.js'; import poweredByBlitzStars from '../utilities/poweredByBlitzStars.js'; +export const inDev = true; + export async function execute( interaction: ChatInputCommandInteraction, ) { diff --git a/src/commands/verify.ts b/src/commands/verify.ts index 068ac4caf..046729c03 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -10,6 +10,8 @@ import { NEGATIVE_COLOR, POSITIVE_COLOR } from '../constants/colors.js'; import { SERVERS } from '../constants/servers.js'; import getBlitzAccount from '../utilities/getBlitzAccount.js'; +export const inDev = true; + export async function execute( interaction: ChatInputCommandInteraction, ) { diff --git a/src/types/accountList.ts b/src/types/accountList.ts deleted file mode 100644 index d2fbdb563..000000000 --- a/src/types/accountList.ts +++ /dev/null @@ -1,12 +0,0 @@ -export interface Account { - nickname: string; - account_id: number; -} - -type AccountList = - | { - data?: Account[]; - } - | undefined; - -export default AccountList; diff --git a/src/types/clanInfo.ts b/src/types/clanDetails.ts similarity index 86% rename from src/types/clanInfo.ts rename to src/types/clanDetails.ts index ea798643e..0154c3441 100644 --- a/src/types/clanInfo.ts +++ b/src/types/clanDetails.ts @@ -1,4 +1,6 @@ -export interface ClanInfo { +import { WargamingResponse } from './wargamingResponse.js'; + +export interface ClanDetails extends WargamingResponse { data: { [key: number]: { recruiting_options: { diff --git a/src/types/accountInfo.ts b/src/types/playerPersonalData.ts similarity index 92% rename from src/types/accountInfo.ts rename to src/types/playerPersonalData.ts index 5c25b1c1b..7792be55d 100644 --- a/src/types/accountInfo.ts +++ b/src/types/playerPersonalData.ts @@ -1,4 +1,6 @@ -export interface AccountInfo { +import { WargamingResponse } from './wargamingResponse.js'; + +export interface PlayerPersonalData extends WargamingResponse { data: { [key: number]: { statistics: { diff --git a/src/types/players.ts b/src/types/players.ts new file mode 100644 index 000000000..b5310df65 --- /dev/null +++ b/src/types/players.ts @@ -0,0 +1,10 @@ +import { WargamingResponse } from './wargamingResponse.js'; + +export interface Player { + nickname: string; + account_id: number; +} + +export interface Players extends WargamingResponse { + data: Player[]; +} diff --git a/src/types/wargamingResponse.ts b/src/types/wargamingResponse.ts new file mode 100644 index 000000000..e23e5f6d6 --- /dev/null +++ b/src/types/wargamingResponse.ts @@ -0,0 +1,3 @@ +export interface WargamingResponse { + status: string; +} diff --git a/src/utilities/getBlitzAccount.ts b/src/utilities/getBlitzAccount.ts index fb9842d86..753b87039 100644 --- a/src/utilities/getBlitzAccount.ts +++ b/src/utilities/getBlitzAccount.ts @@ -6,20 +6,24 @@ import { import fetch from 'node-fetch'; import { NEGATIVE_COLOR } from '../constants/colors.js'; import { SERVERS } from '../constants/servers.js'; -import AccountList, { Account } from '../types/accountList.js'; +import { Player, Players } from '../types/players.js'; export default async function getBlitzAccount( interaction: ChatInputCommandInteraction, ign: string, server: keyof typeof SERVERS, - callback: (account: Account) => void, + callback: (account: Player) => void, ) { const serverName = SERVERS[server]; const players = (await fetch( `https://api.wotblitz.${server}/wotb/account/list/?application_id=${process.env.WARGAMING_APPLICATION_ID}&search=${ign}`, - ).then((response) => response.json())) as AccountList; + ).then((response) => response.json())) as Players; - if (players?.data && players.data[0]?.nickname === ign) { + if ( + players.status === 'ok' && + players.data.length > 0 && + players.data[0].nickname === ign + ) { callback(players.data[0]); } else { // no exact match