js-discord-modularcommand 2.0.0 → 2.0.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.
@@ -1,54 +1,69 @@
1
1
  /**
2
- * @module ModularCommand
3
- * @description A module for creating and managing modular commands in a easy way for me.
2
+ * @file Contains the main class for creating modular commands.
3
+ * @author vicentefelipechile
4
4
  * @license MIT
5
5
  */
6
- /**
7
- * Imports
8
- */
9
6
  import { LocalizationMap, ButtonStyle, Locale } from 'discord.js';
10
7
  import ModularModal from './modularmodal.js';
11
8
  import ModularButton from './modularbutton.js';
12
9
  import { ButtonExecuteFunction, CommandExecuteFunction, CommandOption, ComponentExecuteFunction, ModalExecuteFunction, PermissionCheckFunction } from './types.js';
13
10
  /**
14
11
  * @description Represents a modular command that can be registered with Discord.js.
15
- * It allows for dynamic command creation and execution.
12
+ * It allows for dynamic command creation and execution in a simple way.
16
13
  * @example
17
14
  * const { ModularCommand, RegisterCommand } = require('js-discord-modularcommand');
18
15
  *
19
16
  * const PingCommand = new ModularCommand('ping');
20
17
  * PingCommand.setDescription('Sends a ping message.');
21
18
  * PingCommand.setExecute(async ({interaction}) => {
22
- * await interaction.reply('Pong!');
19
+ * await interaction.reply('Pong!');
23
20
  * });
24
21
  *
25
22
  * PingCommand.setPermissionCheck(({ interaction }) => {
26
- * return interaction.member.permissions.has(PermissionFlagsBits.Administrator);
23
+ * return interaction.member.permissions.has(PermissionFlagsBits.Administrator);
27
24
  * });
28
25
  *
29
26
  * module.exports = RegisterCommand([
30
- * PingCommand
27
+ * PingCommand
31
28
  * ]);
32
29
  */
33
30
  export default class ModularCommand {
31
+ /** The name of the command, must be unique. */
34
32
  name: string;
33
+ /** The main description of the command. */
35
34
  description: string;
35
+ /** A map of localizations for the command's description. */
36
+ descriptionLocalizations?: LocalizationMap;
37
+ /** The options (arguments) that the command accepts. */
38
+ options: CommandOption[];
39
+ /** An object containing localizations for the names and descriptions of the options. */
40
+ optionsLocalizations: Record<string, Record<Locale, string>>;
41
+ /** The main function that executes when the command is invoked. */
36
42
  execute: CommandExecuteFunction;
43
+ /** (Optional) The function to handle button interactions. */
37
44
  buttonExecute?: ButtonExecuteFunction;
45
+ /** (Optional) The function to handle generic component interactions (like select menus). */
38
46
  componentExecute?: ComponentExecuteFunction;
47
+ /** (Optional) The function to handle modal submissions. */
39
48
  modalExecute?: ModalExecuteFunction;
40
- options: CommandOption[];
41
- optionsLocalizations: Record<string, Record<Locale, string>>;
49
+ /** A record of handlers for specific component custom IDs. */
42
50
  customIdHandlers: Record<string, CommandExecuteFunction>;
51
+ /** The command's cooldown time in seconds. */
43
52
  cooldown: number;
44
- modals: Map<string, ModularModal>;
45
- buttons: Map<string, ModularButton>;
46
- buttonsArray: ModularButton[];
53
+ /** Whether the command is marked as Not Safe For Work (NSFW). */
47
54
  isNSFW: boolean;
48
- descriptionLocalizations?: LocalizationMap;
55
+ /** (Optional) An object with localized phrases to be used within the command's execution. */
49
56
  localizationPhrases?: Record<Locale, string>;
57
+ /** (Optional) The function that checks if a user has permission to execute the command. */
50
58
  permissionCheck?: PermissionCheckFunction;
59
+ /** (Optional) The base ID for components associated with this command. */
51
60
  componentId?: string;
61
+ /** A map of modals associated with this command, keyed by modal ID. */
62
+ modals: Map<string, ModularModal>;
63
+ /** A map of buttons associated with this command, keyed by the button's custom ID. */
64
+ buttons: Map<string, ModularButton>;
65
+ /** An array containing all ModularButton instances for easy access. */
66
+ buttonsArray: ModularButton[];
52
67
  constructor(name: string);
53
68
  /**
54
69
  * Sets the description of the command.
@@ -58,10 +73,15 @@ export default class ModularCommand {
58
73
  setDescription(description: string): this;
59
74
  /**
60
75
  * Sets the description localizations for the command.
61
- * @param {LocalizationMap} localizations The description localizations.
76
+ * @param {LocalizationMap} localizations The description localizations map.
62
77
  * @returns {ModularCommand} The command instance for chaining.
63
78
  */
64
79
  setLocalizationsDescription(localizations: LocalizationMap): this;
80
+ /**
81
+ * Sets the localizations for the command's options.
82
+ * @param {Record<string, Record<Locale, string>>} localizations An object with the localizations.
83
+ * @returns {ModularCommand} The command instance for chaining.
84
+ */
65
85
  setLocalizationOptions(localizations: Record<string, Record<Locale, string>>): this;
66
86
  /**
67
87
  * Sets the localization phrases for the command.
@@ -83,7 +103,7 @@ export default class ModularCommand {
83
103
  */
84
104
  setComponentExecute(componentId: string, executeFunction: ComponentExecuteFunction): this;
85
105
  /**
86
- * Set the minimun permissions required to execute the command.
106
+ * Sets the minimum permissions required to execute the command.
87
107
  * @param {PermissionCheckFunction} permissionCheckFunction The function to check permissions.
88
108
  * @returns {ModularCommand} The command instance for chaining.
89
109
  */
@@ -101,14 +121,14 @@ export default class ModularCommand {
101
121
  getComponentId(): string | undefined;
102
122
  /**
103
123
  * Adds an option to the command.
104
- * @param {CommandOption} option The option for the command option.
124
+ * @param {CommandOption} option The option for the command.
105
125
  * @returns {ModularCommand} The command instance for chaining.
106
126
  */
107
127
  addOption(option: CommandOption): this;
108
128
  /**
109
129
  * Adds a custom ID handler for the command.
110
130
  * @param {string} customId The custom ID to match.
111
- * @param {CommandInteraction<CacheType>} handlerFunction The function to execute when the custom ID matches.
131
+ * @param {CommandExecuteFunction} handlerFunction The function to execute when the custom ID matches.
112
132
  * @returns {ModularCommand} The command instance for chaining.
113
133
  */
114
134
  addCustomIDHandler(customId: string, handlerFunction: CommandExecuteFunction): this;
@@ -1,54 +1,40 @@
1
1
  "use strict";
2
2
  /**
3
- * @module ModularCommand
4
- * @description A module for creating and managing modular commands in a easy way for me.
3
+ * @file Contains the main class for creating modular commands.
4
+ * @author vicentefelipechile
5
5
  * @license MIT
6
6
  */
7
7
  var __importDefault = (this && this.__importDefault) || function (mod) {
8
8
  return (mod && mod.__esModule) ? mod : { "default": mod };
9
9
  };
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
- /**
12
- * Imports
13
- */
11
+ // =================================================================================================
12
+ // Imports
13
+ // =================================================================================================
14
14
  const discord_js_1 = require("discord.js");
15
15
  const modularmodal_js_1 = __importDefault(require("./modularmodal.js"));
16
16
  const modularbutton_js_1 = __importDefault(require("./modularbutton.js"));
17
- /**
18
- * Interface
19
- */
20
- /**
21
- * @description Represents a command option for a modular command.
22
- */
23
- /**
24
- * Variables
25
- */
26
- const ALLOWED_OPTION_TYPE = [
27
- discord_js_1.ApplicationCommandOptionType.String,
28
- discord_js_1.ApplicationCommandOptionType.Boolean,
29
- discord_js_1.ApplicationCommandOptionType.Integer,
30
- discord_js_1.ApplicationCommandOptionType.Number,
31
- discord_js_1.ApplicationCommandOptionType.User,
32
- discord_js_1.ApplicationCommandOptionType.Channel,
33
- ];
17
+ // =================================================================================================
18
+ // Class: ModularCommand
19
+ // =================================================================================================
34
20
  /**
35
21
  * @description Represents a modular command that can be registered with Discord.js.
36
- * It allows for dynamic command creation and execution.
22
+ * It allows for dynamic command creation and execution in a simple way.
37
23
  * @example
38
24
  * const { ModularCommand, RegisterCommand } = require('js-discord-modularcommand');
39
25
  *
40
26
  * const PingCommand = new ModularCommand('ping');
41
27
  * PingCommand.setDescription('Sends a ping message.');
42
28
  * PingCommand.setExecute(async ({interaction}) => {
43
- * await interaction.reply('Pong!');
29
+ * await interaction.reply('Pong!');
44
30
  * });
45
31
  *
46
32
  * PingCommand.setPermissionCheck(({ interaction }) => {
47
- * return interaction.member.permissions.has(PermissionFlagsBits.Administrator);
33
+ * return interaction.member.permissions.has(PermissionFlagsBits.Administrator);
48
34
  * });
49
35
  *
50
36
  * module.exports = RegisterCommand([
51
- * PingCommand
37
+ * PingCommand
52
38
  * ]);
53
39
  */
54
40
  class ModularCommand {
@@ -78,7 +64,7 @@ class ModularCommand {
78
64
  }
79
65
  /**
80
66
  * Sets the description localizations for the command.
81
- * @param {LocalizationMap} localizations The description localizations.
67
+ * @param {LocalizationMap} localizations The description localizations map.
82
68
  * @returns {ModularCommand} The command instance for chaining.
83
69
  */
84
70
  setLocalizationsDescription(localizations) {
@@ -86,6 +72,11 @@ class ModularCommand {
86
72
  this.descriptionLocalizations = localizations;
87
73
  return this;
88
74
  }
75
+ /**
76
+ * Sets the localizations for the command's options.
77
+ * @param {Record<string, Record<Locale, string>>} localizations An object with the localizations.
78
+ * @returns {ModularCommand} The command instance for chaining.
79
+ */
89
80
  setLocalizationOptions(localizations) {
90
81
  this.optionsLocalizations = localizations;
91
82
  return this;
@@ -120,7 +111,7 @@ class ModularCommand {
120
111
  return this;
121
112
  }
122
113
  /**
123
- * Set the minimun permissions required to execute the command.
114
+ * Sets the minimum permissions required to execute the command.
124
115
  * @param {PermissionCheckFunction} permissionCheckFunction The function to check permissions.
125
116
  * @returns {ModularCommand} The command instance for chaining.
126
117
  */
@@ -146,20 +137,17 @@ class ModularCommand {
146
137
  }
147
138
  /**
148
139
  * Adds an option to the command.
149
- * @param {CommandOption} option The option for the command option.
140
+ * @param {CommandOption} option The option for the command.
150
141
  * @returns {ModularCommand} The command instance for chaining.
151
142
  */
152
143
  addOption(option) {
153
- if (!ALLOWED_OPTION_TYPE.includes(option.type)) {
154
- throw new Error(`Invalid option type: ${option.type}. Allowed types are: ${ALLOWED_OPTION_TYPE.join(', ')}`);
155
- }
156
144
  this.options.push(option);
157
145
  return this;
158
146
  }
159
147
  /**
160
148
  * Adds a custom ID handler for the command.
161
149
  * @param {string} customId The custom ID to match.
162
- * @param {CommandInteraction<CacheType>} handlerFunction The function to execute when the custom ID matches.
150
+ * @param {CommandExecuteFunction} handlerFunction The function to execute when the custom ID matches.
163
151
  * @returns {ModularCommand} The command instance for chaining.
164
152
  */
165
153
  addCustomIDHandler(customId, handlerFunction) {
@@ -11,4 +11,4 @@ import ModularCommand from "./modularcommand";
11
11
  * @param {ModularCommand[]} commands An array of ModularCommand instances.
12
12
  * @returns {CommandData[]} An array of command data objects ready for the Discord.js client.
13
13
  */
14
- export default function RegisterCommand(commands: ModularCommand[]): CommandData[];
14
+ export default function RegisterCommand(commands: ModularCommand[] | ModularCommand): CommandData[];
@@ -109,13 +109,7 @@ function createChatInputExecutor(command, options) {
109
109
  }
110
110
  const locale = getCommandLocale(command, interaction);
111
111
  // Execute Handler
112
- const { customId } = interaction.isMessageComponent() ? interaction : { customId: null };
113
- if (customId && command.customIdHandlers[customId]) {
114
- await command.customIdHandlers[customId]({ interaction, args, command, locale });
115
- }
116
- else {
117
- await command.execute({ interaction, args, command, locale });
118
- }
112
+ await command.execute({ interaction, args, command, locale });
119
113
  };
120
114
  }
121
115
  /**
@@ -130,7 +124,6 @@ function createComponentExecutor(command) {
130
124
  return async (interaction) => {
131
125
  if (!interaction.customId.startsWith(command.componentId || ''))
132
126
  return;
133
- // Llama a la constante, que TypeScript sabe que está definida.
134
127
  await executor({
135
128
  interaction,
136
129
  command,
@@ -192,8 +185,13 @@ function createButtonExecutor(command) {
192
185
  * @returns {CommandData[]} An array of command data objects ready for the Discord.js client.
193
186
  */
194
187
  function RegisterCommand(commands) {
188
+ commands = Array.isArray(commands) ? commands : [commands];
195
189
  return commands.map(command => {
196
- // --- Build SlashCommand Data ---
190
+ if (command.name === undefined)
191
+ throw new Error("A command is missing a name.");
192
+ if (command.description === undefined)
193
+ throw new Error(`Command "${command.name}" is missing a description.`);
194
+ // Build SlashCommand Data
197
195
  const commandBuilder = new discord_js_1.SlashCommandBuilder()
198
196
  .setName(command.name)
199
197
  .setDescription(command.description)
@@ -235,13 +233,10 @@ function RegisterCommand(commands) {
235
233
  case discord_js_1.ApplicationCommandOptionType.User:
236
234
  commandBuilder.addUserOption(optionBuilder);
237
235
  break;
238
- case discord_js_1.ApplicationCommandOptionType.Channel:
239
- commandBuilder.addChannelOption(optionBuilder);
240
- break;
241
236
  default: throw new Error(`Unsupported option type: ${opt.type}`);
242
237
  }
243
238
  });
244
- // --- Assign Handlers using Constructors ---
239
+ // Assign Handlers using Constructors
245
240
  return {
246
241
  data: commandBuilder,
247
242
  execute: createChatInputExecutor(command, options),
package/dist/types.d.ts CHANGED
@@ -13,7 +13,7 @@ export interface CommandOption {
13
13
  /** The name of the option, must be unique within the command. */
14
14
  name: string;
15
15
  /** The data type the option expects (e.g., STRING, USER, CHANNEL). */
16
- type: OptionType;
16
+ type: AllowedOptionType;
17
17
  /** The description of the option. It can be a string or an object for localization. */
18
18
  description: Record<Locale, string> | string;
19
19
  /** Defines if the option is required. */
@@ -39,6 +39,16 @@ export interface CommandData {
39
39
  /** The command's cooldown time in seconds. */
40
40
  cooldown: number;
41
41
  }
42
+ /**
43
+ * @const ALLOWED_OPTION_TYPE
44
+ * @description A list of allowed option types for slash commands.
45
+ */
46
+ export declare const ALLOWED_OPTION_TYPE: readonly [OptionType.String, OptionType.Boolean, OptionType.Integer, OptionType.Number, OptionType.User];
47
+ /**
48
+ * @type AllowedOptionType
49
+ * @description The union type of allowed option types for slash commands.
50
+ */
51
+ export type AllowedOptionType = typeof ALLOWED_OPTION_TYPE[number];
42
52
  /**
43
53
  * @type LocaleKey
44
54
  * @description An object that maps text identifiers to their translations.
package/dist/types.js CHANGED
@@ -5,3 +5,19 @@
5
5
  * @license MIT
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.ALLOWED_OPTION_TYPE = void 0;
9
+ const discord_js_1 = require("discord.js");
10
+ // =================================================================================================
11
+ // Generic and Utility Types
12
+ // =================================================================================================
13
+ /**
14
+ * @const ALLOWED_OPTION_TYPE
15
+ * @description A list of allowed option types for slash commands.
16
+ */
17
+ exports.ALLOWED_OPTION_TYPE = [
18
+ discord_js_1.ApplicationCommandOptionType.String,
19
+ discord_js_1.ApplicationCommandOptionType.Boolean,
20
+ discord_js_1.ApplicationCommandOptionType.Integer,
21
+ discord_js_1.ApplicationCommandOptionType.Number,
22
+ discord_js_1.ApplicationCommandOptionType.User,
23
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-discord-modularcommand",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "discord",