Merge pull request #331 from tacheometry/main

Subcommand localization
This commit is contained in:
Marius Begby authored and GitHub committed 2023-12-21 22:43:12 +01:00
commit 3d8a74a5e8
3 files changed
+102 -54

No files matched your search

+48
View File
@@ -220,6 +220,54 @@
"unableToReload": "{{icon}} **Oops!**\n_Hmm..._ It seems I am unable to reload the interaction module across shards." "unableToReload": "{{icon}} **Oops!**\n_Hmm..._ It seems I am unable to reload the interaction module across shards."
}, },
"remove": { "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.", "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**", "removedTrack": "{{icon}} **Removed track**",
"removedTracks": "{{icon}} **Removed tracks**\n**`{{count}}`** tracks were removed from the queue.", "removedTracks": "{{icon}} **Removed tracks**\n**`{{count}}`** tracks were removed from the queue.",
+45 -16
View File
@@ -4,13 +4,14 @@ import {
ApplicationCommandOptionType, ApplicationCommandOptionType,
BaseInteraction, BaseInteraction,
Locale, Locale,
SlashCommandBuilder SlashCommandBuilder,
SlashCommandSubcommandBuilder,
SlashCommandSubcommandsOnlyBuilder
} from 'discord.js'; } from 'discord.js';
import { lstatSync, readdirSync } from 'fs'; import { lstatSync, readdirSync } from 'fs';
import i18n from 'i18next'; import i18n from 'i18next';
import i18nextFsBackend, { FsBackendOptions } from 'i18next-fs-backend'; import i18nextFsBackend, { FsBackendOptions } from 'i18next-fs-backend';
import { join } from 'path'; import { join } from 'path';
export const translatorInstance = i18n.createInstance(); export const translatorInstance = i18n.createInstance();
const localeDir = join(__dirname, '..', '..', 'locales'); const localeDir = join(__dirname, '..', '..', 'locales');
translatorInstance.use(i18nextFsBackend).init<FsBackendOptions>({ translatorInstance.use(i18nextFsBackend).init<FsBackendOptions>({
@@ -42,7 +43,7 @@ export function useUserTranslator(interaction: BaseInteraction) {
export const DISCORD_LOCALES = Object.values(Locale); export const DISCORD_LOCALES = Object.values(Locale);
export type CommandMetadata = { export type SubcommandMetadata = {
name?: string; name?: string;
description?: string; description?: string;
options?: Record< options?: Record<
@@ -55,17 +56,31 @@ export type CommandMetadata = {
>; >;
}; };
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }; export type CommandMetadata =
export function localizeCommand(command: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>) { | SubcommandMetadata
const jsonCommand = command as DeepMutable<typeof command>; | (SubcommandMetadata & {
options?: Record<string, SubcommandMetadata>;
});
assert(jsonCommand.name, `Command /${command.name} must have a name using setName.`); type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> };
const metadataKey = `commands.${jsonCommand.name}.metadata`; export function localizeCommand(
command:
| SlashCommandSubcommandBuilder
| SlashCommandSubcommandsOnlyBuilder
| Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>,
resourcePath?: string
) {
const jsonCommand = command as DeepMutable<typeof command>;
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; 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... // 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 */ /* eslint-disable camelcase */
jsonCommand.name_localizations = {}; jsonCommand.name_localizations = {};
/* eslint-disable camelcase */ /* eslint-disable camelcase */
@@ -85,13 +100,27 @@ export function localizeCommand(command: Omit<SlashCommandBuilder, 'addSubcomman
// Localizing the command options... // Localizing the command options...
if (jsonCommand.options.length > 0) { if (jsonCommand.options.length > 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) { for (const unwritableOption of jsonCommand.options) {
const option = unwritableOption as never as APIApplicationCommandOption; 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]; 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.name_localizations = {};
option.description_localizations = {}; option.description_localizations = {};
@@ -102,18 +131,18 @@ export function localizeCommand(command: Omit<SlashCommandBuilder, 'addSubcomman
) { ) {
assert( assert(
englishOptionData.choices, englishOptionData.choices,
`Option ${option.name} in /${command.name} must have choice localizations.` `Option ${option.name} in ${logCommandName} must have choice localizations.`
); );
for (const choice of option.choices) { for (const choice of option.choices) {
choice.name_localizations = {}; choice.name_localizations = {};
assert( assert(
choice.value !== undefined && choice.value !== null, choice.value !== undefined && choice.value !== null,
`Choice in option ${option.name} in /${command.name} must have a value.` `Choice in option ${option.name} in ${logCommandName} must have a value.`
); );
const stringifiedValue = choice.value.toString(); const stringifiedValue = choice.value.toString();
assert( assert(
englishOptionData.choices[stringifiedValue], englishOptionData.choices[stringifiedValue],
`Choice ${choice.value} of option ${option.name} in /${command.name} must have an English localization.` `Choice ${choice.value} of option ${option.name} in ${logCommandName} must have an English localization.`
); );
choice.name = englishOptionData.choices[stringifiedValue]; choice.name = englishOptionData.choices[stringifiedValue];
} }
+9 -38
View File
@@ -6,59 +6,30 @@ import { checkQueueExists } from '../../../utils/validation/queueValidator';
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { Logger } from 'pino'; import { Logger } from 'pino';
import { TFunction } from 'i18next'; import { TFunction } from 'i18next';
import { useServerTranslator } from '../../../common/localeUtil'; import { localizeCommand, useServerTranslator } from '../../../common/localeUtil';
import { formatSlashCommand } from '../../../common/formattingUtils'; import { formatSlashCommand } from '../../../common/formattingUtils';
class RemoveCommand extends BaseSlashCommandInteraction { class RemoveCommand extends BaseSlashCommandInteraction {
constructor() { constructor() {
// TODO: Add subcommand localization support const data = localizeCommand(
const data = new SlashCommandBuilder() new SlashCommandBuilder()
.setName('remove') .setName('remove')
.setDescription('Remove tracks from the queue')
.addSubcommand((subcommand) => .addSubcommand((subcommand) =>
subcommand subcommand
.setName('track') .setName('track')
.setDescription('Remove a track from the queue by position') .addIntegerOption((option) => option.setName('position').setMinValue(1).setRequired(true))
.addIntegerOption((option) =>
option
.setName('position')
.setDescription('The position in queue for track to remove.')
.setMinValue(1)
.setRequired(true)
)
) )
.addSubcommand((subcommand) => .addSubcommand((subcommand) =>
subcommand subcommand
.setName('range') .setName('range')
.setDescription('Remove a range of tracks from the queue') .addIntegerOption((option) => option.setName('start').setMinValue(1).setRequired(true))
.addIntegerOption((option) => .addIntegerOption((option) => option.setName('end').setMinValue(1).setRequired(true))
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'))
.addSubcommand((subcommand) => .addSubcommand((subcommand) =>
subcommand.setName('queue').setDescription('Remove all tracks from the queue') subcommand.setName('user').addUserOption((option) => option.setName('target').setRequired(true))
) )
.addSubcommand((subcommand) => .addSubcommand((subcommand) => subcommand.setName('duplicates'))
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')
); );
super(data); super(data);
} }