oopscord.js 0.0.25 → 0.0.27
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/bun.lock +264 -0
- package/dist/structures/Constants.d.ts +2 -0
- package/dist/structures/Constants.d.ts.map +1 -1
- package/dist/structures/Constants.js +1 -0
- package/dist/structures/DiscordApp.d.ts +79 -2
- package/dist/structures/DiscordApp.d.ts.map +1 -1
- package/dist/structures/DiscordApp.js +215 -56
- package/dist/structures/components/BaseButton.d.ts +35 -1
- package/dist/structures/components/BaseButton.d.ts.map +1 -1
- package/dist/structures/components/BaseButton.js +70 -24
- package/dist/structures/components/BaseModal.d.ts +104 -6
- package/dist/structures/components/BaseModal.d.ts.map +1 -1
- package/dist/structures/components/BaseModal.js +161 -28
- package/dist/structures/components/BaseSelectMenu.d.ts +73 -3
- package/dist/structures/components/BaseSelectMenu.d.ts.map +1 -1
- package/dist/structures/components/BaseSelectMenu.js +117 -47
- package/dist/structures/decorators/Constants.d.ts +2 -0
- package/dist/structures/decorators/Constants.d.ts.map +1 -1
- package/dist/structures/decorators/Constants.js +3 -1
- package/dist/structures/decorators/DiscordModuleDecorator.d.ts +27 -0
- package/dist/structures/decorators/DiscordModuleDecorator.d.ts.map +1 -1
- package/dist/structures/decorators/DiscordModuleDecorator.js +27 -0
- package/dist/structures/decorators/MessageComponentDecorator.d.ts +73 -6
- package/dist/structures/decorators/MessageComponentDecorator.d.ts.map +1 -1
- package/dist/structures/decorators/MessageComponentDecorator.js +71 -3
- package/dist/utils/Util.d.ts +3 -0
- package/dist/utils/Util.d.ts.map +1 -1
- package/dist/utils/Util.js +15 -2
- package/package.json +4 -3
- package/dist/structures/Client.d.ts +0 -6
- package/dist/structures/Client.d.ts.map +0 -1
- package/dist/structures/Client.js +0 -43
- package/dist/structures/decorators/DiscordModule.d.ts +0 -20
- package/dist/structures/decorators/DiscordModule.d.ts.map +0 -1
- package/dist/structures/decorators/DiscordModule.js +0 -56
- package/dist/utils/InteractionUtil.d.ts +0 -5
- package/dist/utils/InteractionUtil.d.ts.map +0 -1
- package/dist/utils/InteractionUtil.js +0 -10
- package/tsconfig.json +0 -110
|
@@ -5,10 +5,34 @@ const discord_js_1 = require("discord.js");
|
|
|
5
5
|
const v10_1 = require("discord-api-types/v10");
|
|
6
6
|
const Constants_1 = require("./decorators/Constants");
|
|
7
7
|
const Constants_2 = require("./Constants");
|
|
8
|
+
const Util_1 = require("../utils/Util");
|
|
8
9
|
const defaultDiscordAppOptions = {
|
|
9
10
|
logger: console,
|
|
10
11
|
plugins: []
|
|
11
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* The main class that you must extend to create a Discord application. You create an instance by using the `DiscordApp.create()` method.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { DiscordApp, BaseDiscordModule } from "oopscord.js";
|
|
20
|
+
*
|
|
21
|
+
* @DiscordModule({
|
|
22
|
+
* token: "your-token-goes-here",
|
|
23
|
+
* intents: [ IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.MessageContent ],
|
|
24
|
+
* imports: [ MyCommandModule, MyComponentModule ]
|
|
25
|
+
* })
|
|
26
|
+
* export class AppModule extends BaseDiscordModule {
|
|
27
|
+
* @EventHandler("clientReady")
|
|
28
|
+
* onReady() {
|
|
29
|
+
* this.logger.info("Client is ready!");
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @see {@link DiscordApp.create}
|
|
35
|
+
*/
|
|
12
36
|
class BaseDiscordModule {
|
|
13
37
|
client;
|
|
14
38
|
logger;
|
|
@@ -22,6 +46,33 @@ class BaseDiscordModule {
|
|
|
22
46
|
}
|
|
23
47
|
exports.BaseDiscordModule = BaseDiscordModule;
|
|
24
48
|
let a1 = false;
|
|
49
|
+
/**
|
|
50
|
+
* Main application class for managing Discord bot functionality.
|
|
51
|
+
*
|
|
52
|
+
* Handles initialization, login, command deployment, and interaction routing for a Discord application.
|
|
53
|
+
* This class uses the Discord.js library and implements a decorator-based command and component system.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { DiscordApp } from "oopscord.js";
|
|
58
|
+
* import { AppModule } from "./app.module";
|
|
59
|
+
*
|
|
60
|
+
* async function bootstrap() {
|
|
61
|
+
* const app = DiscordApp.create(AppModule);
|
|
62
|
+
* await app.login();
|
|
63
|
+
* }
|
|
64
|
+
*
|
|
65
|
+
* bootstrap();
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @remarks
|
|
69
|
+
* - Must be instantiated using the static `create()` method
|
|
70
|
+
* - Supports global and guild-specific (private) command deployment
|
|
71
|
+
* - Routes interactions (commands, buttons, select menus, modals) to appropriate handlers
|
|
72
|
+
* - Uses reflection metadata for decorator-based configuration
|
|
73
|
+
*
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
25
76
|
class DiscordApp {
|
|
26
77
|
#rest;
|
|
27
78
|
#client;
|
|
@@ -41,55 +92,61 @@ class DiscordApp {
|
|
|
41
92
|
this.#loadStartTimestamp = Date.now();
|
|
42
93
|
this.#rest = new discord_js_1.REST({ version: "10" });
|
|
43
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Logs in to Discord and deploys the commands. This method must be called after creating the application with `DiscordApp.create()`.
|
|
97
|
+
* @see {@link DiscordApp.create}
|
|
98
|
+
*/
|
|
44
99
|
async login() {
|
|
100
|
+
const commands = this.#commands.filter(i => Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, i) == Constants_2.InteractionType.CHAT_INPUT_COMMAND || Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, i) == Constants_2.InteractionType.CONTEXT_MENU_COMMAND);
|
|
101
|
+
const privateCommands = commands.filter(c => Reflect.getMetadata(Constants_1.COMMAND_PRIVATE_KEY, c));
|
|
102
|
+
const globalCommands = commands.filter(c => !Reflect.getMetadata(Constants_1.COMMAND_PRIVATE_KEY, c));
|
|
103
|
+
this.#module.logger.info(`Found ${commands.length} command(s) in total, ${globalCommands.length} global command(s) and ${privateCommands.length} private command(s). (${(Date.now() - this.#loadStartTimestamp)}ms)`);
|
|
104
|
+
this.#module.logger.info("Mapping global commands...");
|
|
105
|
+
// Prepare global commands
|
|
106
|
+
const apiGlobalCommands = globalCommands.map(c => {
|
|
107
|
+
const type = Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, c);
|
|
108
|
+
const integrationTypes = Reflect.getMetadata(Constants_1.INTERACTION_INTEGRATION_TYPES_KEY, c);
|
|
109
|
+
if (type == Constants_2.InteractionType.CHAT_INPUT_COMMAND)
|
|
110
|
+
return this.#mapSlashCommand(Reflect.getMetadata(Constants_1.COMMAND_NAME_KEY, c), Reflect.getMetadata(Constants_1.COMMAND_DESCRIPTION_KEY, c), Reflect.getMetadata(Constants_1.COMMAND_OPTIONS_KEY, c), integrationTypes);
|
|
111
|
+
else
|
|
112
|
+
return this.#mapContextMenuCommand(Reflect.getMetadata(Constants_1.COMMAND_NAME_KEY, c), Reflect.getMetadata(Constants_1.CONTEXT_MENU_TYPE_KEY, c), integrationTypes);
|
|
113
|
+
});
|
|
114
|
+
this.#module.logger.info(`└─ Mapped ${apiGlobalCommands.length} global command(s). (${(Date.now() - this.#loadStartTimestamp)}ms)`);
|
|
115
|
+
this.#module.logger.info("Mapping private commands...");
|
|
116
|
+
// Prepare private commands
|
|
117
|
+
const apiPrivateCommands = {};
|
|
118
|
+
privateCommands.forEach(c => {
|
|
119
|
+
const guildId = Reflect.getMetadata(Constants_1.COMMAND_PRIVATE_GUILD_KEY, c);
|
|
120
|
+
const type = Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, c);
|
|
121
|
+
const integrationTypes = Reflect.getMetadata(Constants_1.INTERACTION_INTEGRATION_TYPES_KEY, c);
|
|
122
|
+
let data;
|
|
123
|
+
if (type == Constants_2.InteractionType.CHAT_INPUT_COMMAND)
|
|
124
|
+
data = this.#mapSlashCommand(Reflect.getMetadata(Constants_1.COMMAND_NAME_KEY, c), Reflect.getMetadata(Constants_1.COMMAND_DESCRIPTION_KEY, c), Reflect.getMetadata(Constants_1.COMMAND_OPTIONS_KEY, c), integrationTypes);
|
|
125
|
+
else
|
|
126
|
+
data = this.#mapContextMenuCommand(Reflect.getMetadata(Constants_1.COMMAND_NAME_KEY, c), Reflect.getMetadata(Constants_1.CONTEXT_MENU_TYPE_KEY, c), integrationTypes);
|
|
127
|
+
if (!Array.isArray(apiPrivateCommands[guildId]))
|
|
128
|
+
apiPrivateCommands[guildId] = [];
|
|
129
|
+
apiPrivateCommands[guildId].push(data);
|
|
130
|
+
});
|
|
131
|
+
const wholePrivateCommands = Object.values(apiPrivateCommands).flat();
|
|
132
|
+
this.#module.logger.info(`└─ Mapped ${Object.keys(apiPrivateCommands).length} guild(s) with ${wholePrivateCommands.length} private command(s). (${(Date.now() - this.#loadStartTimestamp)}ms)`);
|
|
45
133
|
this.#rest.setToken(this.#moduleOptions.token);
|
|
134
|
+
this.#module.logger.info("Logging in...");
|
|
46
135
|
await this.#client.login(this.#moduleOptions.token);
|
|
136
|
+
this.#module.logger.info(`Successfully logged in. (${(Date.now() - this.#loadStartTimestamp)}ms)`);
|
|
47
137
|
try {
|
|
48
|
-
const commands = this.#commands.filter(i => Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, i) == Constants_2.InteractionType.CHAT_INPUT_COMMAND || Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, i) == Constants_2.InteractionType.CONTEXT_MENU_COMMAND);
|
|
49
|
-
const privateCommands = commands.filter(c => Reflect.getMetadata(Constants_1.COMMAND_PRIVATE_KEY, c));
|
|
50
|
-
const globalCommands = commands.filter(c => !Reflect.getMetadata(Constants_1.COMMAND_PRIVATE_KEY, c));
|
|
51
|
-
this.#module.logger.info(`Found ${commands.length} command(s) in total, ${globalCommands.length} global command(s) and ${privateCommands.length} private command(s).`);
|
|
52
|
-
this.#module.logger.info("Mapping global commands...");
|
|
53
|
-
// Prepare global commands
|
|
54
|
-
const apiGlobalCommands = globalCommands.map(c => {
|
|
55
|
-
const type = Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, c);
|
|
56
|
-
const integrationTypes = Reflect.getMetadata(Constants_1.INTERACTION_INTEGRATION_TYPES_KEY, c);
|
|
57
|
-
if (type == Constants_2.InteractionType.CHAT_INPUT_COMMAND)
|
|
58
|
-
return this.#mapSlashCommand(Reflect.getMetadata(Constants_1.COMMAND_NAME_KEY, c), Reflect.getMetadata(Constants_1.COMMAND_DESCRIPTION_KEY, c), Reflect.getMetadata(Constants_1.COMMAND_OPTIONS_KEY, c), integrationTypes);
|
|
59
|
-
else
|
|
60
|
-
return this.#mapContextMenuCommand(Reflect.getMetadata(Constants_1.COMMAND_NAME_KEY, c), Reflect.getMetadata(Constants_1.CONTEXT_MENU_TYPE_KEY, c), integrationTypes);
|
|
61
|
-
});
|
|
62
|
-
this.#module.logger.info(`└─ Mapped ${apiGlobalCommands.length} global command(s).`);
|
|
63
|
-
this.#module.logger.info("Mapping private commands...");
|
|
64
|
-
// Prepare private commands
|
|
65
|
-
const apiPrivateCommands = {};
|
|
66
|
-
privateCommands.forEach(c => {
|
|
67
|
-
const guildId = Reflect.getMetadata(Constants_1.COMMAND_PRIVATE_GUILD_KEY, c);
|
|
68
|
-
const type = Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, c);
|
|
69
|
-
const integrationTypes = Reflect.getMetadata(Constants_1.INTERACTION_INTEGRATION_TYPES_KEY, c);
|
|
70
|
-
let data;
|
|
71
|
-
if (type == Constants_2.InteractionType.CHAT_INPUT_COMMAND)
|
|
72
|
-
data = this.#mapSlashCommand(Reflect.getMetadata(Constants_1.COMMAND_NAME_KEY, c), Reflect.getMetadata(Constants_1.COMMAND_DESCRIPTION_KEY, c), Reflect.getMetadata(Constants_1.COMMAND_OPTIONS_KEY, c), integrationTypes);
|
|
73
|
-
else
|
|
74
|
-
data = this.#mapContextMenuCommand(Reflect.getMetadata(Constants_1.COMMAND_NAME_KEY, c), Reflect.getMetadata(Constants_1.CONTEXT_MENU_TYPE_KEY, c), integrationTypes);
|
|
75
|
-
if (!Array.isArray(apiPrivateCommands[guildId]))
|
|
76
|
-
apiPrivateCommands[guildId] = [];
|
|
77
|
-
apiPrivateCommands[guildId].push(data);
|
|
78
|
-
});
|
|
79
|
-
const wholePrivateCommands = Object.values(apiPrivateCommands).flat();
|
|
80
|
-
this.#module.logger.info(`└─ Mapped ${Object.keys(apiPrivateCommands).length} guild(s) with ${wholePrivateCommands.length} private command(s).`);
|
|
81
138
|
this.#module.logger.info("Deploying commands...");
|
|
82
139
|
if (this.#moduleOptions.test.enable) {
|
|
83
140
|
await this.#rest.put(v10_1.Routes.applicationGuildCommands(this.#client.user.id, this.#moduleOptions.test.guild), { body: apiGlobalCommands });
|
|
84
|
-
this.#module.logger.info(`├─ Successfully registered global commands, in test guild ${this.#moduleOptions.test.guild}
|
|
141
|
+
this.#module.logger.info(`├─ Successfully registered global commands, in test guild ${this.#moduleOptions.test.guild}. (${(Date.now() - this.#loadStartTimestamp)}ms)`);
|
|
85
142
|
}
|
|
86
143
|
else {
|
|
87
144
|
await this.#rest.put(v10_1.Routes.applicationCommands(this.#client.user.id), { body: apiGlobalCommands });
|
|
88
|
-
this.#module.logger.info(`├─ Successfully registered global commands
|
|
145
|
+
this.#module.logger.info(`├─ Successfully registered global commands. (${(Date.now() - this.#loadStartTimestamp)}ms)`);
|
|
89
146
|
}
|
|
90
147
|
for (const guildId in apiPrivateCommands) {
|
|
91
148
|
await this.#rest.put(v10_1.Routes.applicationGuildCommands(this.#client.user.id, guildId), { body: apiPrivateCommands[guildId] });
|
|
92
|
-
this.#module.logger.info(`├─ Successfully registered private commands in ${guildId}
|
|
149
|
+
this.#module.logger.info(`├─ Successfully registered private commands in ${guildId}. (${(Date.now() - this.#loadStartTimestamp)}ms)`);
|
|
93
150
|
}
|
|
94
151
|
this.#module.logger.info("└─ Successfully deployed all commands.");
|
|
95
152
|
}
|
|
@@ -99,6 +156,12 @@ class DiscordApp {
|
|
|
99
156
|
}
|
|
100
157
|
this.#module.logger.info("Application took " + (Date.now() - this.#loadStartTimestamp) + "ms to load.");
|
|
101
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Handles incoming chat input command interactions by routing them to the appropriate command handler based on the command name, subcommand group, and subcommand. It uses reflection metadata to determine which handler to invoke and what arguments to pass.
|
|
161
|
+
* @param itr The chat input command interaction to handle.
|
|
162
|
+
* @returns {void}
|
|
163
|
+
* @internal
|
|
164
|
+
*/
|
|
102
165
|
#chatInputCommandHandler(itr) {
|
|
103
166
|
const commands = this.#commands.filter(i => Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, i) == Constants_2.InteractionType.CHAT_INPUT_COMMAND);
|
|
104
167
|
const commandName = itr.commandName;
|
|
@@ -112,6 +175,8 @@ class DiscordApp {
|
|
|
112
175
|
let runMethod = Reflect.getMetadata(Constants_1.INTERACTION_RUN_METHOD_KEY, handler);
|
|
113
176
|
let preArgs = [Reflect.getMetadata(Constants_1.INTERACTION_PARAMETER_INDEX_KEY, handler), ...(Reflect.getMetadata(Constants_1.OPTIONS_PARAMETER_INDEX_KEY, handler) || [])];
|
|
114
177
|
let args = Array(preArgs.length).fill(null);
|
|
178
|
+
if (runMethod == undefined)
|
|
179
|
+
throw new TypeError("Failed to get the run method. Did you add the @Run decorator?");
|
|
115
180
|
preArgs.forEach(arg => {
|
|
116
181
|
if (typeof arg == "number")
|
|
117
182
|
args[arg] = itr;
|
|
@@ -128,6 +193,8 @@ class DiscordApp {
|
|
|
128
193
|
let runMethod = Reflect.getMetadata(Constants_1.INTERACTION_RUN_METHOD_KEY, handler);
|
|
129
194
|
let preArgs = [Reflect.getMetadata(Constants_1.INTERACTION_PARAMETER_INDEX_KEY, handler), ...(Reflect.getMetadata(Constants_1.OPTIONS_PARAMETER_INDEX_KEY, handler) || [])];
|
|
130
195
|
let args = Array(preArgs.length).fill(null);
|
|
196
|
+
if (runMethod == undefined)
|
|
197
|
+
throw new TypeError("Failed to get the run method. Did you add the @Run decorator?");
|
|
131
198
|
preArgs.forEach(arg => {
|
|
132
199
|
if (typeof arg == "number")
|
|
133
200
|
args[arg] = itr;
|
|
@@ -141,6 +208,8 @@ class DiscordApp {
|
|
|
141
208
|
let runMethod = Reflect.getMetadata(Constants_1.INTERACTION_RUN_METHOD_KEY, command);
|
|
142
209
|
let preArgs = [Reflect.getMetadata(Constants_1.INTERACTION_PARAMETER_INDEX_KEY, command), ...(Reflect.getMetadata(Constants_1.OPTIONS_PARAMETER_INDEX_KEY, command) || [])];
|
|
143
210
|
let args = Array(preArgs.length).fill(null);
|
|
211
|
+
if (runMethod == undefined)
|
|
212
|
+
throw new TypeError("Failed to get the run method. Did you add the @Run decorator?");
|
|
144
213
|
preArgs.forEach(arg => {
|
|
145
214
|
if (typeof arg == "number")
|
|
146
215
|
args[arg] = itr;
|
|
@@ -151,42 +220,122 @@ class DiscordApp {
|
|
|
151
220
|
cmd[runMethod]();
|
|
152
221
|
}
|
|
153
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Handles incoming context menu command interactions by routing them to the appropriate command handler based on the command name and type. It uses reflection metadata to determine which handler to invoke and what arguments to pass.
|
|
225
|
+
* @param itr The context menu command interaction to handle.
|
|
226
|
+
* @returns {void}
|
|
227
|
+
* @internal
|
|
228
|
+
*/
|
|
229
|
+
#contextMenuCommandHandler(itr) {
|
|
230
|
+
const commands = this.#commands.filter(i => Reflect.getMetadata(Constants_1.INTERACTION_TYPE_KEY, i) == Constants_2.InteractionType.CONTEXT_MENU_COMMAND);
|
|
231
|
+
const commandName = itr.commandName;
|
|
232
|
+
const command = commands.find(c => Reflect.getMetadata(Constants_1.COMMAND_NAME_KEY, c) == commandName && Reflect.getMetadata(Constants_1.COMMAND_OPTIONS_KEY, c).type == itr.commandType);
|
|
233
|
+
if (!command)
|
|
234
|
+
return;
|
|
235
|
+
let runMethod = Reflect.getMetadata(Constants_1.INTERACTION_RUN_METHOD_KEY, command);
|
|
236
|
+
if (runMethod == undefined)
|
|
237
|
+
throw new TypeError("Failed to get the run method. Did you add the @Run decorator?");
|
|
238
|
+
let cmd = new command(itr);
|
|
239
|
+
cmd[runMethod]();
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Handles incoming button interactions by routing them to the appropriate button handler based on the custom ID. It uses reflection metadata to determine which handler to invoke and what arguments to pass.
|
|
243
|
+
* @param itr The button interaction to handle.
|
|
244
|
+
* @returns {void}
|
|
245
|
+
* @internal
|
|
246
|
+
*/
|
|
154
247
|
#buttonHandler(itr) {
|
|
155
248
|
const button = this.#buttons.find(b => Reflect.getMetadata(Constants_1.BUTTON_OPTIONS_KEY, b).customId == itr.customId);
|
|
156
249
|
if (!button)
|
|
157
250
|
return;
|
|
158
251
|
let runMethod = Reflect.getMetadata(Constants_1.INTERACTION_RUN_METHOD_KEY, button);
|
|
252
|
+
if (runMethod == undefined)
|
|
253
|
+
throw new TypeError("Failed to get the run method. Did you add the @Run decorator?");
|
|
159
254
|
let cmd = new button(itr);
|
|
160
255
|
cmd[runMethod]();
|
|
161
256
|
}
|
|
257
|
+
/**
|
|
258
|
+
* Handles incoming select menu interactions by routing them to the appropriate select menu handler based on the custom ID. It uses reflection metadata to determine which handler to invoke and what arguments to pass.
|
|
259
|
+
* @param itr The select menu interaction to handle.
|
|
260
|
+
* @returns {void}
|
|
261
|
+
* @internal
|
|
262
|
+
*/
|
|
162
263
|
#selectMenuHandler(itr) {
|
|
163
264
|
const selectMenu = this.#selectMenus.find(b => Reflect.getMetadata(Constants_1.SELECT_MENU_OPTIONS_KEY, b).customId == itr.customId);
|
|
164
265
|
if (!selectMenu)
|
|
165
266
|
return;
|
|
166
267
|
let runMethod = Reflect.getMetadata(Constants_1.INTERACTION_RUN_METHOD_KEY, selectMenu);
|
|
268
|
+
if (runMethod == undefined)
|
|
269
|
+
throw new TypeError("Failed to get the run method. Did you add the @Run decorator?");
|
|
167
270
|
let cmd = new selectMenu(itr);
|
|
168
271
|
cmd[runMethod]();
|
|
169
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Handles incoming modal submit interactions by routing them to the appropriate modal handler based on the custom ID. It uses reflection metadata to determine which handler to invoke and what arguments to pass, including extracting values from text input components if necessary.
|
|
275
|
+
* @param itr The modal submit interaction to handle.
|
|
276
|
+
* @returns {void}
|
|
277
|
+
* @internal
|
|
278
|
+
*/
|
|
170
279
|
#modalHandler(itr) {
|
|
171
280
|
const modal = this.#modals.find(b => Reflect.getMetadata(Constants_1.MODAL_OPTIONS_KEY, b).customId == itr.customId);
|
|
172
281
|
if (!modal)
|
|
173
282
|
return;
|
|
283
|
+
const modalComponents = Reflect.getMetadata(Constants_1.MODAL_COMPONENTS_KEY, modal);
|
|
174
284
|
let runMethod = Reflect.getMetadata(Constants_1.INTERACTION_RUN_METHOD_KEY, modal);
|
|
175
|
-
let preArgs = [Reflect.getMetadata(Constants_1.INTERACTION_PARAMETER_INDEX_KEY, modal), ...(Reflect.getMetadata(Constants_1.MODAL_TEXT_INPUT_VALUE_INDEX_KEY, modal) || [])];
|
|
285
|
+
let preArgs = [Reflect.getMetadata(Constants_1.INTERACTION_PARAMETER_INDEX_KEY, modal), ...(Reflect.getMetadata(Constants_1.MODAL_TEXT_INPUT_VALUE_INDEX_KEY, modal) || []), ...(Reflect.getMetadata(Constants_1.MODAL_SELECT_MENU_VALUE_INDEX_KEY, modal) || []), ...(Reflect.getMetadata(Constants_1.MODAL_FILE_UPLOAD_VALUE_INDEX_KEY, modal) || [])];
|
|
176
286
|
let args = Array(preArgs.length).fill(null);
|
|
177
287
|
preArgs.forEach(arg => {
|
|
178
|
-
if (typeof arg == "number")
|
|
288
|
+
if (typeof arg == "number") {
|
|
179
289
|
args[arg] = itr;
|
|
180
|
-
|
|
181
|
-
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
const fieldIndex = arg;
|
|
293
|
+
switch (fieldIndex.type) {
|
|
294
|
+
case "text_input":
|
|
295
|
+
args[arg.index] = itr.fields.getTextInputValue(fieldIndex.customId);
|
|
296
|
+
break;
|
|
297
|
+
case "select_menu":
|
|
298
|
+
const getMethods = {
|
|
299
|
+
"string_select": itr.fields.getStringSelectValues,
|
|
300
|
+
"user_select": itr.fields.getSelectedUsers,
|
|
301
|
+
"role_select": itr.fields.getSelectedRoles,
|
|
302
|
+
"mentionable_select": itr.fields.getSelectedMentionables,
|
|
303
|
+
"channel_select": itr.fields.getSelectedChannels
|
|
304
|
+
};
|
|
305
|
+
const label = modalComponents.find(c => c.outerType == "label" && c.innerType.endsWith("select") && c.component.data.component?.data?.custom_id == fieldIndex.customId);
|
|
306
|
+
const getMethod = getMethods[label?.innerType];
|
|
307
|
+
if (!getMethod)
|
|
308
|
+
throw new TypeError(`Failed to get the select menu values for custom ID ${fieldIndex.customId}. Failed to gather necessary metadata.`);
|
|
309
|
+
const result = getMethod.bind(itr.fields)(fieldIndex.customId);
|
|
310
|
+
if (result instanceof discord_js_1.Collection)
|
|
311
|
+
args[arg.index] = result.toJSON();
|
|
312
|
+
else
|
|
313
|
+
args[arg.index] = result;
|
|
314
|
+
break;
|
|
315
|
+
case "file_upload":
|
|
316
|
+
args[arg.index] = itr.fields.getUploadedFiles(fieldIndex.customId);
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
182
320
|
});
|
|
321
|
+
if (runMethod == undefined)
|
|
322
|
+
throw new TypeError("Failed to get the run method. Did you add the @Run decorator?");
|
|
183
323
|
let cmd = new modal(...args);
|
|
184
324
|
cmd[runMethod]();
|
|
185
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* Automatically handles incoming interactions by determining their type (chat input command, context menu command, button interaction, select menu interaction, or modal submit) and routing them to the appropriate handler method. This method is called internally whenever an interaction is created.
|
|
328
|
+
* @param itr The interaction to handle.
|
|
329
|
+
* @returns {void}
|
|
330
|
+
* @internal
|
|
331
|
+
*/
|
|
186
332
|
#autoHandler(itr) {
|
|
187
333
|
if (itr.isChatInputCommand()) {
|
|
188
334
|
this.#chatInputCommandHandler(itr);
|
|
189
335
|
}
|
|
336
|
+
else if (itr.isContextMenuCommand()) {
|
|
337
|
+
this.#contextMenuCommandHandler(itr);
|
|
338
|
+
}
|
|
190
339
|
else if (itr.isButton()) {
|
|
191
340
|
this.#buttonHandler(itr);
|
|
192
341
|
}
|
|
@@ -196,7 +345,12 @@ class DiscordApp {
|
|
|
196
345
|
else if (itr.isModalSubmit()) {
|
|
197
346
|
this.#modalHandler(itr);
|
|
198
347
|
}
|
|
348
|
+
return;
|
|
199
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* Initializes the Discord application by creating the Discord client, setting up event listeners based on the provided module, and preparing command and component handlers. This method is called internally by the `create()` method after setting the main module and options.
|
|
352
|
+
* @internal
|
|
353
|
+
*/
|
|
200
354
|
#init() {
|
|
201
355
|
const options = Reflect.getMetadata(Constants_1.DICSORD_MODULE_OPTIONS_KEY, this.#moduleFunction);
|
|
202
356
|
const internalEvents = Reflect.getMetadata(Constants_1.DISCORD_MODULE_INTERNAL_EVENTS_KEY, this.#moduleFunction);
|
|
@@ -239,7 +393,7 @@ class DiscordApp {
|
|
|
239
393
|
this.#moduleFunction = discordModule;
|
|
240
394
|
}
|
|
241
395
|
#setOptions(options) {
|
|
242
|
-
this.#appOptions = mergeDefault(defaultDiscordAppOptions, options);
|
|
396
|
+
this.#appOptions = Util_1.Util.mergeDefault(defaultDiscordAppOptions, options);
|
|
243
397
|
}
|
|
244
398
|
#mapSlashCommand(name, description, options = [], integrationTypes = []) {
|
|
245
399
|
const requiredOptions = options.filter(o => o.required);
|
|
@@ -249,7 +403,7 @@ class DiscordApp {
|
|
|
249
403
|
if (integrationTypes.length > 0)
|
|
250
404
|
data.setIntegrationTypes(integrationTypes);
|
|
251
405
|
Reflect.set(data, "options", [...requiredOptions, ...options.filter(o => !o.required)]);
|
|
252
|
-
this.#module.logger.info(`├─ Mapped slash command ${name}
|
|
406
|
+
this.#module.logger.info(`├─ Mapped slash command ${name}. (${(Date.now() - this.#loadStartTimestamp)}ms)`);
|
|
253
407
|
return data.toJSON();
|
|
254
408
|
}
|
|
255
409
|
#mapContextMenuCommand(name, type, integrationTypes = []) {
|
|
@@ -258,9 +412,27 @@ class DiscordApp {
|
|
|
258
412
|
.setType(type);
|
|
259
413
|
if (integrationTypes.length > 0)
|
|
260
414
|
data.setIntegrationTypes(integrationTypes);
|
|
261
|
-
this.#module.logger.info(`├─ Mapped context command ${name}
|
|
415
|
+
this.#module.logger.info(`├─ Mapped context command ${name}. (${(Date.now() - this.#loadStartTimestamp)}ms)`);
|
|
262
416
|
return data.toJSON();
|
|
263
417
|
}
|
|
418
|
+
/**
|
|
419
|
+
* Creates a new Discord application. This is the main entry point of the library. You must provide a class that extends {@link BaseDiscordModule}, which will be used as the main module of the application.
|
|
420
|
+
* @param discordModule The main module of the application. This class must extend `BaseDiscordModule`.
|
|
421
|
+
* @param options The options for the application.
|
|
422
|
+
* @example
|
|
423
|
+
* ```ts
|
|
424
|
+
* import { DiscordApp } from "oopscord.js";
|
|
425
|
+
* import { AppModule } from "./app.module";
|
|
426
|
+
*
|
|
427
|
+
* async function bootstrap() {
|
|
428
|
+
* const app = DiscordApp.create(AppModule);
|
|
429
|
+
*
|
|
430
|
+
* await app.login();
|
|
431
|
+
* }
|
|
432
|
+
*
|
|
433
|
+
* bootstrap();
|
|
434
|
+
* ```
|
|
435
|
+
*/
|
|
264
436
|
static create(discordModule, options = {}) {
|
|
265
437
|
a1 = true;
|
|
266
438
|
const app = new DiscordApp();
|
|
@@ -271,16 +443,3 @@ class DiscordApp {
|
|
|
271
443
|
}
|
|
272
444
|
}
|
|
273
445
|
exports.DiscordApp = DiscordApp;
|
|
274
|
-
function mergeDefault(def, given) {
|
|
275
|
-
if (!given)
|
|
276
|
-
return def;
|
|
277
|
-
for (const key in def) {
|
|
278
|
-
if (!Object.hasOwn(given, key) || given[key] === undefined) {
|
|
279
|
-
given[key] = def[key];
|
|
280
|
-
}
|
|
281
|
-
else if (given[key] === Object(given[key])) {
|
|
282
|
-
given[key] = mergeDefault(def[key], given[key]);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
return given;
|
|
286
|
-
}
|
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
import { ButtonBuilder } from "discord.js";
|
|
2
|
+
import { ButtonOptions } from "..";
|
|
3
|
+
/**
|
|
4
|
+
* The base class for buttons created with the {@link Button} decorator. This class is used to create a button from the options defined in the class, and to override the default options with new options when creating the button.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { ButtonInteraction, ButtonStyle } from "discord.js";
|
|
9
|
+
* import { Button, BaseButton, Interaction, Run } from "oopscord.js";
|
|
10
|
+
*
|
|
11
|
+
* @Button({
|
|
12
|
+
* customId: "my_button",
|
|
13
|
+
* label: "Click me!",
|
|
14
|
+
* style: ButtonStyle.Primary
|
|
15
|
+
* })
|
|
16
|
+
* class MyButton extends BaseButton {
|
|
17
|
+
* constructor(@Interaction readonly interaction: ButtonInteraction) {
|
|
18
|
+
* super();
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @Run
|
|
22
|
+
* async run() {
|
|
23
|
+
* await this.interaction.reply("You clicked the button!");
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
2
28
|
export declare class BaseButton {
|
|
3
|
-
static
|
|
29
|
+
private static options;
|
|
30
|
+
private static button;
|
|
31
|
+
/** Returns {@link ButtonBuilder} that was created from the button options. */
|
|
4
32
|
static getButton(): ButtonBuilder;
|
|
33
|
+
/**
|
|
34
|
+
* Returns a new {@link ButtonBuilder} with the specified options merged with the default options defined in the class.
|
|
35
|
+
*
|
|
36
|
+
* @param options The options to merge with the default options.
|
|
37
|
+
*/
|
|
38
|
+
static override<T extends ButtonOptions>(options: Partial<T>): ButtonBuilder;
|
|
5
39
|
}
|
|
6
40
|
//# sourceMappingURL=BaseButton.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseButton.d.ts","sourceRoot":"","sources":["../../../src/structures/components/BaseButton.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAe,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"BaseButton.d.ts","sourceRoot":"","sources":["../../../src/structures/components/BaseButton.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAe,MAAM,YAAY,CAAC;AACxD,OAAO,EAA8B,aAAa,EAAE,MAAM,IAAI,CAAC;AAG/D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,UAAU;IACnB,OAAO,CAAC,MAAM,CAAC,OAAO,CAAgB;IACtC,OAAO,CAAC,MAAM,CAAC,MAAM,CAA8B;IAEnD,8EAA8E;IAC9E,MAAM,CAAC,SAAS;IAiBhB;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,aAAa,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;CAS/D"}
|
|
@@ -3,39 +3,85 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BaseButton = void 0;
|
|
4
4
|
const discord_js_1 = require("discord.js");
|
|
5
5
|
const __1 = require("..");
|
|
6
|
+
const Util_1 = require("../../utils/Util");
|
|
7
|
+
/**
|
|
8
|
+
* The base class for buttons created with the {@link Button} decorator. This class is used to create a button from the options defined in the class, and to override the default options with new options when creating the button.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { ButtonInteraction, ButtonStyle } from "discord.js";
|
|
13
|
+
* import { Button, BaseButton, Interaction, Run } from "oopscord.js";
|
|
14
|
+
*
|
|
15
|
+
* @Button({
|
|
16
|
+
* customId: "my_button",
|
|
17
|
+
* label: "Click me!",
|
|
18
|
+
* style: ButtonStyle.Primary
|
|
19
|
+
* })
|
|
20
|
+
* class MyButton extends BaseButton {
|
|
21
|
+
* constructor(@Interaction readonly interaction: ButtonInteraction) {
|
|
22
|
+
* super();
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* @Run
|
|
26
|
+
* async run() {
|
|
27
|
+
* await this.interaction.reply("You clicked the button!");
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
6
32
|
class BaseButton {
|
|
33
|
+
static options;
|
|
7
34
|
static button = null;
|
|
35
|
+
/** Returns {@link ButtonBuilder} that was created from the button options. */
|
|
8
36
|
static getButton() {
|
|
9
37
|
if (this.button)
|
|
10
38
|
return this.button;
|
|
11
|
-
const button = new discord_js_1.ButtonBuilder();
|
|
12
39
|
const options = Reflect.getMetadata(__1.BUTTON_OPTIONS_KEY, this);
|
|
13
40
|
if (!options)
|
|
14
41
|
throw new TypeError("Failed to get the button.");
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
.setStyle(options.style)
|
|
18
|
-
.setSKUId(options.skuId);
|
|
19
|
-
return button;
|
|
20
|
-
}
|
|
21
|
-
if (options.style == discord_js_1.ButtonStyle.Link) {
|
|
22
|
-
button
|
|
23
|
-
.setStyle(options.style)
|
|
24
|
-
.setURL(options.url);
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
button
|
|
28
|
-
.setStyle(options.style)
|
|
29
|
-
.setCustomId(options.customId);
|
|
30
|
-
}
|
|
31
|
-
if (options.label)
|
|
32
|
-
button.setLabel(options.label);
|
|
33
|
-
if (options.emoji)
|
|
34
|
-
button.setEmoji(options.emoji);
|
|
35
|
-
if (options.disabled)
|
|
36
|
-
button.setDisabled(options.disabled);
|
|
42
|
+
this.options = options;
|
|
43
|
+
const button = create(options);
|
|
37
44
|
this.button = button;
|
|
38
|
-
|
|
45
|
+
// Return a copy of the button to prevent accidental mutations to the original button stored in the class.
|
|
46
|
+
return discord_js_1.ButtonBuilder.from(button.toJSON());
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns a new {@link ButtonBuilder} with the specified options merged with the default options defined in the class.
|
|
50
|
+
*
|
|
51
|
+
* @param options The options to merge with the default options.
|
|
52
|
+
*/
|
|
53
|
+
static override(options) {
|
|
54
|
+
const currentOptions = this.options ?? Reflect.getMetadata(__1.BUTTON_OPTIONS_KEY, this);
|
|
55
|
+
if (!currentOptions)
|
|
56
|
+
throw new TypeError("Failed to get the button options.");
|
|
57
|
+
const newOptions = Util_1.Util.mergeDefault(currentOptions, options);
|
|
58
|
+
return create(newOptions);
|
|
39
59
|
}
|
|
40
60
|
}
|
|
41
61
|
exports.BaseButton = BaseButton;
|
|
62
|
+
function create(options) {
|
|
63
|
+
const button = new discord_js_1.ButtonBuilder();
|
|
64
|
+
if (options.style == discord_js_1.ButtonStyle.Premium) {
|
|
65
|
+
button
|
|
66
|
+
.setStyle(options.style)
|
|
67
|
+
.setSKUId(options.skuId);
|
|
68
|
+
return button;
|
|
69
|
+
}
|
|
70
|
+
if (options.style == discord_js_1.ButtonStyle.Link) {
|
|
71
|
+
button
|
|
72
|
+
.setStyle(options.style)
|
|
73
|
+
.setURL(options.url);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
button
|
|
77
|
+
.setStyle(options.style)
|
|
78
|
+
.setCustomId(options.customId);
|
|
79
|
+
}
|
|
80
|
+
if (options.label)
|
|
81
|
+
button.setLabel(options.label);
|
|
82
|
+
if (options.emoji)
|
|
83
|
+
button.setEmoji(options.emoji);
|
|
84
|
+
if (options.disabled)
|
|
85
|
+
button.setDisabled(options.disabled);
|
|
86
|
+
return button;
|
|
87
|
+
}
|