Files
blitzkit/packages/bot/src/core/blitz/resolveTankId.ts
T

48 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-10-02 10:15:38 -05:00
import { SEARCH_KEYS } from "@blitzkit/core";
import { literals } from "@blitzkit/i18n";
import { Locale } from "discord.js";
import { go } from "fuzzysort";
import markdownEscape from "markdown-escape";
import { tankDefinitions, tankNames } from "../blitzkit/nonBlockingPromises";
import { UserError } from "../blitzkit/userError";
import { tankNamesTechTreeOnly } from "../discord/autocompleteTanks";
import { translator } from "../localization/translator";
2023-05-30 17:24:24 +00:00
2024-09-08 10:11:24 -05:00
export async function resolveTankId(
2024-03-09 11:48:35 -06:00
tank: string | number,
locale: Locale,
2025-10-02 10:15:38 -05:00
techTreeOnly = false
2024-03-09 11:48:35 -06:00
) {
2025-01-16 13:48:13 -06:00
const { strings } = translator(locale);
2025-10-02 10:15:38 -05:00
const number = typeof tank === "string" ? Number(tank) : tank;
2023-05-30 17:24:24 +00:00
if (Number.isNaN(number)) {
2024-03-15 21:50:19 -05:00
const searchResult = go(
`${tank}`,
await (techTreeOnly ? tankNamesTechTreeOnly : tankNames),
2025-10-02 10:15:38 -05:00
{ keys: SEARCH_KEYS, limit: 1 }
2024-03-15 21:50:19 -05:00
);
2023-05-30 17:24:24 +00:00
if (searchResult.length === 0) {
2024-01-19 16:07:15 -06:00
throw new UserError(
2025-10-02 10:15:38 -05:00
literals(strings.bot.common.errors.tank_name_not_found, {
search: markdownEscape(`${tank}`),
})
2024-01-19 16:07:15 -06:00
);
2023-05-30 17:24:24 +00:00
} else {
2023-11-29 12:17:21 -06:00
return searchResult[0].obj.id;
2023-05-30 17:24:24 +00:00
}
} else {
2024-11-18 10:56:24 -06:00
const awaitedTankDefinitions = await tankDefinitions;
if (awaitedTankDefinitions.tanks[number]) {
2023-05-30 17:24:24 +00:00
return number;
} else {
2024-01-19 16:07:15 -06:00
throw new UserError(
2025-10-02 10:15:38 -05:00
literals(strings.bot.common.errors.tank_id_not_found, {
search: number,
})
2024-01-19 16:07:15 -06:00
);
2023-05-30 17:24:24 +00:00
}
}
}