js-discord-modularcommand 3.1.0 → 3.2.0

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.
@@ -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
- return enLocalizations?.[`${subCommandName}.description`] || defaultDescription;
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
- return enLocalizations?.[`${subCommandName}.${optionName}.description`] || defaultDescription;
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() === '') {
@@ -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 === undefined)
416
+ if (typeof command.name !== 'string' || command.name.trim() === '') {
385
417
  throw new Error("A command is missing a name.");
386
- if (command.description === undefined)
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-discord-modularcommand",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "discord",