diff --git a/locales/en/bot.json b/locales/en/bot.json index 1102a02..4b84f75 100644 --- a/locales/en/bot.json +++ b/locales/en/bot.json @@ -220,6 +220,54 @@ "unableToReload": "{{icon}} **Oops!**\n_Hmm..._ It seems I am unable to reload the interaction module across shards." }, "remove": { + "metadata": { + "name": "remove", + "description": "Remove tracks from the queue using certain filters.", + "options": { + "track": { + "name": "track", + "description": "Remove a track from the queue by position.", + "options": { + "position": { + "name": "position", + "description": "The position in queue for the track to remove." + } + } + }, + "range": { + "name": "range", + "description": "Remove a range of tracks from the queue.", + "options": { + "start": { + "name": "start", + "description": "The starting position of the range to remove." + }, + "end": { + "name": "end", + "description": "The ending position of the range to remove." + } + } + }, + "queue": { + "name": "queue", + "description": "Remove all tracks from the queue." + }, + "user": { + "name": "user", + "description": "Remove all tracks added by a specific user.", + "options": { + "target": { + "name": "target", + "description": "User whose tracks to remove." + } + } + }, + "duplicates": { + "name": "duplicates", + "description": "Remove all duplicate tracks from the queue." + } + } + }, "noTracksRemoved": "{{icon}} **No tracks removed**\nThere were no tracks removed from the queue. Please check if the option you selected has tracks to remove.", "removedTrack": "{{icon}} **Removed track**", "removedTracks": "{{icon}} **Removed tracks**\n**`{{count}}`** tracks were removed from the queue.", diff --git a/src/common/localeUtil.ts b/src/common/localeUtil.ts index fe055a8..b3e5561 100644 --- a/src/common/localeUtil.ts +++ b/src/common/localeUtil.ts @@ -4,13 +4,14 @@ import { ApplicationCommandOptionType, BaseInteraction, Locale, - SlashCommandBuilder + SlashCommandBuilder, + SlashCommandSubcommandBuilder, + SlashCommandSubcommandsOnlyBuilder } from 'discord.js'; import { lstatSync, readdirSync } from 'fs'; import i18n from 'i18next'; import i18nextFsBackend, { FsBackendOptions } from 'i18next-fs-backend'; import { join } from 'path'; - export const translatorInstance = i18n.createInstance(); const localeDir = join(__dirname, '..', '..', 'locales'); translatorInstance.use(i18nextFsBackend).init({ @@ -42,7 +43,7 @@ export function useUserTranslator(interaction: BaseInteraction) { export const DISCORD_LOCALES = Object.values(Locale); -export type CommandMetadata = { +export type SubcommandMetadata = { name?: string; description?: string; options?: Record< @@ -55,17 +56,31 @@ export type CommandMetadata = { >; }; -type DeepMutable = { -readonly [P in keyof T]: DeepMutable }; -export function localizeCommand(command: Omit) { - const jsonCommand = command as DeepMutable; +export type CommandMetadata = + | SubcommandMetadata + | (SubcommandMetadata & { + options?: Record; + }); - assert(jsonCommand.name, `Command /${command.name} must have a name using setName.`); - const metadataKey = `commands.${jsonCommand.name}.metadata`; +type DeepMutable = { -readonly [P in keyof T]: DeepMutable }; +export function localizeCommand( + command: + | SlashCommandSubcommandBuilder + | SlashCommandSubcommandsOnlyBuilder + | Omit, + resourcePath?: string +) { + const jsonCommand = command as DeepMutable; + const logCommandName = resourcePath ?? `/${command.name}`; + + assert(jsonCommand.name, `Command ${logCommandName} must have a name using setName.`); + const metadataKey = resourcePath ?? `commands.${jsonCommand.name}.metadata`; const englishData = translatorInstance.getResource('en', 'bot', metadataKey) as CommandMetadata | undefined; - assert(englishData, `Command /${command.name} must have English localization data.`); + assert(englishData, `Command ${logCommandName} must have English localization data.`); // Localizing the top-level command... - jsonCommand.description = englishData.description ?? '!MISSING DESCRIPTION!'; + assert(englishData.description, `Command ${logCommandName} is missing an English description.`); + jsonCommand.description = englishData.description; /* eslint-disable camelcase */ jsonCommand.name_localizations = {}; /* eslint-disable camelcase */ @@ -85,13 +100,27 @@ export function localizeCommand(command: Omit 0) { - assert(englishData.options, `Command /${command.name} must have option localizations.`); + assert(englishData.options, `Command ${logCommandName} must have option localizations.`); for (const unwritableOption of jsonCommand.options) { const option = unwritableOption as never as APIApplicationCommandOption; - assert(option.name, `Option in /${command.name} must have a name using setName.`); + // Subcommands are stored in options - they have an 'options' field that always exists (even if empty) on subcommands + if ('options' in option && Array.isArray(option.options)) { + localizeCommand( + unwritableOption as SlashCommandSubcommandBuilder, + `commands.${command.name}.metadata.options.${option.name}` + ); + continue; + } + + assert(option.name, `Option in ${logCommandName} must have a name using setName.`); const englishOptionData = englishData.options[option.name]; - option.description = englishOptionData.description ?? '!MISSING DESCRIPTION!'; + + assert( + englishOptionData.description, + `Option ${option.name} in command ${logCommandName} is missing an English description.` + ); + option.description = englishOptionData.description; option.name_localizations = {}; option.description_localizations = {}; @@ -102,18 +131,18 @@ export function localizeCommand(command: Omit - subcommand - .setName('track') - .setDescription('Remove a track from the queue by position') - .addIntegerOption((option) => - option - .setName('position') - .setDescription('The position in queue for track to remove.') - .setMinValue(1) - .setRequired(true) - ) - ) - .addSubcommand((subcommand) => - subcommand - .setName('range') - .setDescription('Remove a range of tracks from the queue') - .addIntegerOption((option) => - option - .setName('start') - .setDescription('The starting position of the range to remove') - .setMinValue(1) - .setRequired(true) - ) - .addIntegerOption((option) => - option - .setName('end') - .setDescription('The ending position of the range to remove') - .setMinValue(1) - .setRequired(true) - ) - ) - .addSubcommand((subcommand) => - subcommand.setName('queue').setDescription('Remove all tracks from the queue') - ) - .addSubcommand((subcommand) => - subcommand - .setName('user') - .setDescription('Remove all tracks from a specific user') - .addUserOption((option) => - option.setName('target').setDescription('User to remove tracks for').setRequired(true) - ) - ) - .addSubcommand((subcommand) => - subcommand.setName('duplicates').setDescription('Remove all duplicate tracks from the queue') - ); + const data = localizeCommand( + new SlashCommandBuilder() + .setName('remove') + .addSubcommand((subcommand) => + subcommand + .setName('track') + .addIntegerOption((option) => option.setName('position').setMinValue(1).setRequired(true)) + ) + .addSubcommand((subcommand) => + subcommand + .setName('range') + .addIntegerOption((option) => option.setName('start').setMinValue(1).setRequired(true)) + .addIntegerOption((option) => option.setName('end').setMinValue(1).setRequired(true)) + ) + .addSubcommand((subcommand) => subcommand.setName('queue')) + .addSubcommand((subcommand) => + subcommand.setName('user').addUserOption((option) => option.setName('target').setRequired(true)) + ) + .addSubcommand((subcommand) => subcommand.setName('duplicates')) + ); super(data); }