feat: period subcommands

This commit is contained in:
TrèsAbhi committed 2024-03-05 10:51:14 -06:00
1 parent db7c865c76
commit 8a76558f01
10 files changed
+199 -100

No files matched your search

+2 -2
View File
@@ -24,14 +24,14 @@ export const aboutCommand = new Promise<CommandRegistry>(async (resolve) => {
inProduction: true, inProduction: true,
inPublic: true, inPublic: true,
command: await createLocalizedCommand('about', [ command: createLocalizedCommand('about', [
{ subcommand: 'commands' }, { subcommand: 'commands' },
...subcommands.map((subcommand) => ({ subcommand })), ...subcommands.map((subcommand) => ({ subcommand })),
]), ]),
async handler(interaction) { async handler(interaction) {
const subcommand = interaction.options.getSubcommand(); const subcommand = interaction.options.getSubcommand();
const { t } = await translator(interaction.locale); const { t } = translator(interaction.locale);
if (subcommand === 'commands') { if (subcommand === 'commands') {
return t`bot.commands.about.subcommands.commands.body${( return t`bot.commands.about.subcommands.commands.body${(
+5 -9
View File
@@ -1,4 +1,3 @@
import { SlashCommandBuilder } from 'discord.js';
import { chunk } from 'lodash'; import { chunk } from 'lodash';
import * as Breakdown from '../components/Breakdown'; import * as Breakdown from '../components/Breakdown';
import NoData, { NoDataType } from '../components/NoData'; import NoData, { NoDataType } from '../components/NoData';
@@ -21,6 +20,7 @@ import autocompleteTanks from '../core/discord/autocompleteTanks';
import autocompleteUsername from '../core/discord/autocompleteUsername'; import autocompleteUsername from '../core/discord/autocompleteUsername';
import buttonPrimary from '../core/discord/buttonPrimary'; import buttonPrimary from '../core/discord/buttonPrimary';
import commandToURL from '../core/discord/commandToURL'; import commandToURL from '../core/discord/commandToURL';
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
import { getCustomPeriodParams } from '../core/discord/getCustomPeriodParams'; import { getCustomPeriodParams } from '../core/discord/getCustomPeriodParams';
import { getFiltersFromButton } from '../core/discord/getFiltersFromButton'; import { getFiltersFromButton } from '../core/discord/getFiltersFromButton';
import { getFiltersFromCommand } from '../core/discord/getFiltersFromCommand'; import { getFiltersFromCommand } from '../core/discord/getFiltersFromCommand';
@@ -292,18 +292,14 @@ export async function renderBreakdown(
export const breakdownCommand = new Promise<CommandRegistryPromisable>( export const breakdownCommand = new Promise<CommandRegistryPromisable>(
async (resolve) => { async (resolve) => {
const command = await addPeriodicFilterOptions(
new SlashCommandBuilder()
.setName('breakdown')
.setDescription("A period's breakdown by tanks played"),
(option) => option.addStringOption(addUsernameChoices),
);
resolve({ resolve({
inProduction: true, inProduction: true,
inPublic: true, inPublic: true,
command, command: await addPeriodicFilterOptions(
createLocalizedCommand('breakdown'),
(option) => option.addStringOption(addUsernameChoices),
),
async handler(interaction) { async handler(interaction) {
const player = await resolvePlayerFromCommand(interaction); const player = await resolvePlayerFromCommand(interaction);
+9 -11
View File
@@ -13,7 +13,7 @@ import { emblemIdToURL } from '../core/blitzkrieg/emblemIdToURL';
import getTankHistories from '../core/blitzkrieg/getTankHistories'; import getTankHistories from '../core/blitzkrieg/getTankHistories';
import { getBlitzStarsLinkButton } from '../core/blitzstars/getBlitzStarsLinkButton'; import { getBlitzStarsLinkButton } from '../core/blitzstars/getBlitzStarsLinkButton';
import getPlayerHistories from '../core/blitzstars/getPlayerHistories'; import getPlayerHistories from '../core/blitzstars/getPlayerHistories';
import addPeriodSubCommands from '../core/discord/addPeriodSubCommands'; import { addPeriodSubCommands } from '../core/discord/addPeriodSubCommands';
import addTankChoices from '../core/discord/addTankChoices'; import addTankChoices from '../core/discord/addTankChoices';
import addUsernameChoices from '../core/discord/addUsernameChoices'; import addUsernameChoices from '../core/discord/addUsernameChoices';
import autocompleteTanks from '../core/discord/autocompleteTanks'; import autocompleteTanks from '../core/discord/autocompleteTanks';
@@ -127,16 +127,14 @@ export const evolutionCommand = new Promise<CommandRegistryPromisable>(
return tankGroup; return tankGroup;
}); });
await Promise.all([ addPeriodSubCommands(playerGroup!, (option) =>
addPeriodSubCommands(playerGroup!, (option) => option.addStringOption(addUsernameChoices),
option.addStringOption(addUsernameChoices), );
), addPeriodSubCommands(tankGroup!, (option) =>
addPeriodSubCommands(tankGroup!, (option) => option
option .addStringOption(addTankChoices)
.addStringOption(addTankChoices) .addStringOption(addUsernameChoices),
.addStringOption(addUsernameChoices), );
),
]);
resolve({ resolve({
inProduction: true, inProduction: true,
+91 -20
View File
@@ -1,49 +1,120 @@
import { import {
Locale,
SlashCommandBuilder, SlashCommandBuilder,
SlashCommandSubcommandBuilder, SlashCommandSubcommandBuilder,
SlashCommandSubcommandGroupBuilder, SlashCommandSubcommandGroupBuilder,
} from 'discord.js'; } from 'discord.js';
import { translator } from '../localization/translator';
import { localizationObject } from './localizationObject';
export type PeriodType = 'today' | '30' | '60' | '90' | 'career' | 'custom'; export type PeriodType = 'today' | '30' | '60' | '90' | 'career' | 'custom';
export type PeriodSize = 'today' | `${number}` | 'career'; export type PeriodSize = 'today' | `${number}` | 'career';
export default function addPeriodSubCommands( export function addPeriodSubCommands(
option: SlashCommandBuilder | SlashCommandSubcommandGroupBuilder, option: SlashCommandBuilder | SlashCommandSubcommandGroupBuilder,
extra: ( extra: (
option: SlashCommandSubcommandBuilder, option: SlashCommandSubcommandBuilder,
) => SlashCommandSubcommandBuilder = (option) => option, ) => SlashCommandSubcommandBuilder = (option) => option,
) { ) {
const { translate } = translator(Locale.EnglishUS);
const subcommandNameLocalizations = {
today: localizationObject('bot.common.subcommands.today'),
30: localizationObject('bot.common.subcommands.30'),
60: localizationObject('bot.common.subcommands.60'),
90: localizationObject('bot.common.subcommands.90'),
career: localizationObject('bot.common.subcommands.career'),
custom: localizationObject('bot.common.subcommands.custom'),
};
const subcommandDescriptionLocalizations = {
today: localizationObject('bot.common.subcommands.today.description'),
30: localizationObject('bot.common.subcommands.30.description'),
60: localizationObject('bot.common.subcommands.60.description'),
90: localizationObject('bot.common.subcommands.90.description'),
career: localizationObject('bot.common.subcommands.career.description'),
custom: localizationObject('bot.common.subcommands.custom.description'),
};
option option
.addSubcommand((option) => .addSubcommand((option) =>
extra(option.setName('today').setDescription('Today')), extra(
) option
.addSubcommand((option) => .setName(translate('bot.common.subcommands.today'))
extra(option.setName('30').setDescription('30 days')), .setNameLocalizations(subcommandNameLocalizations.today)
) .setDescription(translate('bot.common.subcommands.today.description'))
.addSubcommand((option) => .setDescriptionLocalizations(
extra(option.setName('60').setDescription('60 days')), subcommandDescriptionLocalizations.today,
) ),
.addSubcommand((option) => ),
extra(option.setName('90').setDescription('90 days')),
)
.addSubcommand((option) =>
extra(option.setName('career').setDescription('Career')),
) )
.addSubcommand((option) => .addSubcommand((option) =>
extra( extra(
option option
.setName('custom') .setName(translate('bot.common.subcommands.30'))
.setDescription('Custom range') .setNameLocalizations(subcommandNameLocalizations[30])
.setDescription(translate('bot.common.subcommands.30.description'))
.setDescriptionLocalizations(subcommandDescriptionLocalizations[30]),
),
)
.addSubcommand((option) =>
extra(
option
.setName(translate('bot.common.subcommands.60'))
.setNameLocalizations(subcommandNameLocalizations[60])
.setDescription(translate('bot.common.subcommands.60.description'))
.setDescriptionLocalizations(subcommandDescriptionLocalizations[60]),
),
)
.addSubcommand((option) =>
extra(
option
.setName(translate('bot.common.subcommands.90'))
.setNameLocalizations(subcommandNameLocalizations[90])
.setDescription(translate('bot.common.subcommands.90.description'))
.setDescriptionLocalizations(subcommandDescriptionLocalizations[90]),
),
)
.addSubcommand((option) =>
extra(
option
.setName(translate('bot.common.subcommands.career'))
.setNameLocalizations(subcommandNameLocalizations.career)
.setDescription(
translate('bot.common.subcommands.career.description'),
)
.setDescriptionLocalizations(
subcommandDescriptionLocalizations.career,
),
),
)
.addSubcommand((option) =>
extra(
option
.setName(translate('bot.common.subcommands.custom'))
.setNameLocalizations(subcommandNameLocalizations.custom)
.setDescription(
translate('bot.common.subcommands.custom.description'),
)
.setDescriptionLocalizations(
subcommandDescriptionLocalizations.custom,
)
.addIntegerOption((option) => .addIntegerOption((option) =>
option option
.setName('start') .setName(translate('bot.common.subcommands.custom.options.start'))
.setDescription('Start in number of days ago from today') .setDescription(
translate(
'bot.common.subcommands.custom.options.start.description',
),
)
.setRequired(true), .setRequired(true),
) )
.addIntegerOption((option) => .addIntegerOption((option) =>
option option
.setName('end') .setName(translate('bot.common.subcommands.custom.options.end'))
.setDescription('End in number of days') .setDescription(
translate(
'bot.common.subcommands.custom.options.end.description',
),
)
.setRequired(true), .setRequired(true),
), ),
), ),
+1 -2
View File
@@ -5,7 +5,7 @@ import {
} from 'discord.js'; } from 'discord.js';
import { encyclopediaInfo } from '../blitz/encyclopediaInfo'; import { encyclopediaInfo } from '../blitz/encyclopediaInfo';
import { addFilterOptions } from './addFilterOptions'; import { addFilterOptions } from './addFilterOptions';
import addPeriodSubCommands from './addPeriodSubCommands'; import { addPeriodSubCommands } from './addPeriodSubCommands';
export default async function addPeriodicFilterOptions< export default async function addPeriodicFilterOptions<
OptionType extends SlashCommandBuilder | SlashCommandSubcommandGroupBuilder, OptionType extends SlashCommandBuilder | SlashCommandSubcommandGroupBuilder,
@@ -16,7 +16,6 @@ export default async function addPeriodicFilterOptions<
) => SlashCommandSubcommandBuilder = (option) => option, ) => SlashCommandSubcommandBuilder = (option) => option,
): Promise<OptionType> { ): Promise<OptionType> {
const awaitedEncyclopediaInfo = await encyclopediaInfo; const awaitedEncyclopediaInfo = await encyclopediaInfo;
addPeriodSubCommands(option, (option) => { addPeriodSubCommands(option, (option) => {
return addFilterOptions(extra(option), awaitedEncyclopediaInfo); return addFilterOptions(extra(option), awaitedEncyclopediaInfo);
}); });
+23 -25
View File
@@ -6,42 +6,40 @@ interface CreateLocalizedCommandSubcommand {
subcommand: string; subcommand: string;
} }
export async function createLocalizedCommand( export function createLocalizedCommand(
command: string, command: string,
subcommands?: CreateLocalizedCommandSubcommand[], subcommands?: CreateLocalizedCommandSubcommand[],
) { ) {
const { translate } = await translator(Locale.EnglishUS); const { translate } = translator(Locale.EnglishUS);
const slashCommand = new SlashCommandBuilder() const slashCommand = new SlashCommandBuilder()
.setName(command) .setName(command)
.setNameLocalizations(await localizationObject(`bot.commands.${command}`)) .setNameLocalizations(localizationObject(`bot.commands.${command}`))
.setDescription(translate(`bot.commands.${command}.description`)) .setDescription(translate(`bot.commands.${command}.description`))
.setDescriptionLocalizations( .setDescriptionLocalizations(
await localizationObject(`bot.commands.${command}.description`), localizationObject(`bot.commands.${command}.description`),
); );
if (subcommands) { if (subcommands) {
await Promise.all( subcommands.map(({ subcommand }) => {
subcommands.map(async ({ subcommand }) => { const subcommandNameLocalizations = localizationObject(
const subcommandNameLocalizations = await localizationObject( `bot.commands.${command}.subcommands.${subcommand}`,
`bot.commands.${command}.subcommands.${subcommand}`, );
); const subcommandDescriptionLocalizations = localizationObject(
const subcommandDescriptionLocalizations = await localizationObject( `bot.commands.${command}.subcommands.${subcommand}.description`,
`bot.commands.${command}.subcommands.${subcommand}.description`, );
);
slashCommand.addSubcommand((option) => slashCommand.addSubcommand((option) =>
option option
.setName(subcommand) .setName(subcommand)
.setNameLocalizations(subcommandNameLocalizations) .setNameLocalizations(subcommandNameLocalizations)
.setDescription( .setDescription(
translate( translate(
`bot.commands.${command}.subcommands.${subcommand}.description`, `bot.commands.${command}.subcommands.${subcommand}.description`,
), ),
) )
.setDescriptionLocalizations(subcommandDescriptionLocalizations), .setDescriptionLocalizations(subcommandDescriptionLocalizations),
); );
}), });
);
} }
return slashCommand; return slashCommand;
+4 -5
View File
@@ -2,11 +2,10 @@ import { Locale } from 'discord.js';
import { locales } from '../localization/strings'; import { locales } from '../localization/strings';
import { translator } from '../localization/translator'; import { translator } from '../localization/translator';
export async function localizationObject(path: string) { export function localizationObject(path: string) {
return ( return locales.reduce<Partial<Record<Locale, string>>>(
await Promise.all(locales.map(async (locale) => await translator(locale))) (localizations, locale) => {
).reduce<Partial<Record<Locale, string>>>( const { translate } = translator(locale);
(localizations, { locale, translate }) => {
return { return {
...localizations, ...localizations,
[locale]: translate(path), [locale]: translate(path),
+11 -14
View File
@@ -9,18 +9,15 @@ export type TranslationFragmentTree = {
}; };
export type TranslationFragment = string | TranslationFragmentTree; export type TranslationFragment = string | TranslationFragmentTree;
export const translations = Promise.all( export const translations = locales
locales.map(async (locale) => ({ .map((locale) => ({
locale, locale,
translations: await import(`../../lang/${locale}.json`), translations: require(`../../lang/${locale}.json`),
})), }))
).then( .reduce(
(translations) => (table, { locale, translations }) => ({
translations.reduce( ...table,
(table, { locale, translations }) => ({ [locale]: translations,
...table, }),
[locale]: translations, {},
}), ) as Record<Locale, TranslationFragment>;
{},
) as Record<Locale, TranslationFragment>,
);
+15 -8
View File
@@ -1,26 +1,33 @@
import { Locale } from 'discord.js'; import { Locale } from 'discord.js';
import { locales, translations } from './strings'; import { locales, translations } from './strings';
export async function translator(localeRaw: Locale) { export function translator(localeRaw: Locale) {
const locale = locales.includes(localeRaw) ? localeRaw : Locale.EnglishUS; const locale = locales.includes(localeRaw) ? localeRaw : Locale.EnglishUS;
const strings = (await translations)[locale]; const strings = translations[locale];
function translate(path: string) { function translate(path: string): string {
const pathArray = path.split('.'); const pathArray = path.split('.');
let fragment = strings; let fragment = strings;
for (const pathItem of pathArray) { for (const pathItem of pathArray) {
if (typeof fragment === 'string') { if (typeof fragment === 'string') {
throw new SyntaxError( throw new SyntaxError(
`Attempted to access sub-string ("${pathItem}" in "${path}") from a string instead of a tree`, `Attempted to access string with key "${pathItem}" in "${path}" from within a string instead of an object in locale "${locale}"`,
); );
} else { } else {
fragment = fragment[pathItem]; fragment = fragment[pathItem];
if (typeof fragment === 'undefined') { if (typeof fragment === 'undefined') {
throw new Error( if (locale === Locale.EnglishUS) {
`Undefined translation for key "${path}" (in "${path}") in language "${locale}"`, throw new Error(
); `Undefined translation at "${pathItem}" in "${path}" for locale "${locale}"`,
);
} else {
console.warn(
`Undefined translation at "${pathItem}" in "${path}" for locale "${locale}"; falling back to en-US`,
);
return translator(Locale.EnglishUS).translate(path);
}
} }
} }
} }
@@ -31,7 +38,7 @@ export async function translator(localeRaw: Locale) {
return fragment.$!; return fragment.$!;
} else { } else {
throw new Error( throw new Error(
`Unresolved tree ending for "${path}" in language "${locale}"`, `Unresolved tree ending for "${path}" in locale "${locale}"`,
); );
} }
} }
+38 -4
View File
@@ -13,6 +13,44 @@
"great": "Great", "great": "Great",
"unicum": "Unicum", "unicum": "Unicum",
"super_unicum": "Super unicum" "super_unicum": "Super unicum"
},
"subcommands": {
"today": {
"$": "today",
"description": "Today"
},
"30": {
"$": "30",
"description": "30 days"
},
"60": {
"$": "60",
"description": "60 days"
},
"90": {
"$": "90",
"description": "90 days"
},
"career": {
"$": "career",
"description": "Career"
},
"custom": {
"$": "custom",
"description": "Custom range",
"options": {
"start": {
"$": "start",
"description": "Start in number of days ago from today"
},
"end": {
"$": "end",
"description": "End in number of days ago from today"
}
}
}
} }
}, },
@@ -58,10 +96,6 @@
"battles": "Battles", "battles": "Battles",
"winrate": "Winrate", "winrate": "Winrate",
"damage": "Damage" "damage": "Damage"
},
"buttons": {
"refresh": "Refresh"
} }
} }
} }