diff --git a/src/types/players.ts b/src/types/players.ts index a2038bef3..abbcaa2b8 100644 --- a/src/types/players.ts +++ b/src/types/players.ts @@ -1,8 +1,6 @@ -import { WargamingResponse } from './wargamingResponse.js'; - export interface Player { nickname: string; account_id: number; } -export type Players = WargamingResponse; +export type Players = Player[]; diff --git a/src/utilities/getBlitzAccount.ts b/src/utilities/getBlitzAccount.ts index e80b2ae34..5eecdfd23 100644 --- a/src/utilities/getBlitzAccount.ts +++ b/src/utilities/getBlitzAccount.ts @@ -3,11 +3,10 @@ import { ChatInputCommandInteraction, EmbedBuilder, } from 'discord.js'; -import fetch from 'node-fetch'; import { NEGATIVE_COLOR } from '../constants/colors.js'; import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js'; import { Player, Players } from '../types/players.js'; -import wargamingError from './wargamingError.js'; +import getWargamingResponse from './getWargamingResponse.js'; export default async function getBlitzAccount( interaction: ChatInputCommandInteraction, @@ -16,41 +15,40 @@ export default async function getBlitzAccount( callback: (account: Player) => void, ) { const serverName = BLITZ_SERVERS[server]; - const players = (await fetch( + + getWargamingResponse( `https://api.wotblitz.${server}/wotb/account/list/?application_id=${process.env.WARGAMING_APPLICATION_ID}&search=${name}`, - ).then((response) => response.json())) as Players; + interaction, + async (players) => { + if (players.length > 0 && players[0].nickname === name) { + callback(players[0]); + } else { + // no exact match + await interaction.reply({ + embeds: [ + new EmbedBuilder() + .setColor(NEGATIVE_COLOR) + .setTitle(`Account not found`) + .setDescription( + `I couldn't find "${name}" in the ${serverName} server. I found ${ + players + ? players.length < 100 + ? players.length + : 'over 100' + : 0 + } similarly spelled account${ + players?.length !== 1 ? 's' : '' + }. ${ + players && players.length > 0 + ? `Did you mean "${players[0].nickname}"? ` + : '' + }Re-run the command, don't make typos, and capitalize correctly.`, + ), + ], + }); - if (players.status === 'ok') { - if (players.data.length > 0 && players.data[0].nickname === name) { - callback(players.data[0]); - } else { - // no exact match - await interaction.reply({ - embeds: [ - new EmbedBuilder() - .setColor(NEGATIVE_COLOR) - .setTitle(`Account not found`) - .setDescription( - `I couldn't find "${name}" in the ${serverName} server. I found ${ - players?.data - ? players.data.length < 100 - ? players.data.length - : 'over 100' - : 0 - } similarly spelled account${ - players?.data?.length !== 1 ? 's' : '' - }. ${ - players?.data && players.data.length > 0 - ? `Did you mean "${players.data[0].nickname}"? ` - : '' - }Re-run the command, don't make typos, and capitalize correctly.`, - ), - ], - }); - - console.log(`Account not found for ${name} in ${serverName} server.`); - } - } else { - wargamingError(interaction); - } + console.log(`Account not found for ${name} in ${serverName} server.`); + } + }, + ); }