mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 11:39:50 +02:00
feat: bug fixes and error safety
This commit is contained in:
1 parent
0b6b2ecedc
commit
780d9d5f8e
25 files changed
+136
-182
No files matched your search
@@ -13,17 +13,23 @@ export default async function usernameAutocomplete(
|
||||
if (focusedOption.name !== 'username') return;
|
||||
const players = await listAccountsPanServer(focusedOption.value);
|
||||
|
||||
await interaction.respond(
|
||||
players
|
||||
? players.map(
|
||||
(player) =>
|
||||
({
|
||||
name: `${player.nickname} (${BLITZ_SERVERS[player.server]})`,
|
||||
value: `${player.server}/${player.account_id}`,
|
||||
} satisfies ApplicationCommandOptionChoiceData<string>),
|
||||
)
|
||||
: [],
|
||||
);
|
||||
try {
|
||||
await interaction.respond(
|
||||
players
|
||||
? players.map(
|
||||
(player) =>
|
||||
({
|
||||
name: `${player.nickname} (${BLITZ_SERVERS[player.server]})`,
|
||||
value: `${player.server}/${player.account_id}`,
|
||||
} satisfies ApplicationCommandOptionChoiceData<string>),
|
||||
)
|
||||
: [],
|
||||
);
|
||||
|
||||
console.log(`Username autocomplete for ${focusedOption.value}`);
|
||||
console.log(`Username autocomplete for ${focusedOption.value}`);
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`Failed to autocomplete username for ${focusedOption.value} in time`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
|
||||
export default async function getBlitzAccount(
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
username: string,
|
||||
): Promise<{ server: BlitzServer; id: number } | null> {
|
||||
) {
|
||||
if (serverAndIdPattern.test(username)) {
|
||||
const [server, accountId] = username.split('/');
|
||||
return { server: server as BlitzServer, id: Number(accountId) };
|
||||
@@ -27,7 +27,7 @@ export default async function getBlitzAccount(
|
||||
],
|
||||
});
|
||||
|
||||
return null;
|
||||
throw new Error(`Could not find user "${username}"`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import listClansPanServer from './listClansPanServer.js';
|
||||
export default async function getBlitzClan(
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
clan: string,
|
||||
): Promise<{ server: BlitzServer; id: number } | null> {
|
||||
) {
|
||||
if (serverAndIdPattern.test(clan)) {
|
||||
const [server, accountId] = clan.split('/');
|
||||
return { server: server as BlitzServer, id: Number(accountId) };
|
||||
@@ -20,13 +20,13 @@ export default async function getBlitzClan(
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
errorEmbed(
|
||||
'Could not find user',
|
||||
'Could not find clan',
|
||||
`I couldn't find clan \`${clan}\`. Try selecting a username from the search result.`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
return null;
|
||||
throw new Error(`Could not find clan "${clan}"`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,8 @@ export default async function getWargamingResponse<Data extends object>(
|
||||
if (parsed.status === 'ok') {
|
||||
return parsed.data;
|
||||
} else {
|
||||
console.error(
|
||||
`Wargaming response status ${parsed.status} ${parsed.error.message}`,
|
||||
throw new Error(
|
||||
`Wargaming response error status:"${parsed.status}" message: "${parsed.error.message}" url: "${url}"`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -8,50 +8,47 @@ export type AccountListWithServer = (Account & {
|
||||
})[];
|
||||
|
||||
export default async function listAccountsPanServer(search: string, limit = 9) {
|
||||
const trimmed = search.trim();
|
||||
if (trimmed.length < 3 && trimmed.length > 100) return [];
|
||||
const normalizedSearch = encodeURIComponent(search.trim());
|
||||
const normalizedLimit = Math.round(limit / 3);
|
||||
|
||||
return (
|
||||
await Promise.all([
|
||||
getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.com/wotb/account/list/?application_id=${
|
||||
args['wargaming-application-id']
|
||||
}&search=${encodeURIComponent(search)}&limit=${normalizedLimit}`,
|
||||
).then(
|
||||
(value) =>
|
||||
value &&
|
||||
value.map((account) => ({
|
||||
...account,
|
||||
server: 'com' as BlitzServer,
|
||||
})),
|
||||
),
|
||||
getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.eu/wotb/account/list/?application_id=${
|
||||
args['wargaming-application-id']
|
||||
}&search=${encodeURIComponent(search)}&limit=${normalizedLimit}`,
|
||||
).then(
|
||||
(value) =>
|
||||
value &&
|
||||
value.map((account) => ({
|
||||
...account,
|
||||
server: 'eu' as BlitzServer,
|
||||
})),
|
||||
),
|
||||
getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.asia/wotb/account/list/?application_id=${
|
||||
args['wargaming-application-id']
|
||||
}&search=${encodeURIComponent(search)}&limit=${normalizedLimit}`,
|
||||
).then(
|
||||
(value) =>
|
||||
value &&
|
||||
value.map((account) => ({
|
||||
...account,
|
||||
server: 'asia' as BlitzServer,
|
||||
})),
|
||||
),
|
||||
])
|
||||
)
|
||||
.filter(Boolean)
|
||||
.flat();
|
||||
if (normalizedSearch.length >= 3 && normalizedSearch.length <= 100) {
|
||||
return (
|
||||
await Promise.all([
|
||||
getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.com/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then(
|
||||
(value) =>
|
||||
value &&
|
||||
value.map((account) => ({
|
||||
...account,
|
||||
server: 'com' as BlitzServer,
|
||||
})),
|
||||
),
|
||||
getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.eu/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then(
|
||||
(value) =>
|
||||
value &&
|
||||
value.map((account) => ({
|
||||
...account,
|
||||
server: 'eu' as BlitzServer,
|
||||
})),
|
||||
),
|
||||
getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.asia/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then(
|
||||
(value) =>
|
||||
value &&
|
||||
value.map((account) => ({
|
||||
...account,
|
||||
server: 'asia' as BlitzServer,
|
||||
})),
|
||||
),
|
||||
])
|
||||
)
|
||||
.filter(Boolean)
|
||||
.flat();
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,6 @@ export default async function getBlitzStarsAccount(
|
||||
embeds: [notTrackedByBlitzStars],
|
||||
});
|
||||
|
||||
console.log(`Not tracked by BlitzStars.`);
|
||||
|
||||
return null;
|
||||
throw new Error(`${accountId} not tracked by BlitzStars.`);
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,6 @@ export default async function getTankStatsOverTime(
|
||||
const latestTankStatsRaw = await getWargamingResponse<TanksStats>(
|
||||
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
||||
);
|
||||
if (!latestTankStatsRaw) return;
|
||||
const tankHistoriesResponse = await fetch(
|
||||
`https://www.blitzstars.com/api/tankhistories/for/${id}/`,
|
||||
);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { EmbedBuilder } from 'discord.js';
|
||||
import { INFO_COLOR } from '../../constants/colors.js';
|
||||
import { ACCENT_COLOR } from '../../constants/colors.js';
|
||||
|
||||
export default function infoEmbed(title: string, description: string) {
|
||||
return new EmbedBuilder()
|
||||
.setColor(INFO_COLOR)
|
||||
.setColor(ACCENT_COLOR)
|
||||
.setTitle(title)
|
||||
.setDescription(description);
|
||||
}
|
||||
Reference in new issue
Block a user