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
+36 -40

No files matched your search

+1 -3
View File
@@ -1,8 +1,6 @@
import { WargamingResponse } from './wargamingResponse.js';
export interface Player {
nickname: string;
account_id: number;
}
export type Players = WargamingResponse<Player[]>;
export type Players = Player[];
+35 -37
View File
@@ -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<CacheType>,
@@ -16,41 +15,40 @@ export default async function getBlitzAccount(
callback: (account: Player) => void,
) {
const serverName = BLITZ_SERVERS[server];
const players = (await fetch(
getWargamingResponse<Players>(
`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.`);
}
},
);
}