fix: getblitzaccount now uses getwargamingresponse

This commit is contained in:
TresAbhi committed 2023-04-27 00:27:40 +00:00
1 parent 859efd1f02
commit 799afe3de9
2 files changed
+16 -20

No files matched your search

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