js-discord-modularcommand 3.1.0 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/registercommand.js +41 -7
- package/dist/types.d.ts +1 -3
- package/package.json +1 -1
package/dist/registercommand.js
CHANGED
|
@@ -50,35 +50,64 @@ const cooldown_1 = __importStar(require("./cooldown"));
|
|
|
50
50
|
const locales_1 = require("./locales");
|
|
51
51
|
/**
|
|
52
52
|
* @description Gets localized description for a subcommand.
|
|
53
|
+
* Supports multiple languages and falls back to default description if not found.
|
|
53
54
|
* @param {ModularCommand} command The command instance.
|
|
54
55
|
* @param {string} subCommandName The name of the subcommand.
|
|
55
56
|
* @param {string} defaultDescription The default description.
|
|
57
|
+
* @param {string} locale The locale to use (defaults to EnglishUS).
|
|
56
58
|
* @returns {string} The localized description.
|
|
57
59
|
*/
|
|
58
|
-
function getLocalizedSubCommandDescription(command, subCommandName, defaultDescription) {
|
|
60
|
+
function getLocalizedSubCommandDescription(command, subCommandName, defaultDescription, locale = discord_js_1.Locale.EnglishUS) {
|
|
59
61
|
if (!command.subCommandLocalizations)
|
|
60
62
|
return defaultDescription;
|
|
61
63
|
const localizations = command.subCommandLocalizations;
|
|
64
|
+
const key = `${subCommandName}.description`;
|
|
65
|
+
// Try to get the localization for the requested locale
|
|
66
|
+
const targetLocalizations = localizations[locale];
|
|
67
|
+
if (targetLocalizations?.[key]) {
|
|
68
|
+
return targetLocalizations[key];
|
|
69
|
+
}
|
|
70
|
+
// Fall back to English if the requested locale is not found
|
|
62
71
|
const enLocalizations = localizations[discord_js_1.Locale.EnglishUS];
|
|
63
|
-
|
|
72
|
+
if (enLocalizations?.[key]) {
|
|
73
|
+
return enLocalizations[key];
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
throw new Error(`Missing localization for subcommand '${subCommandName}' in command '${command.name}'`);
|
|
77
|
+
}
|
|
64
78
|
}
|
|
65
79
|
/**
|
|
66
80
|
* @description Gets localized description for a subcommand option.
|
|
81
|
+
* Supports multiple languages and falls back to default description if not found.
|
|
67
82
|
* @param {ModularCommand} command The command instance.
|
|
68
83
|
* @param {string} subCommandName The name of the subcommand.
|
|
69
84
|
* @param {string} optionName The name of the option.
|
|
70
85
|
* @param {string} defaultDescription The default description.
|
|
86
|
+
* @param {string} locale The locale to use (defaults to EnglishUS).
|
|
71
87
|
* @returns {string} The localized description.
|
|
72
88
|
*/
|
|
73
|
-
function getLocalizedOptionDescription(command, subCommandName, optionName, defaultDescription) {
|
|
89
|
+
function getLocalizedOptionDescription(command, subCommandName, optionName, defaultDescription, locale = discord_js_1.Locale.EnglishUS) {
|
|
74
90
|
if (!command.subCommandLocalizations)
|
|
75
91
|
return defaultDescription;
|
|
76
92
|
const localizations = command.subCommandLocalizations;
|
|
93
|
+
const key = `${subCommandName}.${optionName}.description`;
|
|
94
|
+
// Try to get the localization for the requested locale
|
|
95
|
+
const targetLocalizations = localizations[locale];
|
|
96
|
+
if (targetLocalizations?.[key]) {
|
|
97
|
+
return targetLocalizations[key];
|
|
98
|
+
}
|
|
99
|
+
// Fall back to English if the requested locale is not found
|
|
77
100
|
const enLocalizations = localizations[discord_js_1.Locale.EnglishUS];
|
|
78
|
-
|
|
101
|
+
if (enLocalizations?.[key]) {
|
|
102
|
+
return enLocalizations[key];
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
throw new Error(`Missing localization for option '${optionName}' in subcommand '${subCommandName}' for command '${command.name}'`);
|
|
106
|
+
}
|
|
79
107
|
}
|
|
80
108
|
/**
|
|
81
109
|
* @description Creates an option builder function with common configuration.
|
|
110
|
+
* Supports multi-language descriptions through LocalizationMap.
|
|
82
111
|
* @param {CommandOption} opt The option configuration.
|
|
83
112
|
* @param {string} description The resolved description.
|
|
84
113
|
* @returns {Function} The option builder function.
|
|
@@ -140,6 +169,9 @@ function processSubCommands(commandBuilder, command, options) {
|
|
|
140
169
|
return;
|
|
141
170
|
command.subCommands.forEach(subCmd => {
|
|
142
171
|
commandBuilder.addSubcommand((subcommand) => {
|
|
172
|
+
if (typeof subCmd.name !== 'string' || subCmd.name.trim() === '') {
|
|
173
|
+
throw new Error("A subcommand is missing a name.");
|
|
174
|
+
}
|
|
143
175
|
// Get localized description for subcommand
|
|
144
176
|
const subCmdDescription = getLocalizedSubCommandDescription(command, subCmd.name, subCmd.description);
|
|
145
177
|
if (typeof subCmdDescription !== 'string' || subCmdDescription.trim() === '') {
|
|
@@ -222,7 +254,7 @@ function getCommandLocale(command, interaction) {
|
|
|
222
254
|
function createChatInputExecutor(command, options) {
|
|
223
255
|
return async (interaction) => {
|
|
224
256
|
// Permission & NSFW Checks
|
|
225
|
-
if (command.permissionCheck && !command.permissionCheck(
|
|
257
|
+
if (command.permissionCheck && !command.permissionCheck(interaction)) {
|
|
226
258
|
await interaction.reply({ content: locales_1.LOCALE_FORBIDDEN[interaction.locale], flags: discord_js_1.MessageFlags.Ephemeral });
|
|
227
259
|
return;
|
|
228
260
|
}
|
|
@@ -381,10 +413,12 @@ function createSelectMenuExecutor(command) {
|
|
|
381
413
|
function RegisterCommand(commands) {
|
|
382
414
|
commands = Array.isArray(commands) ? commands : [commands];
|
|
383
415
|
return commands.map(command => {
|
|
384
|
-
if (command.name ===
|
|
416
|
+
if (typeof command.name !== 'string' || command.name.trim() === '') {
|
|
385
417
|
throw new Error("A command is missing a name.");
|
|
386
|
-
|
|
418
|
+
}
|
|
419
|
+
if (typeof command.description !== 'string' || command.description.trim() === '') {
|
|
387
420
|
throw new Error(`Command "${command.name}" is missing a description.`);
|
|
421
|
+
}
|
|
388
422
|
// Build SlashCommand Data
|
|
389
423
|
const commandBuilder = new discord_js_1.SlashCommandBuilder()
|
|
390
424
|
.setName(command.name)
|
package/dist/types.d.ts
CHANGED
|
@@ -163,6 +163,4 @@ export type SelectMenuExecuteFunction = (params: SelectMenuExecuteParams) => Pro
|
|
|
163
163
|
* @description Defines the signature for a function that checks a user's permissions to execute a command.
|
|
164
164
|
* @returns {boolean | Promise<boolean>} `true` if the user has permission, `false` otherwise.
|
|
165
165
|
*/
|
|
166
|
-
export type PermissionCheckFunction = (
|
|
167
|
-
interaction: CommandInteraction;
|
|
168
|
-
}) => boolean | Promise<boolean>;
|
|
166
|
+
export type PermissionCheckFunction = (interaction: CommandInteraction) => boolean | Promise<boolean>;
|