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

+91 -20
View File
@@ -1,49 +1,120 @@
import {
Locale,
SlashCommandBuilder,
SlashCommandSubcommandBuilder,
SlashCommandSubcommandGroupBuilder,
} from 'discord.js';
import { translator } from '../localization/translator';
import { localizationObject } from './localizationObject';
export type PeriodType = 'today' | '30' | '60' | '90' | 'career' | 'custom';
export type PeriodSize = 'today' | `${number}` | 'career';
export default function addPeriodSubCommands(
export function addPeriodSubCommands(
option: SlashCommandBuilder | SlashCommandSubcommandGroupBuilder,
extra: (
option: SlashCommandSubcommandBuilder,
) => 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
.addSubcommand((option) =>
extra(option.setName('today').setDescription('Today')),
)
.addSubcommand((option) =>
extra(option.setName('30').setDescription('30 days')),
)
.addSubcommand((option) =>
extra(option.setName('60').setDescription('60 days')),
)
.addSubcommand((option) =>
extra(option.setName('90').setDescription('90 days')),
)
.addSubcommand((option) =>
extra(option.setName('career').setDescription('Career')),
extra(
option
.setName(translate('bot.common.subcommands.today'))
.setNameLocalizations(subcommandNameLocalizations.today)
.setDescription(translate('bot.common.subcommands.today.description'))
.setDescriptionLocalizations(
subcommandDescriptionLocalizations.today,
),
),
)
.addSubcommand((option) =>
extra(
option
.setName('custom')
.setDescription('Custom range')
.setName(translate('bot.common.subcommands.30'))
.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) =>
option
.setName('start')
.setDescription('Start in number of days ago from today')
.setName(translate('bot.common.subcommands.custom.options.start'))
.setDescription(
translate(
'bot.common.subcommands.custom.options.start.description',
),
)
.setRequired(true),
)
.addIntegerOption((option) =>
option
.setName('end')
.setDescription('End in number of days')
.setName(translate('bot.common.subcommands.custom.options.end'))
.setDescription(
translate(
'bot.common.subcommands.custom.options.end.description',
),
)
.setRequired(true),
),
),
+1 -2
View File
@@ -5,7 +5,7 @@ import {
} from 'discord.js';
import { encyclopediaInfo } from '../blitz/encyclopediaInfo';
import { addFilterOptions } from './addFilterOptions';
import addPeriodSubCommands from './addPeriodSubCommands';
import { addPeriodSubCommands } from './addPeriodSubCommands';
export default async function addPeriodicFilterOptions<
OptionType extends SlashCommandBuilder | SlashCommandSubcommandGroupBuilder,
@@ -16,7 +16,6 @@ export default async function addPeriodicFilterOptions<
) => SlashCommandSubcommandBuilder = (option) => option,
): Promise<OptionType> {
const awaitedEncyclopediaInfo = await encyclopediaInfo;
addPeriodSubCommands(option, (option) => {
return addFilterOptions(extra(option), awaitedEncyclopediaInfo);
});
+23 -25
View File
@@ -6,42 +6,40 @@ interface CreateLocalizedCommandSubcommand {
subcommand: string;
}
export async function createLocalizedCommand(
export function createLocalizedCommand(
command: string,
subcommands?: CreateLocalizedCommandSubcommand[],
) {
const { translate } = await translator(Locale.EnglishUS);
const { translate } = translator(Locale.EnglishUS);
const slashCommand = new SlashCommandBuilder()
.setName(command)
.setNameLocalizations(await localizationObject(`bot.commands.${command}`))
.setNameLocalizations(localizationObject(`bot.commands.${command}`))
.setDescription(translate(`bot.commands.${command}.description`))
.setDescriptionLocalizations(
await localizationObject(`bot.commands.${command}.description`),
localizationObject(`bot.commands.${command}.description`),
);
if (subcommands) {
await Promise.all(
subcommands.map(async ({ subcommand }) => {
const subcommandNameLocalizations = await localizationObject(
`bot.commands.${command}.subcommands.${subcommand}`,
);
const subcommandDescriptionLocalizations = await localizationObject(
`bot.commands.${command}.subcommands.${subcommand}.description`,
);
subcommands.map(({ subcommand }) => {
const subcommandNameLocalizations = localizationObject(
`bot.commands.${command}.subcommands.${subcommand}`,
);
const subcommandDescriptionLocalizations = localizationObject(
`bot.commands.${command}.subcommands.${subcommand}.description`,
);
slashCommand.addSubcommand((option) =>
option
.setName(subcommand)
.setNameLocalizations(subcommandNameLocalizations)
.setDescription(
translate(
`bot.commands.${command}.subcommands.${subcommand}.description`,
),
)
.setDescriptionLocalizations(subcommandDescriptionLocalizations),
);
}),
);
slashCommand.addSubcommand((option) =>
option
.setName(subcommand)
.setNameLocalizations(subcommandNameLocalizations)
.setDescription(
translate(
`bot.commands.${command}.subcommands.${subcommand}.description`,
),
)
.setDescriptionLocalizations(subcommandDescriptionLocalizations),
);
});
}
return slashCommand;
+4 -5
View File
@@ -2,11 +2,10 @@ import { Locale } from 'discord.js';
import { locales } from '../localization/strings';
import { translator } from '../localization/translator';
export async function localizationObject(path: string) {
return (
await Promise.all(locales.map(async (locale) => await translator(locale)))
).reduce<Partial<Record<Locale, string>>>(
(localizations, { locale, translate }) => {
export function localizationObject(path: string) {
return locales.reduce<Partial<Record<Locale, string>>>(
(localizations, locale) => {
const { translate } = translator(locale);
return {
...localizations,
[locale]: translate(path),
+11 -14
View File
@@ -9,18 +9,15 @@ export type TranslationFragmentTree = {
};
export type TranslationFragment = string | TranslationFragmentTree;
export const translations = Promise.all(
locales.map(async (locale) => ({
export const translations = locales
.map((locale) => ({
locale,
translations: await import(`../../lang/${locale}.json`),
})),
).then(
(translations) =>
translations.reduce(
(table, { locale, translations }) => ({
...table,
[locale]: translations,
}),
{},
) as Record<Locale, TranslationFragment>,
);
translations: require(`../../lang/${locale}.json`),
}))
.reduce(
(table, { locale, translations }) => ({
...table,
[locale]: translations,
}),
{},
) as Record<Locale, TranslationFragment>;
+15 -8
View File
@@ -1,26 +1,33 @@
import { Locale } from 'discord.js';
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 strings = (await translations)[locale];
const strings = translations[locale];
function translate(path: string) {
function translate(path: string): string {
const pathArray = path.split('.');
let fragment = strings;
for (const pathItem of pathArray) {
if (typeof fragment === 'string') {
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 {
fragment = fragment[pathItem];
if (typeof fragment === 'undefined') {
throw new Error(
`Undefined translation for key "${path}" (in "${path}") in language "${locale}"`,
);
if (locale === Locale.EnglishUS) {
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.$!;
} else {
throw new Error(
`Unresolved tree ending for "${path}" in language "${locale}"`,
`Unresolved tree ending for "${path}" in locale "${locale}"`,
);
}
}