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
@@ -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 [];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user