mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-02 14:38:49 +00:00
3 files changed
+114
-66
No files matched your search
@@ -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.",
|
||||
|
||||
+45
-16
@@ -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<FsBackendOptions>({
|
||||
@@ -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<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> };
|
||||
export function localizeCommand(command: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>) {
|
||||
const jsonCommand = command as DeepMutable<typeof command>;
|
||||
export type CommandMetadata =
|
||||
| SubcommandMetadata
|
||||
| (SubcommandMetadata & {
|
||||
options?: Record<string, SubcommandMetadata>;
|
||||
});
|
||||
|
||||
assert(jsonCommand.name, `Command /${command.name} must have a name using setName.`);
|
||||
const metadataKey = `commands.${jsonCommand.name}.metadata`;
|
||||
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> };
|
||||
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;
|
||||
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<SlashCommandBuilder, 'addSubcomman
|
||||
|
||||
// Localizing the command options...
|
||||
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) {
|
||||
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<SlashCommandBuilder, 'addSubcomman
|
||||
) {
|
||||
assert(
|
||||
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) {
|
||||
choice.name_localizations = {};
|
||||
assert(
|
||||
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();
|
||||
assert(
|
||||
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];
|
||||
}
|
||||
|
||||
@@ -6,60 +6,31 @@ import { checkQueueExists } from '../../../utils/validation/queueValidator';
|
||||
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
|
||||
import { Logger } from 'pino';
|
||||
import { TFunction } from 'i18next';
|
||||
import { useServerTranslator } from '../../../common/localeUtil';
|
||||
import { localizeCommand, useServerTranslator } from '../../../common/localeUtil';
|
||||
import { formatSlashCommand } from '../../../common/formattingUtils';
|
||||
|
||||
class RemoveCommand extends BaseSlashCommandInteraction {
|
||||
constructor() {
|
||||
// TODO: Add subcommand localization support
|
||||
const data = new SlashCommandBuilder()
|
||||
.setName('remove')
|
||||
.setDescription('Remove tracks from the queue')
|
||||
.addSubcommand((subcommand) =>
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user