From 4ff5f7cdeb371776e608ea60356304e14433fc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Mon, 15 Apr 2024 13:25:35 -0500 Subject: [PATCH] feat: null handling --- scripts/test.ts | 3 +-- src/commands/breakdown/render.tsx | 7 ++++++- src/commands/ownedTanks.tsx | 9 +++++++-- src/commands/research.tsx | 6 +++++- src/core/blitzkrieg/getTankHistories.ts | 11 ++++++++++- src/lang/en-US.json | 24 ++++++++++++------------ 6 files changed, 41 insertions(+), 19 deletions(-) diff --git a/scripts/test.ts b/scripts/test.ts index 9addb4563..cab7f3bab 100644 --- a/scripts/test.ts +++ b/scripts/test.ts @@ -1,4 +1,3 @@ -import { Locale } from 'discord.js'; import { chunk, times } from 'lodash'; import { compress } from 'lz4js'; import { argv } from 'process'; @@ -64,7 +63,7 @@ for (const batch of batches) { let playersDone = 0; const players = await Promise.all( chunk.users.map(async (user) => { - const tanks = await getTankStats(region, user.blitz, Locale.EnglishUS); + const tanks = await getTankStats(region, user.blitz); console.log(`Player ${++playersDone} of ${chunk.users.length}`); diff --git a/src/commands/breakdown/render.tsx b/src/commands/breakdown/render.tsx index ff7b087ca..a90c877f3 100644 --- a/src/commands/breakdown/render.tsx +++ b/src/commands/breakdown/render.tsx @@ -20,6 +20,7 @@ import { translator } from '../../core/localization/translator'; import calculateWN8 from '../../core/statistics/calculateWN8'; import { StatFilters, filterStats } from '../../core/statistics/filterStats'; import getWN8Percentile from '../../core/statistics/getWN8Percentile'; +import { UserError } from '../../hooks/userError'; const ROWS_PER_PAGE = 8; const MAX_PAGES = 9; @@ -37,7 +38,7 @@ export async function renderBreakdown( const { filteredOrder } = await filterStats(statsInPeriod, filters); const accountInfo = await getAccountInfo(region, id); const clanData = await getClanAccountInfo(region, id, ['clan']); - const tankStats = await getTankStats(region, id, locale); + const tankStats = await getTankStats(region, id); const filterDescriptions = await filtersToDescription(filters, locale); const orderedCurrentStats: AllStats[] = []; const orderedCareerStats: AllStats[] = []; @@ -45,6 +46,10 @@ export async function renderBreakdown( const orderedCareerWN8: (number | undefined)[] = []; const orderedCareerWN8Full: (number | undefined)[] = []; + if (tankStats === null) { + throw new UserError(t`bot.common.errors.no_tank_stats`); + } + filteredOrder.forEach((id) => { const tankAveragesEntry = awaitedTankAverages[id]; const current = statsInPeriod.diff[id]; diff --git a/src/commands/ownedTanks.tsx b/src/commands/ownedTanks.tsx index 29f081c67..8a51ffde2 100644 --- a/src/commands/ownedTanks.tsx +++ b/src/commands/ownedTanks.tsx @@ -31,11 +31,16 @@ export const ownedTanksCommand = new Promise((resolve) => { .addStringOption(addUsernameChoices), async handler(interaction) { - const { translate } = translator(interaction.locale); + const { t, translate } = translator(interaction.locale); const tier = Number(interaction.options.getString('tier')); const { id, region } = await resolvePlayerFromCommand(interaction); const accountInfo = await getAccountInfo(region, id); - const tankStats = await getTankStats(region, id, interaction.locale); + const tankStats = await getTankStats(region, id); + + if (tankStats === null) { + return t`bot.common.errors.no_tank_stats`; + } + const filteredTanks = ( await Promise.all( tankStats.map(async (tankData) => ({ diff --git a/src/commands/research.tsx b/src/commands/research.tsx index ab50379c6..d2bfd9e21 100644 --- a/src/commands/research.tsx +++ b/src/commands/research.tsx @@ -126,7 +126,11 @@ export const researchCommand = new Promise((resolve) => { ]); } - const tankStats = await getTankStats(region, id, interaction.locale); + const tankStats = await getTankStats(region, id); + + if (tankStats === null) { + return t`bot.common.errors.no_tank_stats`; + } if (tankStats.some(({ tank_id }) => tank_id === targetTankId)) { return translate('bot.commands.research.errors.already_researched', [ diff --git a/src/core/blitzkrieg/getTankHistories.ts b/src/core/blitzkrieg/getTankHistories.ts index bba997d01..1a281beec 100644 --- a/src/core/blitzkrieg/getTankHistories.ts +++ b/src/core/blitzkrieg/getTankHistories.ts @@ -1,5 +1,6 @@ import { Locale } from 'discord.js'; import { Region } from '../../constants/regions'; +import { UserError } from '../../hooks/userError'; import { GetHistoriesOptions, Histories, @@ -8,6 +9,7 @@ import { } from '../../types/histories'; import getTankStats from '../blitz/getTankStats'; import { emptyAllStats } from '../blitzstars/getStatsInPeriod/constants'; +import { translator } from '../localization/translator'; export interface TankHistory extends History { tank_id: number; @@ -38,6 +40,7 @@ export default async function getTankHistories( locale: Locale, options?: Partial, ) { + const { t } = translator(locale); const mergedOptions = { ...getTankHistoriesDefaultOptions, ...options }; const tankHistoriesResponse = await fetch( `https://www.blitzstars.com/api/tankhistories/for/${id}/`, @@ -78,7 +81,13 @@ export default async function getTankHistories( const latestNodes: TankHistories = []; if (mergedOptions.includeLatestHistories) { - (await getTankStats(server, id, locale)).forEach((tankHistory) => { + const tankStats = await getTankStats(server, id); + + if (tankStats === null) { + throw new UserError(t`bot.common.errors.no_tank_stats`); + } + + tankStats.forEach((tankHistory) => { if ( tankHistory.all.battles > (lastBattles[tankHistory.tank_id] ?? 0) && (mergedOptions.tankId === undefined || diff --git a/src/lang/en-US.json b/src/lang/en-US.json index 3c1f6ab93..ae0712ae0 100644 --- a/src/lang/en-US.json +++ b/src/lang/en-US.json @@ -62,6 +62,18 @@ "bot": { "common": { + "errors": { + "clan_not_found": "# Could not find clan\nCouldn't find \"%s\". Try selecting a clan from the search result.", + "player_not_found": "# Could not find user\nI couldn't find user \"%s\". Try picking an user from the search result, typing in a valid username, or using the `/link` command.", + "player_not_linked": "# I don't know who you are\nUse the `/link` command to get started.", + "tank_name_not_found": "# Tank not found\nCould not find tank by the name \"%s\".", + "tank_id_not_found": "# Tank not found\nCould not find tank by the ID \"%s\". Try typing the name of the tank instead of a number.", + "no_tank_stats": "# No tank stats available\nThis player doesn't have any stats available. This may not be the player you are looking for.", + "get_help": "Help Discord server", + "uncaught_error": "# Blitzkrieg ran into an error!\nWe're so sorry about this. Feel free to [join the official Discord server](https://discord.gg/nDt7AjGJQH) to get help.", + "preview": "# This command is in preview\n\nYou can only access this command in [the official Discord server](https://discord.gg/nDt7AjGJQH). The command will be generally available soon." + }, + "wn8": { "$": "WN8", "very_bad": "Very bad", @@ -183,18 +195,6 @@ "refresh": "Refresh" }, - "errors": { - "clan_not_found": "# Could not find clan\nCouldn't find \"%s\". Try selecting a clan from the search result.", - "player_not_found": "# Could not find user\nI couldn't find user \"%s\". Try picking an user from the search result, typing in a valid username, or using the `/link` command.", - "player_not_linked": "# I don't know who you are\nUse the `/link` command to get started.", - "tank_name_not_found": "# Tank not found\nCould not find tank by the name \"%s\".", - "tank_id_not_found": "# Tank not found\nCould not find tank by the ID \"%s\". Try typing the name of the tank instead of a number.", - "no_tank_stats": "# No tank stats available\nThis player doesn't have any stats available. This may not be the player you are looking for.", - "get_help": "Help Discord server", - "uncaught_error": "# Blitzkrieg ran into an error!\nWe're so sorry about this. Feel free to [join the official Discord server](https://discord.gg/nDt7AjGJQH) to get help.", - "preview": "# This command is in preview\n\nYou can only access this command in [the official Discord server](https://discord.gg/nDt7AjGJQH). The command will be generally available soon." - }, - "hero_stats": { "battles": "Battles", "winrate": "Winrate",