feat: null handling

This commit is contained in:
TrèsAbhi committed 2024-04-16 13:45:03 -05:00
1 parent 3e7a73c528
commit 7aa0b65d1d
6 files changed
+41 -19

No files matched your search

+1 -2
View File
@@ -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}`);
+6 -1
View File
@@ -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];
+7 -2
View File
@@ -31,11 +31,16 @@ export const ownedTanksCommand = new Promise<CommandRegistry>((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) => ({
+5 -1
View File
@@ -126,7 +126,11 @@ export const researchCommand = new Promise<CommandRegistry>((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', [
+10 -1
View File
@@ -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<GetTankHistoriesOptions>,
) {
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 ||
+12 -12
View File
@@ -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",