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

89 lines
3.2 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,
2024-09-07 21:45:00 -05:00
} from '@blitzkit/core';
import markdownEscape from 'markdown-escape';
2024-10-05 15:24:54 -05:00
import {
gameDefinitions,
tankDefinitions,
} from '../core/blitzkit/nonBlockingPromises';
2024-09-08 10:11:24 -05:00
import { addTierChoices } from '../core/discord/addTierChoices';
import { addUsernameChoices } from '../core/discord/addUsernameChoices';
import { autocompleteUsername } from '../core/discord/autocompleteUsername';
import { chunkLines } from '../core/discord/chunkLines';
2024-03-08 20:29:35 -06:00
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
2024-09-08 10:11:24 -05:00
import { resolvePlayerFromCommand } from '../core/discord/resolvePlayerFromCommand';
2024-03-08 20:29:35 -06:00
import { translator } from '../core/localization/translator';
2023-07-11 19:54:57 +00:00
import { 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({
2024-03-08 20:29:35 -06: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) {
2024-04-15 13:25:35 -05:00
const { t, translate } = translator(interaction.locale);
2024-03-06 15:34:10 -06: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) {
return t`bot.common.errors.no_tank_stats`;
}
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,
})),
)
).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;
const title = translate('bot.commands.owned_tanks.body.title', [
markdownEscape(accountInfo.nickname),
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) =>
TANK_CLASSES.indexOf(a.class) - TANK_CLASSES.indexOf(b.class),
)
.sort(
(a, b) =>
2024-06-20 13:34:27 -05:00
awaitedGameDefinitions.nations.indexOf(a.nation) -
awaitedGameDefinitions.nations.indexOf(b.nation),
2024-03-16 00:06:44 -05:00
)
.map(
(tank) =>
`[${flags[tank.nation]} ${markdownEscape(tank.name)}](<https://blitzkit.app/tools/tankopedia/${tank.id}>)`,
);
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,
});
});