js-discord-modularcommand 2.1.0 → 2.3.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.
- package/README.md +28 -14
- package/dist/modularbutton.d.ts +7 -3
- package/dist/modularbutton.js +9 -6
- package/dist/modularcommand.d.ts +6 -6
- package/dist/modularcommand.js +5 -4
- package/dist/modularlocale.js +1 -0
- package/dist/modularmodal.d.ts +2 -3
- package/dist/modularmodal.js +2 -4
- package/dist/registercommand.js +6 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,17 +6,12 @@ A module to create and manage modular commands in a simple way for Discord.js bo
|
|
|
6
6
|
|
|
7
7
|
This library simplifies the creation and management of slash commands for [Discord.js](https://discord.js.org/). It allows you to structure your commands in a modular way, making it easier to handle logic, permissions, cooldowns, localizations, and interactive components like buttons and modals.
|
|
8
8
|
|
|
9
|
-
The main classes are:
|
|
10
|
-
- [`ModularCommand`](src/modularcommand.ts): To define the base structure of a command.
|
|
11
|
-
- [`ModularButton`](src/modularcommand.ts): To create interactive buttons associated with a command.
|
|
12
|
-
- [`ModularModal`](src/modularcommand.ts): To create modals (forms) that the user can fill out.
|
|
13
|
-
|
|
14
9
|
## How to use it?
|
|
15
10
|
|
|
16
11
|
First, install the package in your project:
|
|
17
12
|
|
|
18
13
|
```sh
|
|
19
|
-
npm install js-discord-modularcommand
|
|
14
|
+
npm install js-discord-modularcommand@latest
|
|
20
15
|
```
|
|
21
16
|
|
|
22
17
|
Then, you can create your commands in a modular fashion. Here is a basic example of a `ping` command:
|
|
@@ -24,7 +19,7 @@ Then, you can create your commands in a modular fashion. Here is a basic example
|
|
|
24
19
|
```javascript
|
|
25
20
|
// filepath: commands/ping.js
|
|
26
21
|
const { ModularCommand, RegisterCommand } = require('js-discord-modularcommand');
|
|
27
|
-
const { PermissionFlagsBits } = require('discord.js');
|
|
22
|
+
const { PermissionFlagsBits, Locale } = require('discord.js');
|
|
28
23
|
|
|
29
24
|
// Create a new command instance
|
|
30
25
|
const PingCommand = new ModularCommand('ping');
|
|
@@ -37,15 +32,34 @@ PingCommand.setPermissionCheck(async ({ interaction }) => {
|
|
|
37
32
|
return interaction.member.permissions.has(PermissionFlagsBits.Administrator);
|
|
38
33
|
});
|
|
39
34
|
|
|
40
|
-
//
|
|
41
|
-
PingCommand.
|
|
42
|
-
|
|
35
|
+
// Optional: Localization to use with 'locale'
|
|
36
|
+
PingCommand.setLocalizationPhrases({
|
|
37
|
+
[Locale.EnglishUS]: {
|
|
38
|
+
response: 'Replies with Pong!',
|
|
39
|
+
},
|
|
40
|
+
[Locale.SpanishLATAM]: {
|
|
41
|
+
response: 'Responde con Pong!',
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Optional: Set a cooldown (in seconds) by default is 3 seconds
|
|
46
|
+
PingCommand.setCooldown(5);
|
|
47
|
+
|
|
48
|
+
// Set the command's description
|
|
49
|
+
PingCommand.setDescription('Replies with Pong!');
|
|
50
|
+
|
|
51
|
+
// Optional: Add more localization descriptions for the command itself
|
|
52
|
+
PingCommand.setLocalizationDescription({
|
|
53
|
+
[Locale.EnglishUS]: 'Replies with Pong!',
|
|
54
|
+
[Locale.SpanishLATAM]: 'Responde con Pong!',
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Set the executor function
|
|
58
|
+
PingCommand.setExecute(async ({ interaction, locale }) => {
|
|
59
|
+
await interaction.reply(locale['response']);
|
|
43
60
|
});
|
|
44
61
|
|
|
45
|
-
|
|
46
|
-
module.exports = RegisterCommand([
|
|
47
|
-
PingCommand
|
|
48
|
-
]);
|
|
62
|
+
module.exports = RegisterCommand(PingCommand)
|
|
49
63
|
```
|
|
50
64
|
|
|
51
65
|
In your main file, you can load the commands and register their executors with your Discord client.
|
package/dist/modularbutton.d.ts
CHANGED
|
@@ -16,15 +16,19 @@ export default class ModularButton {
|
|
|
16
16
|
customId: string;
|
|
17
17
|
/** The visual style of the button. */
|
|
18
18
|
style: ButtonStyle;
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated This property is deprecated and will be removed in future versions.
|
|
21
|
+
* Use other mechanisms to handle button interactions.
|
|
22
|
+
*/
|
|
20
23
|
execute: ButtonExecuteFunction;
|
|
21
24
|
/**
|
|
22
25
|
* @description Creates a new ModularButton instance.
|
|
23
26
|
* @param {string} customId The custom ID for the button. This should be unique within the context of a message.
|
|
24
|
-
* @param {ButtonStyle} style The visual style of the button.
|
|
25
27
|
*/
|
|
26
|
-
constructor(customId: string
|
|
28
|
+
constructor(customId: string);
|
|
27
29
|
/**
|
|
30
|
+
* @deprecated This method is deprecated and will be removed in future versions.
|
|
31
|
+
* Use other mechanisms to handle button interactions.
|
|
28
32
|
* @description Sets the execution function for the button's click event.
|
|
29
33
|
* @param {ButtonExecuteFunction} executeFunction The function to run when the button is interacted with.
|
|
30
34
|
* @returns {this} The current ModularButton instance for method chaining.
|
package/dist/modularbutton.js
CHANGED
|
@@ -17,18 +17,21 @@ class ModularButton {
|
|
|
17
17
|
/**
|
|
18
18
|
* @description Creates a new ModularButton instance.
|
|
19
19
|
* @param {string} customId The custom ID for the button. This should be unique within the context of a message.
|
|
20
|
-
* @param {ButtonStyle} style The visual style of the button.
|
|
21
20
|
*/
|
|
22
|
-
constructor(customId
|
|
23
|
-
/**
|
|
21
|
+
constructor(customId) {
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated This property is deprecated and will be removed in future versions.
|
|
24
|
+
* Use other mechanisms to handle button interactions.
|
|
25
|
+
*/
|
|
24
26
|
this.execute = async () => { };
|
|
25
27
|
this.buttonObject = new discord_js_1.ButtonBuilder()
|
|
26
|
-
.setCustomId(customId)
|
|
27
|
-
.setStyle(style);
|
|
28
|
+
.setCustomId(customId);
|
|
28
29
|
this.customId = customId;
|
|
29
|
-
this.style =
|
|
30
|
+
this.style = discord_js_1.ButtonStyle.Primary;
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
33
|
+
* @deprecated This method is deprecated and will be removed in future versions.
|
|
34
|
+
* Use other mechanisms to handle button interactions.
|
|
32
35
|
* @description Sets the execution function for the button's click event.
|
|
33
36
|
* @param {ButtonExecuteFunction} executeFunction The function to run when the button is interacted with.
|
|
34
37
|
* @returns {this} The current ModularButton instance for method chaining.
|
package/dist/modularcommand.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @author vicentefelipechile
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
|
-
import { LocalizationMap
|
|
6
|
+
import { LocalizationMap } from 'discord.js';
|
|
7
7
|
import ModularModal from './modularmodal.js';
|
|
8
8
|
import ModularButton from './modularbutton.js';
|
|
9
9
|
import { ButtonExecuteFunction, CommandExecuteFunction, CommandOption, ComponentExecuteFunction, ModalExecuteFunction, PermissionCheckFunction } from './types.js';
|
|
@@ -47,7 +47,7 @@ export default class ModularCommand {
|
|
|
47
47
|
/** (Optional) The function to handle modal submissions. */
|
|
48
48
|
modalExecute?: ModalExecuteFunction;
|
|
49
49
|
/** A record of handlers for specific component custom IDs. */
|
|
50
|
-
customIdHandlers: Record<string, CommandExecuteFunction>;
|
|
50
|
+
customIdHandlers: Record<string, CommandExecuteFunction | ButtonExecuteFunction | ModalExecuteFunction>;
|
|
51
51
|
/** The command's cooldown time in seconds. */
|
|
52
52
|
cooldown: number;
|
|
53
53
|
/** Whether the command is marked as Not Safe For Work (NSFW). */
|
|
@@ -136,10 +136,10 @@ export default class ModularCommand {
|
|
|
136
136
|
/**
|
|
137
137
|
* Adds a custom ID handler for the command.
|
|
138
138
|
* @param {string} customId The custom ID to match.
|
|
139
|
-
* @param {CommandExecuteFunction} handlerFunction The function to execute when the custom ID matches.
|
|
139
|
+
* @param {CommandExecuteFunction | ButtonExecuteFunction | ModalExecuteFunction} handlerFunction The function to execute when the custom ID matches.
|
|
140
140
|
* @returns {ModularCommand} The command instance for chaining.
|
|
141
141
|
*/
|
|
142
|
-
addCustomIDHandler(customId: string, handlerFunction: CommandExecuteFunction): this;
|
|
142
|
+
addCustomIDHandler(customId: string, handlerFunction: CommandExecuteFunction | ButtonExecuteFunction | ModalExecuteFunction): this;
|
|
143
143
|
/**
|
|
144
144
|
* Creates a new modal for the command.
|
|
145
145
|
* @param {string} modalId The ID for the modal.
|
|
@@ -149,8 +149,8 @@ export default class ModularCommand {
|
|
|
149
149
|
/**
|
|
150
150
|
* Creates a new button for the command.
|
|
151
151
|
* @param {string} customId The custom ID for the button.
|
|
152
|
-
* @param {
|
|
152
|
+
* @param {ButtonExecuteFunction} execute The function to execute when the button is clicked.
|
|
153
153
|
* @return {ModularButton} The created button instance.
|
|
154
154
|
*/
|
|
155
|
-
addButton(customId: string,
|
|
155
|
+
addButton(customId: string, execute: ButtonExecuteFunction): ModularButton;
|
|
156
156
|
}
|
package/dist/modularcommand.js
CHANGED
|
@@ -166,7 +166,7 @@ class ModularCommand {
|
|
|
166
166
|
/**
|
|
167
167
|
* Adds a custom ID handler for the command.
|
|
168
168
|
* @param {string} customId The custom ID to match.
|
|
169
|
-
* @param {CommandExecuteFunction} handlerFunction The function to execute when the custom ID matches.
|
|
169
|
+
* @param {CommandExecuteFunction | ButtonExecuteFunction | ModalExecuteFunction} handlerFunction The function to execute when the custom ID matches.
|
|
170
170
|
* @returns {ModularCommand} The command instance for chaining.
|
|
171
171
|
*/
|
|
172
172
|
addCustomIDHandler(customId, handlerFunction) {
|
|
@@ -186,13 +186,14 @@ class ModularCommand {
|
|
|
186
186
|
/**
|
|
187
187
|
* Creates a new button for the command.
|
|
188
188
|
* @param {string} customId The custom ID for the button.
|
|
189
|
-
* @param {
|
|
189
|
+
* @param {ButtonExecuteFunction} execute The function to execute when the button is clicked.
|
|
190
190
|
* @return {ModularButton} The created button instance.
|
|
191
191
|
*/
|
|
192
|
-
addButton(customId,
|
|
193
|
-
const button = new modularbutton_js_1.default(customId
|
|
192
|
+
addButton(customId, execute) {
|
|
193
|
+
const button = new modularbutton_js_1.default(customId);
|
|
194
194
|
this.buttons.set(customId, button);
|
|
195
195
|
this.buttonsArray.push(button);
|
|
196
|
+
this.addCustomIDHandler(customId, execute);
|
|
196
197
|
return button;
|
|
197
198
|
}
|
|
198
199
|
}
|
package/dist/modularlocale.js
CHANGED
package/dist/modularmodal.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @author vicentefelipechile
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
|
-
import { ModalBuilder, TextInputBuilder
|
|
6
|
+
import { ModalBuilder, TextInputBuilder } from "discord.js";
|
|
7
7
|
import { LocaleKey, ModalExecuteFunction } from "./types";
|
|
8
8
|
import ModularCommand from "./modularcommand";
|
|
9
9
|
/**
|
|
@@ -38,10 +38,9 @@ export default class ModularModal {
|
|
|
38
38
|
/**
|
|
39
39
|
* @description Creates a new text input component and adds it to the modal.
|
|
40
40
|
* @param {string} id The custom ID for the text input.
|
|
41
|
-
* @param {TextInputStyle} style The visual style of the text input.
|
|
42
41
|
* @returns {TextInputBuilder} The created text input instance.
|
|
43
42
|
*/
|
|
44
|
-
newTextInput(id: string
|
|
43
|
+
newTextInput(id: string): TextInputBuilder;
|
|
45
44
|
/**
|
|
46
45
|
* @description Builds the final modal object, applying localized titles and labels.
|
|
47
46
|
* @param {LocaleKey} locale The localization object containing translated texts.
|
package/dist/modularmodal.js
CHANGED
|
@@ -40,13 +40,11 @@ class ModularModal {
|
|
|
40
40
|
/**
|
|
41
41
|
* @description Creates a new text input component and adds it to the modal.
|
|
42
42
|
* @param {string} id The custom ID for the text input.
|
|
43
|
-
* @param {TextInputStyle} style The visual style of the text input.
|
|
44
43
|
* @returns {TextInputBuilder} The created text input instance.
|
|
45
44
|
*/
|
|
46
|
-
newTextInput(id
|
|
45
|
+
newTextInput(id) {
|
|
47
46
|
const textInput = new discord_js_1.TextInputBuilder()
|
|
48
|
-
.setCustomId(id)
|
|
49
|
-
.setStyle(style);
|
|
47
|
+
.setCustomId(id);
|
|
50
48
|
this.modalInputs.set(id, textInput);
|
|
51
49
|
const actionRow = new discord_js_1.ActionRowBuilder().addComponents(textInput);
|
|
52
50
|
this.modalObject.addComponents(actionRow);
|
package/dist/registercommand.js
CHANGED
|
@@ -47,7 +47,7 @@ const locales_1 = require("./locales");
|
|
|
47
47
|
* Falls back to EnglishUS if the target locale is not available.
|
|
48
48
|
* @param {ModularCommand} command The command instance.
|
|
49
49
|
* @param {Interaction} interaction The interaction object to get the locale from.
|
|
50
|
-
* @returns {
|
|
50
|
+
* @returns {LocalizationPhrases} The resolved locale object.
|
|
51
51
|
* @throws {Error} If the EnglishUS localization is missing when at least one localization is defined.
|
|
52
52
|
*/
|
|
53
53
|
function getCommandLocale(command, interaction) {
|
|
@@ -58,8 +58,11 @@ function getCommandLocale(command, interaction) {
|
|
|
58
58
|
if (!localeTable[discord_js_1.Locale.EnglishUS]) {
|
|
59
59
|
throw new Error(`Missing localization for EnglishUS in command ${command.name}`);
|
|
60
60
|
}
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
let phrases = localeTable[interaction.locale];
|
|
62
|
+
if (!phrases) {
|
|
63
|
+
phrases = localeTable[discord_js_1.Locale.EnglishUS];
|
|
64
|
+
}
|
|
65
|
+
return phrases;
|
|
63
66
|
}
|
|
64
67
|
// =================================================================================================
|
|
65
68
|
// Execution Context Constructors
|