Files
blitzkit/packages/bot/src/commands/ownedTanks.tsx
T

90 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-12-05 17:36:18 -06:00
import {
2024-09-07 21:45:00 -05:00
TANK_CLASSES,
TIER_ROMAN_NUMERALS,
2023-12-14 22:19:01 -06:00
TankDefinition,
2024-03-16 00:06:44 -05:00
flags,
2024-09-08 10:17:40 -05:00
getAccountInfo,
getTankStats,
2025-10-01 22:45:21 -05:00
} from "@blitzkit/core";
import { literals } from "@blitzkit/i18n";
import markdownEscape from "markdown-escape";
2024-10-05 15:24:54 -05:00
import {
gameDefinitions,
tankDefinitions,
2025-10-01 22:45:21 -05:00
} from "../core/blitzkit/nonBlockingPromises";
import { addTierChoices } from "../core/discord/addTierChoices";
import { addUsernameChoices } from "../core/discord/addUsernameChoices";
import { autocompleteUsername } from "../core/discord/autocompleteUsername";
import { chunkLines } from "../core/discord/chunkLines";
import { createLocalizedCommand } from "../core/discord/createLocalizedCommand";
import { resolvePlayerFromCommand } from "../core/discord/resolvePlayerFromCommand";
import { translator } from "../core/localization/translator";
2025-10-13 23:20:26 -05:00
import { type CommandRegistry } from "../events/interactionCreate";
2023-04-25 01:39:43 +00:00
2024-03-06 15:34:10 -06:00
export const ownedTanksCommand = new Promise<CommandRegistry>((resolve) => {
resolve({
2025-10-01 22:45:21 -05:00
command: createLocalizedCommand("owned-tanks")
2024-03-06 15:34:10 -06:00
.addStringOption(addTierChoices)
.addStringOption(addUsernameChoices),
2023-04-25 01:39:43 +00:00
2024-03-06 15:34:10 -06:00
async handler(interaction) {
2025-01-16 13:48:13 -06:00
const { strings, unwrap } = translator(interaction.locale);
2025-10-01 22:45:21 -05:00
const tier = Number(interaction.options.getString("tier"));
2024-03-14 16:43:28 -05:00
const { id, region } = await resolvePlayerFromCommand(interaction);
const accountInfo = await getAccountInfo(region, id);
2024-04-15 13:25:35 -05:00
const tankStats = await getTankStats(region, id);
if (tankStats === null) {
2025-01-16 13:48:13 -06:00
return strings.bot.common.errors.no_tank_stats;
2024-04-15 13:25:35 -05:00
}
2024-03-06 15:34:10 -06:00
const filteredTanks = (
await Promise.all(
tankStats.map(async (tankData) => ({
2024-10-05 15:24:54 -05:00
tankDefinitions: (await tankDefinitions).tanks[tankData.tank_id]!,
2024-03-06 15:34:10 -06:00
id: tankData.tank_id,
2026-03-31 14:11:45 -05:00
})),
2024-03-06 15:34:10 -06:00
)
).filter((tank) => tank.tankDefinitions?.tier === tier);
const groupedTanks: Record<string, TankDefinition[]> = {};
const nations: string[] = [];
2023-08-09 00:19:50 +00:00
2024-03-06 15:34:10 -06:00
filteredTanks.forEach((tank) => {
if (groupedTanks[tank.tankDefinitions.nation] === undefined) {
groupedTanks[tank.tankDefinitions.nation] = [tank.tankDefinitions];
nations.push(tank.tankDefinitions.nation);
} else {
groupedTanks[tank.tankDefinitions.nation].push(tank.tankDefinitions);
}
});
2023-08-09 00:19:50 +00:00
2024-03-16 00:06:44 -05:00
const awaitedTankDefinitions = await tankDefinitions;
2024-06-20 13:34:27 -05:00
const awaitedGameDefinitions = await gameDefinitions;
2025-10-01 22:45:21 -05:00
const title = literals(strings.bot.commands.owned_tanks.body.title, {
name: markdownEscape(accountInfo.nickname),
tier: TIER_ROMAN_NUMERALS[tier],
});
const tankList = tankStats
2024-10-05 15:24:54 -05:00
.map(({ tank_id }) => awaitedTankDefinitions.tanks[tank_id])
2024-03-16 00:06:44 -05:00
.filter((tank) => tank.tier === tier)
.sort(
(a, b) =>
2026-03-31 14:11:45 -05:00
TANK_CLASSES.indexOf(a.class) - TANK_CLASSES.indexOf(b.class),
2024-03-16 00:06:44 -05:00
)
.sort(
(a, b) =>
2024-06-20 13:34:27 -05:00
awaitedGameDefinitions.nations.indexOf(a.nation) -
2026-03-31 14:11:45 -05:00
awaitedGameDefinitions.nations.indexOf(b.nation),
2024-03-16 00:06:44 -05:00
)
.map(
(tank) =>
2026-03-31 14:11:45 -05:00
`[${flags[tank.nation]} ${markdownEscape(unwrap(tank.name!))}](<https://blitzkit.app/tanks/${tank.slug}>)`,
);
const lines = [title, ...tankList];
2023-04-26 00:02:10 +00:00
return chunkLines(lines);
2024-03-06 15:34:10 -06:00
},
2023-05-14 17:13:42 +00:00
2024-03-06 15:34:10 -06:00
autocomplete: autocompleteUsername,
});
});