feat: bug fixes and error safety

This commit is contained in:
TresAbhi committed 2023-05-28 16:56:15 +00:00
1 parent 0b6b2ecedc
commit 780d9d5f8e
25 files changed
+136 -182

No files matched your search

+2 -2
View File
@@ -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}"`);
}
}
}
+3 -3
View File
@@ -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}"`);
}
}
}
+2 -3
View File
@@ -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;
}
}
+41 -44
View File
@@ -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 [];
}
}