feat: filter strings

This commit is contained in:
TrèsAbhi committed 2024-03-07 13:49:38 -06:00
1 parent 935c578ef6
commit b60f8bfd57
12 files changed
+187 -128

No files matched your search

+12 -12
View File
@@ -181,24 +181,24 @@ export const tankNames = tanksDefinitionsArray.then((tanks) =>
);
export const TANK_ICONS: Record<TankType, string> = {
tankDestroyer: 'https://i.imgur.com/BIHSEH0.png',
light: 'https://i.imgur.com/CSNha5V.png',
medium: 'https://i.imgur.com/wvf3ltm.png',
heavy: 'https://i.imgur.com/ECeqlZa.png',
'AT-SPG': 'https://i.imgur.com/BIHSEH0.png',
lightTank: 'https://i.imgur.com/CSNha5V.png',
mediumTank: 'https://i.imgur.com/wvf3ltm.png',
heavyTank: 'https://i.imgur.com/ECeqlZa.png',
};
export const TANK_ICONS_PREMIUM: Record<TankType, string> = {
tankDestroyer: 'https://i.imgur.com/TCu3EdR.png',
light: 'https://i.imgur.com/zdkpTRb.png',
medium: 'https://i.imgur.com/3z7eHX6.png',
heavy: 'https://i.imgur.com/P3vbmyA.png',
'AT-SPG': 'https://i.imgur.com/TCu3EdR.png',
lightTank: 'https://i.imgur.com/zdkpTRb.png',
mediumTank: 'https://i.imgur.com/3z7eHX6.png',
heavyTank: 'https://i.imgur.com/P3vbmyA.png',
};
export const TANK_ICONS_COLLECTOR: Record<TankType, string> = {
tankDestroyer: 'https://i.imgur.com/WTjeirB.png',
light: 'https://i.imgur.com/EwhtKkU.png',
medium: 'https://i.imgur.com/u8YDMBh.png',
heavy: 'https://i.imgur.com/8xRf3nc.png',
'AT-SPG': 'https://i.imgur.com/WTjeirB.png',
lightTank: 'https://i.imgur.com/EwhtKkU.png',
mediumTank: 'https://i.imgur.com/u8YDMBh.png',
heavyTank: 'https://i.imgur.com/8xRf3nc.png',
};
export const TIERS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as const;
+102 -54
View File
@@ -1,60 +1,108 @@
import { SlashCommandBuilder, SlashCommandSubcommandBuilder } from 'discord.js';
import { TREE_TYPE_NAMES, TreeType } from '../../components/Tanks';
import { EncyclopediaInfo } from '../blitz/encyclopediaInfo';
import {
APIApplicationCommandOptionChoice,
Locale,
SlashCommandBuilder,
SlashCommandSubcommandBuilder,
} from 'discord.js';
import { TANK_TYPES, TREE_TYPES } from '../../components/Tanks';
import { translator } from '../localization/translator';
import addTankChoices from './addTankChoices';
import addTierChoices from './addTierChoices';
import { localizationObject } from './localizationObject';
export function addFilterOptions<
Option extends SlashCommandBuilder | SlashCommandSubcommandBuilder,
>(option: Option, encyclopediaInfo: EncyclopediaInfo) {
return option
.addStringOption((option) =>
option
.setName('nation')
.setDescription('Nation')
.addChoices(
...Object.entries(encyclopediaInfo.vehicle_nations).map(
([type, name]) => ({ name, value: type }),
),
)
.setRequired(false),
)
.addStringOption((option) => addTierChoices(option).setRequired(false))
.addStringOption((option) =>
option
.setName('tank-type')
.setDescription('Tank type')
.addChoices(
...Object.entries(encyclopediaInfo.vehicle_types).map(
([type, name]) => ({
name: name.replace(' Tank', ''),
value: type,
}),
),
)
.setRequired(false),
)
.addStringOption((option) =>
option
.setName('tree-type')
.setDescription('Tech tree type')
.addChoices(
{
name: TREE_TYPE_NAMES['researchable'],
value: 'researchable' satisfies TreeType,
},
{
name: TREE_TYPE_NAMES.premium,
value: 'premium' satisfies TreeType,
},
{
name: TREE_TYPE_NAMES.collector,
value: 'collector' satisfies TreeType,
},
)
.setRequired(false),
)
.addStringOption((option) =>
addTankChoices(option).setRequired(false),
) as Option;
>(option: Option, nations: string[]) {
const { translate } = translator(Locale.EnglishUS);
return (
option
.addStringOption((option) =>
option
.setName(translate('bot.common.options.filters.nation'))
.setNameLocalizations(
localizationObject('bot.common.options.filters.nation'),
)
.setDescription(
translate('bot.common.options.filters.nation.description'),
)
.setDescriptionLocalizations(
localizationObject('bot.common.options.filters.nation.description'),
)
.addChoices(
...nations.map(
(nation) =>
({
value: nation,
name: translate(`common.nations.${nation}`),
name_localizations: localizationObject(
`common.nations.${nation}`,
),
}) satisfies APIApplicationCommandOptionChoice,
),
)
.setRequired(false),
)
.addStringOption((option) => addTierChoices(option).setRequired(false))
.addStringOption((option) =>
option
.setName(translate('bot.common.options.filters.tank_type'))
.setNameLocalizations(
localizationObject('bot.common.options.filters.tank_type'),
)
.setDescription(
translate('bot.common.options.filters.tank_type.description'),
)
.setDescriptionLocalizations(
localizationObject(
'bot.common.options.filters.tank_type.description',
),
)
.addChoices(
...TANK_TYPES.map(
(tankType) =>
({
value: tankType,
name: translate(`common.tank_type_short.${tankType}`),
name_localizations: localizationObject(
`common.tank_type_short.${tankType}`,
),
}) satisfies APIApplicationCommandOptionChoice,
),
)
.setRequired(false),
)
.addStringOption((option) =>
option
.setName(translate('bot.common.options.filters.tree_type'))
.setNameLocalizations(
localizationObject('bot.common.options.filters.tree_type'),
)
.setDescription(
translate('bot.common.options.filters.tree_type.description'),
)
.setDescriptionLocalizations(
localizationObject(
'bot.common.options.filters.tree_type.description',
),
)
.addChoices(
...TREE_TYPES.map(
(treeType) =>
({
value: treeType,
name: translate(`common.tree_type.${treeType}`),
name_localizations: localizationObject(
`common.tree_type.${treeType}`,
),
}) satisfies APIApplicationCommandOptionChoice,
),
)
.setRequired(false),
)
// TODO: somehow localize this?
.addStringOption((option) =>
addTankChoices(option).setRequired(false),
) as Option
);
}
+5 -5
View File
@@ -3,7 +3,7 @@ import {
SlashCommandSubcommandBuilder,
SlashCommandSubcommandGroupBuilder,
} from 'discord.js';
import { encyclopediaInfo } from '../blitz/encyclopediaInfo';
import { NATIONS } from '../blitzkrieg/tankDefinitions';
import { addFilterOptions } from './addFilterOptions';
import { addPeriodSubCommands } from './addPeriodSubCommands';
@@ -15,10 +15,10 @@ export default async function addPeriodicFilterOptions<
option: SlashCommandSubcommandBuilder,
) => SlashCommandSubcommandBuilder = (option) => option,
): Promise<OptionType> {
const awaitedEncyclopediaInfo = await encyclopediaInfo;
addPeriodSubCommands(option, (option) => {
return addFilterOptions(extra(option), awaitedEncyclopediaInfo);
});
const nations = await NATIONS;
addPeriodSubCommands(option, (option) =>
addFilterOptions(extra(option), nations),
);
return option;
}
+11 -3
View File
@@ -1,9 +1,17 @@
import { SlashCommandStringOption } from 'discord.js';
import { Locale, SlashCommandStringOption } from 'discord.js';
import { translator } from '../localization/translator';
import { localizationObject } from './localizationObject';
export default function addTankChoices(option: SlashCommandStringOption) {
const { translate } = translator(Locale.EnglishUS);
return option
.setName('tank')
.setDescription('The tank to get stats for')
.setName(translate('bot.common.options.tank'))
.setNameLocalizations(localizationObject('bot.common.options.tank'))
.setDescription(translate('bot.common.options.tank.description'))
.setDescriptionLocalizations(
localizationObject('bot.common.options.tank.description'),
)
.setAutocomplete(true)
.setRequired(true);
}
+10 -9
View File
@@ -23,23 +23,24 @@ export function createLocalizedCommand(
| CreateLocalizedCommandSubcommand[]
| CreateLocalizedCommandSubcommandGroup[],
) {
const commandPathItem = command.replaceAll('-', '_');
const { translate } = translator(Locale.EnglishUS);
const slashCommand = new SlashCommandBuilder()
.setName(command)
.setNameLocalizations(localizationObject(`bot.commands.${command}`))
.setDescription(translate(`bot.commands.${command}.description`))
.setNameLocalizations(localizationObject(`bot.commands.${commandPathItem}`))
.setDescription(translate(`bot.commands.${commandPathItem}.description`))
.setDescriptionLocalizations(
localizationObject(`bot.commands.${command}.description`),
localizationObject(`bot.commands.${commandPathItem}.description`),
);
if (sub) {
sub.map((subItem) => {
if ('subcommand' in subItem) {
const subcommandNameLocalizations = localizationObject(
`bot.commands.${command}.subcommands.${subItem.subcommand}`,
`bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}`,
);
const subcommandDescriptionLocalizations = localizationObject(
`bot.commands.${command}.subcommands.${subItem.subcommand}.description`,
`bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}.description`,
);
slashCommand.addSubcommand((option) => {
@@ -50,17 +51,17 @@ export function createLocalizedCommand(
.setNameLocalizations(subcommandNameLocalizations)
.setDescription(
translate(
`bot.commands.${command}.subcommands.${subItem.subcommand}.description`,
`bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}.description`,
),
)
.setDescriptionLocalizations(subcommandDescriptionLocalizations);
});
} else {
const subcommandNameLocalizations = localizationObject(
`bot.commands.${command}.groups.${subItem.group}`,
`bot.commands.${commandPathItem}.groups.${subItem.group}`,
);
const subcommandDescriptionLocalizations = localizationObject(
`bot.commands.${command}.groups.${subItem.group}.description`,
`bot.commands.${commandPathItem}.groups.${subItem.group}.description`,
);
slashCommand.addSubcommandGroup((option) => {
@@ -71,7 +72,7 @@ export function createLocalizedCommand(
.setNameLocalizations(subcommandNameLocalizations)
.setDescription(
translate(
`bot.commands.${command}.groups.${subItem.group}.description`,
`bot.commands.${commandPathItem}.groups.${subItem.group}.description`,
),
)
.setDescriptionLocalizations(subcommandDescriptionLocalizations);