feat: user error strings

This commit is contained in:
TrèsAbhi committed 2024-03-09 11:48:35 -06:00
1 parent 265eff98bf
commit 889ae39e3b
14 files changed
+71 -374

No files matched your search

+9 -4
View File
@@ -1,18 +1,23 @@
import { Locale } from 'discord.js';
import { Region } from '../../constants/regions';
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
import { UserError } from '../../hooks/userError';
import { TanksStats } from '../../types/tanksStats';
import { translator } from '../localization/translator';
import fetchBlitz from './fetchBlitz';
export default async function getTankStats(region: Region, id: number) {
export default async function getTankStats(
region: Region,
id: number,
locale: Locale,
) {
const { t } = translator(locale);
const tankStats = await fetchBlitz<TanksStats>(
`https://api.wotblitz.${region}/wotb/tanks/stats/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
);
if (tankStats[id] === null) {
throw new UserError(
"# No tank stats available\nThis player doesn't have any stats available. This may not be the player you are looking for.",
);
throw new UserError(t`bot.common.errors.no_tank_stats`);
}
return tankStats[id];
+12 -4
View File
@@ -1,12 +1,18 @@
import { Locale } from 'discord.js';
import { go } from 'fuzzysort';
import { UserError } from '../../hooks/userError';
import markdownEscape from 'markdown-escape';
import { UserError } from '../../hooks/userError';
import {
tankDefinitions,
tanksDefinitionsArray,
} from '../blitzkrieg/tankDefinitions';
import { translator } from '../localization/translator';
export default async function resolveTankId(tank: string | number) {
export default async function resolveTankId(
tank: string | number,
locale: Locale,
) {
const { translate } = translator(locale);
const number = typeof tank === 'string' ? parseInt(tank) : tank;
if (Number.isNaN(number)) {
@@ -17,7 +23,9 @@ export default async function resolveTankId(tank: string | number) {
if (searchResult.length === 0) {
throw new UserError(
`# Tank not found\nCould not find tank by the name "${markdownEscape(`${tank}`)}".`,
translate('bot.common.errors.tank_name_not_found', [
markdownEscape(`${tank}`),
]),
);
} else {
return searchResult[0].obj.id;
@@ -27,7 +35,7 @@ export default async function resolveTankId(tank: string | number) {
return number;
} else {
throw new UserError(
`# Tank not found\nCould not find tank by the ID "${number}". Try typing the name of the tank instead of a number.`,
translate('bot.common.errors.tank_id_not_found', [`${number}`]),
);
}
}
+3 -1
View File
@@ -1,3 +1,4 @@
import { Locale } from 'discord.js';
import { Region } from '../../constants/regions';
import {
GetHistoriesOptions,
@@ -34,6 +35,7 @@ export const getTankHistoriesDefaultOptions: GetTankHistoriesOptions = {
export default async function getTankHistories(
server: Region,
id: number,
locale: Locale,
options?: Partial<GetTankHistoriesOptions>,
) {
const mergedOptions = { ...getTankHistoriesDefaultOptions, ...options };
@@ -76,7 +78,7 @@ export default async function getTankHistories(
const latestNodes: TankHistories = [];
if (mergedOptions.includeLatestHistories) {
(await getTankStats(server, id)).forEach((tankHistory) => {
(await getTankStats(server, id, locale)).forEach((tankHistory) => {
if (
tankHistory.all.battles > (lastBattles[tankHistory.tank_id] ?? 0) &&
(mergedOptions.tankId === undefined ||
+3 -1
View File
@@ -1,3 +1,4 @@
import { Locale } from 'discord.js';
import { Region } from '../../constants/regions';
import { AllStats } from '../blitz/getAccountInfo';
import getTankHistories, {
@@ -45,8 +46,9 @@ export default async function getStatsInPeriod(
id: number,
start: number,
end: number,
locale: Locale,
) {
const history = await getTankHistories(server, id, {
const history = await getTankHistories(server, id, locale, {
includeLatestHistories: true,
});
+4 -1
View File
@@ -15,6 +15,9 @@ export async function getFiltersFromCommand(
treeType:
(interaction.options.getString('tree-type') as TreeType | undefined) ??
undefined,
tank: tankRaw === null ? undefined : await resolveTankId(tankRaw),
tank:
tankRaw === null
? undefined
: await resolveTankId(tankRaw, interaction.locale),
} satisfies StatFilters;
}
+3 -1
View File
@@ -3,11 +3,13 @@ import markdownEscape from 'markdown-escape';
import { Region } from '../../constants/regions';
import { UserError } from '../../hooks/userError';
import searchClansAcrossRegions from '../blitz/searchClansAcrossRegions';
import { translator } from '../localization/translator';
import { serverAndIdPattern } from './resolvePlayerFromCommand';
export default async function resolveClanFromCommand(
interaction: ChatInputCommandInteraction,
) {
const { translate } = translator(interaction.locale);
const clan = interaction.options.getString('clan', true);
if (serverAndIdPattern.test(clan)) {
@@ -20,7 +22,7 @@ export default async function resolveClanFromCommand(
return { region: accounts[0].region, id: accounts[0].clan_id };
} else {
throw new UserError(
`# Could not find clan\nCouldn't find "${markdownEscape(clan)}". Try selecting a clan from the search result.`,
translate('bot.common.errors.clan_not_found', [markdownEscape(clan)]),
);
}
}
+6 -6
View File
@@ -4,6 +4,7 @@ import { Region } from '../../constants/regions';
import { UserError } from '../../hooks/userError';
import searchPlayersAcrossRegions from '../blitz/searchPlayersAcrossRegions';
import { getBlitzFromDiscord } from '../blitzkrieg/discordBlitz';
import { translator } from '../localization/translator';
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
@@ -15,6 +16,7 @@ export interface ResolvedPlayer {
export default async function resolvePlayerFromCommand(
interaction: ChatInputCommandInteraction<CacheType>,
) {
const { t, translate } = translator(interaction.locale);
const commandUsername = interaction.options.getString('username');
if (commandUsername) {
@@ -35,9 +37,9 @@ export default async function resolvePlayerFromCommand(
} satisfies ResolvedPlayer;
} else {
throw new UserError(
`# Could not find user\nI couldn't find user "${markdownEscape(
commandUsername,
)}". Try picking an user from the search result, typing in a valid username, or using the \`/link\` command.`,
translate('bot.common.errors.player_not_found', [
markdownEscape(commandUsername),
]),
);
}
}
@@ -47,9 +49,7 @@ export default async function resolvePlayerFromCommand(
if (account) {
return { region: account.region, id: account.blitz };
} else {
throw new UserError(
"# I don't know who you are\nUse the `/link` command to get started.",
);
throw new UserError(t`bot.common.errors.player_not_linked`);
}
}
}