js-discord-modularcommand 2.0.3 → 2.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.
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,31 @@ PingCommand.setPermissionCheck(async ({ interaction }) => {
37
32
  return interaction.member.permissions.has(PermissionFlagsBits.Administrator);
38
33
  });
39
34
 
40
- // Define the logic to be executed
41
- PingCommand.setExecute(async ({ interaction }) => {
42
- await interaction.reply('Pong!');
35
+ // Optional: Localization to use with 'locale'
36
+ pong.setLocalizationPhrases({
37
+ [Locale.EnglishUS]: {
38
+ response: 'Replies with Pong!',
39
+ },
40
+ [Locale.SpanishLATAM]: {
41
+ response: 'Responde con Pong!',
42
+ }
43
+ });
44
+
45
+ // Set the command's description
46
+ pong.setDescription('Replies with Pong!');
47
+
48
+ // Optional: Add more localization descriptions for the command itself
49
+ pong.setLocalizationDescription({
50
+ [Locale.EnglishUS]: 'Replies with Pong!',
51
+ [Locale.SpanishLATAM]: 'Responde con Pong!',
52
+ });
53
+
54
+ // Set the executor function
55
+ pong.setExecute(async ({ interaction, locale }) => {
56
+ await interaction.reply(locale['response']);
43
57
  });
44
58
 
45
- // Register the command so it can be used by the Discord.js client
46
- module.exports = RegisterCommand([
47
- PingCommand
48
- ]);
59
+ module.exports = RegisterCommand(pong)
49
60
  ```
50
61
 
51
62
  In your main file, you can load the commands and register their executors with your Discord client.
@@ -3,7 +3,7 @@
3
3
  * @author vicentefelipechile
4
4
  * @license MIT
5
5
  */
6
- import { LocalizationMap, ButtonStyle, Locale } from 'discord.js';
6
+ import { LocalizationMap, ButtonStyle } 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';
@@ -37,7 +37,7 @@ export default class ModularCommand {
37
37
  /** The options (arguments) that the command accepts. */
38
38
  options: CommandOption[];
39
39
  /** An object containing localizations for the names and descriptions of the options. */
40
- optionsLocalizations: Record<string, Record<Locale, string>>;
40
+ optionsLocalizations: LocalizationMap;
41
41
  /** The main function that executes when the command is invoked. */
42
42
  execute: CommandExecuteFunction;
43
43
  /** (Optional) The function to handle button interactions. */
@@ -53,7 +53,7 @@ export default class ModularCommand {
53
53
  /** Whether the command is marked as Not Safe For Work (NSFW). */
54
54
  isNSFW: boolean;
55
55
  /** (Optional) An object with localized phrases to be used within the command's execution. */
56
- localizationPhrases?: Record<Locale, string>;
56
+ localizationPhrases?: LocalizationMap;
57
57
  /** (Optional) The function that checks if a user has permission to execute the command. */
58
58
  permissionCheck?: PermissionCheckFunction;
59
59
  /** (Optional) The base ID for components associated with this command. */
@@ -72,23 +72,31 @@ export default class ModularCommand {
72
72
  */
73
73
  setDescription(description: string): this;
74
74
  /**
75
+ * @deprecated Use setLocalizationDescription instead.
75
76
  * Sets the description localizations for the command.
76
77
  * @param {LocalizationMap} localizations The description localizations map.
77
78
  * @returns {ModularCommand} The command instance for chaining.
78
79
  */
79
80
  setLocalizationsDescription(localizations: LocalizationMap): this;
81
+ /**
82
+ * Sets the description localizations for the command.
83
+ * @param {LocalizationMap} localizations The description localizations map.
84
+ * @returns {ModularCommand} The command instance for chaining.
85
+ */
86
+ setLocalizationDescription(localizations: LocalizationMap): this;
80
87
  /**
81
88
  * Sets the localizations for the command's options.
82
- * @param {Record<string, Record<Locale, string>>} localizations An object with the localizations.
89
+ * @param {LocalizationMap} localizations An object with the localizations.
83
90
  * @returns {ModularCommand} The command instance for chaining.
84
91
  */
85
- setLocalizationOptions(localizations: Record<string, Record<Locale, string>>): this;
92
+ setLocalizationOptions(localizations: LocalizationMap): this;
86
93
  /**
87
94
  * Sets the localization phrases for the command.
88
- * @param {Record<Locale, string>} localizationPhrases The localization phrases.
95
+ * Accepts a partial record, so not all locales need to be provided.
96
+ * @param {LocalizationMap} localizationPhrases The localization phrases.
89
97
  * @returns {ModularCommand} The command instance for chaining.
90
98
  */
91
- setLocalizationPhrases(localizationPhrases: Record<Locale, string>): this;
99
+ setLocalizationPhrases(localizationPhrases: LocalizationMap): this;
92
100
  /**
93
101
  * Sets the execute function for the command.
94
102
  * @param {CommandExecuteFunction} executeFunction The function to execute.
@@ -63,31 +63,50 @@ class ModularCommand {
63
63
  return this;
64
64
  }
65
65
  /**
66
+ * @deprecated Use setLocalizationDescription instead.
66
67
  * Sets the description localizations for the command.
67
68
  * @param {LocalizationMap} localizations The description localizations map.
68
69
  * @returns {ModularCommand} The command instance for chaining.
69
70
  */
70
71
  setLocalizationsDescription(localizations) {
72
+ return this.setLocalizationDescription(localizations);
73
+ }
74
+ /**
75
+ * Sets the description localizations for the command.
76
+ * @param {LocalizationMap} localizations The description localizations map.
77
+ * @returns {ModularCommand} The command instance for chaining.
78
+ */
79
+ setLocalizationDescription(localizations) {
71
80
  this.description = localizations[discord_js_1.Locale.EnglishUS] || this.description;
72
- this.descriptionLocalizations = localizations;
81
+ this.descriptionLocalizations = {
82
+ ...this.descriptionLocalizations,
83
+ ...localizations,
84
+ };
73
85
  return this;
74
86
  }
75
87
  /**
76
88
  * Sets the localizations for the command's options.
77
- * @param {Record<string, Record<Locale, string>>} localizations An object with the localizations.
89
+ * @param {LocalizationMap} localizations An object with the localizations.
78
90
  * @returns {ModularCommand} The command instance for chaining.
79
91
  */
80
92
  setLocalizationOptions(localizations) {
81
- this.optionsLocalizations = localizations;
93
+ this.optionsLocalizations = {
94
+ ...this.optionsLocalizations,
95
+ ...localizations,
96
+ };
82
97
  return this;
83
98
  }
84
99
  /**
85
100
  * Sets the localization phrases for the command.
86
- * @param {Record<Locale, string>} localizationPhrases The localization phrases.
101
+ * Accepts a partial record, so not all locales need to be provided.
102
+ * @param {LocalizationMap} localizationPhrases The localization phrases.
87
103
  * @returns {ModularCommand} The command instance for chaining.
88
104
  */
89
105
  setLocalizationPhrases(localizationPhrases) {
90
- this.localizationPhrases = localizationPhrases;
106
+ this.localizationPhrases = {
107
+ ...this.localizationPhrases,
108
+ ...localizationPhrases,
109
+ };
91
110
  return this;
92
111
  }
93
112
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-discord-modularcommand",
3
- "version": "2.0.3",
3
+ "version": "2.2.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "discord",