commandkit 0.0.9 → 0.1.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/dist/index.d.mts +79 -0
- package/dist/index.d.ts +79 -1
- package/dist/index.js +652 -15
- package/dist/index.mjs +616 -0
- package/package.json +29 -5
- package/.github/workflows/publish.yaml +0 -26
- package/CHANGELOG.md +0 -52
- package/dist/CommandKit.d.ts +0 -36
- package/dist/CommandKit.js +0 -57
- package/dist/handlers/command-handler/CommandHandler.d.ts +0 -12
- package/dist/handlers/command-handler/CommandHandler.js +0 -62
- package/dist/handlers/command-handler/functions/handleCommands.d.ts +0 -2
- package/dist/handlers/command-handler/functions/handleCommands.js +0 -50
- package/dist/handlers/command-handler/functions/registerCommands.d.ts +0 -2
- package/dist/handlers/command-handler/functions/registerCommands.js +0 -135
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.d.ts +0 -1
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.js +0 -17
- package/dist/handlers/command-handler/validations/botPermissions.d.ts +0 -1
- package/dist/handlers/command-handler/validations/botPermissions.js +0 -17
- package/dist/handlers/command-handler/validations/devOnly.d.ts +0 -1
- package/dist/handlers/command-handler/validations/devOnly.js +0 -29
- package/dist/handlers/command-handler/validations/guildOnly.d.ts +0 -1
- package/dist/handlers/command-handler/validations/guildOnly.js +0 -11
- package/dist/handlers/command-handler/validations/userPermissions.d.ts +0 -1
- package/dist/handlers/command-handler/validations/userPermissions.js +0 -17
- package/dist/handlers/event-handler/EventHandler.d.ts +0 -11
- package/dist/handlers/event-handler/EventHandler.js +0 -52
- package/dist/handlers/index.d.ts +0 -3
- package/dist/handlers/index.js +0 -19
- package/dist/handlers/validation-handler/ValidationHandler.d.ts +0 -7
- package/dist/handlers/validation-handler/ValidationHandler.js +0 -29
- package/dist/utils/get-paths.d.ts +0 -3
- package/dist/utils/get-paths.js +0 -42
- package/tsconfig.json +0 -14
- package/typings.d.ts +0 -63
package/dist/index.js
CHANGED
|
@@ -1,17 +1,654 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
11
|
};
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
CommandKit: () => CommandKit,
|
|
34
|
+
CommandType: () => CommandType
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
|
37
|
+
|
|
38
|
+
// src/utils/get-paths.ts
|
|
39
|
+
var import_path = __toESM(require("path"));
|
|
40
|
+
var import_fs = __toESM(require("fs"));
|
|
41
|
+
function getFilePaths(directory, nesting) {
|
|
42
|
+
let filePaths = [];
|
|
43
|
+
if (!directory)
|
|
44
|
+
return filePaths;
|
|
45
|
+
const files = import_fs.default.readdirSync(directory, { withFileTypes: true });
|
|
46
|
+
for (const file of files) {
|
|
47
|
+
const filePath = import_path.default.join(directory, file.name);
|
|
48
|
+
if (file.isFile()) {
|
|
49
|
+
filePaths.push(filePath);
|
|
50
|
+
}
|
|
51
|
+
if (nesting && file.isDirectory()) {
|
|
52
|
+
filePaths = [...filePaths, ...getFilePaths(filePath, true)];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return filePaths;
|
|
56
|
+
}
|
|
57
|
+
function getFolderPaths(directory, nesting) {
|
|
58
|
+
let folderPaths = [];
|
|
59
|
+
if (!directory)
|
|
60
|
+
return folderPaths;
|
|
61
|
+
const folders = import_fs.default.readdirSync(directory, { withFileTypes: true });
|
|
62
|
+
for (const folder of folders) {
|
|
63
|
+
const folderPath = import_path.default.join(directory, folder.name);
|
|
64
|
+
if (folder.isDirectory()) {
|
|
65
|
+
folderPaths.push(folderPath);
|
|
66
|
+
if (nesting) {
|
|
67
|
+
folderPaths = [...folderPaths, ...getFolderPaths(folderPath, true)];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return folderPaths;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/utils/resolve-file-url.ts
|
|
75
|
+
var import_path2 = __toESM(require("path"));
|
|
76
|
+
function toFileURL(filePath) {
|
|
77
|
+
const resolvedPath = import_path2.default.resolve(filePath);
|
|
78
|
+
return "file://" + resolvedPath.replace(/\\/g, "/");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/handlers/command-handler/validations/botPermissions.ts
|
|
82
|
+
function botPermissions_default({ interaction, targetCommand }) {
|
|
83
|
+
const botMember = interaction.guild?.members.me;
|
|
84
|
+
let commandPermissions = targetCommand.options?.botPermissions;
|
|
85
|
+
if (!botMember || !commandPermissions)
|
|
86
|
+
return;
|
|
87
|
+
if (!Array.isArray(commandPermissions)) {
|
|
88
|
+
commandPermissions = [commandPermissions];
|
|
89
|
+
}
|
|
90
|
+
const missingPermissions = [];
|
|
91
|
+
for (const permission of commandPermissions) {
|
|
92
|
+
const hasPermission = botMember.permissions.has(permission);
|
|
93
|
+
if (!hasPermission) {
|
|
94
|
+
missingPermissions.push(`\`${permission.toString()}\``);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (missingPermissions.length) {
|
|
98
|
+
interaction.reply({
|
|
99
|
+
content: `\u274C I do not have enough permissions to execute this command. Missing: ${missingPermissions.join(
|
|
100
|
+
", "
|
|
101
|
+
)}`,
|
|
102
|
+
ephemeral: true
|
|
103
|
+
});
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/handlers/command-handler/validations/devOnly.ts
|
|
109
|
+
function devOnly_default({ interaction, targetCommand, handlerData }) {
|
|
110
|
+
if (targetCommand.options?.devOnly) {
|
|
111
|
+
if (interaction.inGuild() && !handlerData.devGuildIds.includes(interaction.guildId)) {
|
|
112
|
+
interaction.reply({
|
|
113
|
+
content: "\u274C This command can only be used inside development servers.",
|
|
114
|
+
ephemeral: true
|
|
115
|
+
});
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
const guildMember = interaction.guild?.members.cache.get(interaction.user.id);
|
|
119
|
+
const memberRoles = guildMember?.roles.cache;
|
|
120
|
+
let hasDevRole = false;
|
|
121
|
+
memberRoles?.forEach((role) => {
|
|
122
|
+
if (handlerData.devRoleIds?.includes(role.id)) {
|
|
123
|
+
hasDevRole = true;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
const isDevUser = handlerData.devUserIds.includes(interaction.user.id) || hasDevRole;
|
|
127
|
+
if (!isDevUser) {
|
|
128
|
+
interaction.reply({
|
|
129
|
+
content: "\u274C This command can only be used by developers.",
|
|
130
|
+
ephemeral: true
|
|
131
|
+
});
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/handlers/command-handler/validations/guildOnly.ts
|
|
138
|
+
function guildOnly_default({ interaction, targetCommand }) {
|
|
139
|
+
if (targetCommand.options?.guildOnly && !interaction.inGuild()) {
|
|
140
|
+
interaction.reply({
|
|
141
|
+
content: "\u274C This command can only be used inside a server.",
|
|
142
|
+
ephemeral: true
|
|
143
|
+
});
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/handlers/command-handler/validations/userPermissions.ts
|
|
149
|
+
function userPermissions_default({ interaction, targetCommand }) {
|
|
150
|
+
const memberPermissions = interaction.memberPermissions;
|
|
151
|
+
let commandPermissions = targetCommand.options?.userPermissions;
|
|
152
|
+
if (!memberPermissions || !commandPermissions)
|
|
153
|
+
return;
|
|
154
|
+
if (!Array.isArray(commandPermissions)) {
|
|
155
|
+
commandPermissions = [commandPermissions];
|
|
156
|
+
}
|
|
157
|
+
const missingPermissions = [];
|
|
158
|
+
for (const permission of commandPermissions) {
|
|
159
|
+
const hasPermission = memberPermissions.has(permission);
|
|
160
|
+
if (!hasPermission) {
|
|
161
|
+
missingPermissions.push(`\`${permission.toString()}\``);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (missingPermissions.length) {
|
|
165
|
+
interaction.reply({
|
|
166
|
+
content: `\u274C You do not have enough permissions to run this command. Missing: ${missingPermissions.join(
|
|
167
|
+
", "
|
|
168
|
+
)}`,
|
|
169
|
+
ephemeral: true
|
|
170
|
+
});
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/handlers/command-handler/validations/index.ts
|
|
176
|
+
var validations_default = [botPermissions_default, devOnly_default, guildOnly_default, userPermissions_default];
|
|
177
|
+
|
|
178
|
+
// src/handlers/command-handler/utils/areSlashCommandsDifferent.ts
|
|
179
|
+
function areSlashCommandsDifferent(appCommand, localCommand) {
|
|
180
|
+
if (!appCommand.options)
|
|
181
|
+
appCommand.options = [];
|
|
182
|
+
if (!localCommand.options)
|
|
183
|
+
localCommand.options = [];
|
|
184
|
+
if (!appCommand.description)
|
|
185
|
+
appCommand.description = "";
|
|
186
|
+
if (!localCommand.description)
|
|
187
|
+
localCommand.description = "";
|
|
188
|
+
if (localCommand.description !== appCommand.description || localCommand.options.length !== appCommand.options.length) {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// src/handlers/command-handler/functions/registerCommands.ts
|
|
194
|
+
var import_safe = __toESM(require("colors/safe"));
|
|
195
|
+
async function registerCommands(commandHandler) {
|
|
196
|
+
const client = commandHandler._data.client;
|
|
197
|
+
const devGuildIds = commandHandler._data.devGuildIds;
|
|
198
|
+
const commands = commandHandler._data.commands;
|
|
199
|
+
client.once("ready", async () => {
|
|
200
|
+
const devGuilds = [];
|
|
201
|
+
for (const devGuildId of devGuildIds) {
|
|
202
|
+
const guild = client.guilds.cache.get(devGuildId);
|
|
203
|
+
if (!guild) {
|
|
204
|
+
console.log(
|
|
205
|
+
import_safe.default.yellow(
|
|
206
|
+
`\u23E9 Ignoring: Guild ${devGuildId} does not exist or client isn't in this guild.`
|
|
207
|
+
)
|
|
208
|
+
);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
devGuilds.push(guild);
|
|
212
|
+
}
|
|
213
|
+
const appCommands = client.application?.commands;
|
|
214
|
+
await appCommands?.fetch();
|
|
215
|
+
const devGuildCommands = [];
|
|
216
|
+
for (const guild of devGuilds) {
|
|
217
|
+
const guildCommands = guild.commands;
|
|
218
|
+
await guildCommands?.fetch();
|
|
219
|
+
devGuildCommands.push(guildCommands);
|
|
220
|
+
}
|
|
221
|
+
for (const command of commands) {
|
|
222
|
+
let commandData = command.data;
|
|
223
|
+
if (command.options?.deleted) {
|
|
224
|
+
const targetCommand = appCommands?.cache.find(
|
|
225
|
+
(cmd) => cmd.name === commandData.name
|
|
226
|
+
);
|
|
227
|
+
if (!targetCommand) {
|
|
228
|
+
console.log(
|
|
229
|
+
import_safe.default.yellow(
|
|
230
|
+
`\u23E9 Ignoring: Command "${commandData.name}" is globally marked as deleted.`
|
|
231
|
+
)
|
|
232
|
+
);
|
|
233
|
+
} else {
|
|
234
|
+
targetCommand.delete().then(() => {
|
|
235
|
+
console.log(
|
|
236
|
+
import_safe.default.green(`\u{1F6AE} Deleted command "${commandData.name}" globally.`)
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
for (const guildCommands of devGuildCommands) {
|
|
241
|
+
const targetCommand2 = guildCommands.cache.find(
|
|
242
|
+
(cmd) => cmd.name === commandData.name
|
|
243
|
+
);
|
|
244
|
+
if (!targetCommand2) {
|
|
245
|
+
console.log(
|
|
246
|
+
import_safe.default.yellow(
|
|
247
|
+
`\u23E9 Ignoring: Command "${commandData.name}" is marked as deleted for ${guildCommands.guild.name}.`
|
|
248
|
+
)
|
|
249
|
+
);
|
|
250
|
+
} else {
|
|
251
|
+
targetCommand2.delete().then(() => {
|
|
252
|
+
console.log(
|
|
253
|
+
import_safe.default.green(
|
|
254
|
+
`\u{1F6AE} Deleted command "${commandData.name}" in ${guildCommands.guild.name}.`
|
|
255
|
+
)
|
|
256
|
+
);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
let editedCommand = false;
|
|
263
|
+
const appGlobalCommand = appCommands?.cache.find(
|
|
264
|
+
(cmd) => cmd.name === commandData.name
|
|
265
|
+
);
|
|
266
|
+
if (appGlobalCommand) {
|
|
267
|
+
const commandsAreDifferent = areSlashCommandsDifferent(
|
|
268
|
+
appGlobalCommand,
|
|
269
|
+
commandData
|
|
270
|
+
);
|
|
271
|
+
if (commandsAreDifferent) {
|
|
272
|
+
appGlobalCommand.edit(commandData).then(() => {
|
|
273
|
+
console.log(
|
|
274
|
+
import_safe.default.green(`\u2705 Edited command "${commandData.name}" globally.`)
|
|
275
|
+
);
|
|
276
|
+
}).catch((error) => {
|
|
277
|
+
console.log(
|
|
278
|
+
import_safe.default.red(
|
|
279
|
+
`\u274C Failed to edit command "${commandData.name}" globally.`
|
|
280
|
+
)
|
|
281
|
+
);
|
|
282
|
+
console.error(error);
|
|
283
|
+
});
|
|
284
|
+
editedCommand = true;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
for (const guildCommands of devGuildCommands) {
|
|
288
|
+
const appGuildCommand = guildCommands.cache.find(
|
|
289
|
+
(cmd) => cmd.name === commandData.name
|
|
290
|
+
);
|
|
291
|
+
if (appGuildCommand) {
|
|
292
|
+
const commandsAreDifferent = areSlashCommandsDifferent(
|
|
293
|
+
appGuildCommand,
|
|
294
|
+
commandData
|
|
295
|
+
);
|
|
296
|
+
if (commandsAreDifferent) {
|
|
297
|
+
appGuildCommand.edit(commandData).then(() => {
|
|
298
|
+
console.log(
|
|
299
|
+
import_safe.default.green(
|
|
300
|
+
`\u2705 Edited command "${commandData.name}" in ${guildCommands.guild.name}.`
|
|
301
|
+
)
|
|
302
|
+
);
|
|
303
|
+
}).catch((error) => {
|
|
304
|
+
console.log(
|
|
305
|
+
import_safe.default.red(
|
|
306
|
+
`\u274C Failed to edit command "${commandData.name}" in ${guildCommands.guild.name}.`
|
|
307
|
+
)
|
|
308
|
+
);
|
|
309
|
+
console.error(error);
|
|
310
|
+
});
|
|
311
|
+
editedCommand = true;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (editedCommand)
|
|
316
|
+
continue;
|
|
317
|
+
if (command.options?.devOnly) {
|
|
318
|
+
if (!devGuilds.length) {
|
|
319
|
+
console.log(
|
|
320
|
+
import_safe.default.yellow(
|
|
321
|
+
`\u23E9 Ignoring: Cannot register command "${commandData.name}" as no valid "devGuildIds" were provided.`
|
|
322
|
+
)
|
|
323
|
+
);
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
for (const guild of devGuilds) {
|
|
327
|
+
const cmdExists = guild.commands.cache.some(
|
|
328
|
+
(cmd) => cmd.name === commandData.name
|
|
329
|
+
);
|
|
330
|
+
if (cmdExists)
|
|
331
|
+
continue;
|
|
332
|
+
guild?.commands.create(commandData).then(() => {
|
|
333
|
+
console.log(
|
|
334
|
+
import_safe.default.green(
|
|
335
|
+
`\u2705 Registered command "${commandData.name}" in ${guild.name}.`
|
|
336
|
+
)
|
|
337
|
+
);
|
|
338
|
+
}).catch((error) => {
|
|
339
|
+
console.log(
|
|
340
|
+
import_safe.default.red(
|
|
341
|
+
`\u274C Failed to register command "${commandData.name}" in ${guild.name}.`
|
|
342
|
+
)
|
|
343
|
+
);
|
|
344
|
+
console.error(error);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
} else {
|
|
348
|
+
const cmdExists = appCommands?.cache.some((cmd) => cmd.name === commandData.name);
|
|
349
|
+
if (cmdExists)
|
|
350
|
+
continue;
|
|
351
|
+
appCommands?.create(commandData).then(() => {
|
|
352
|
+
console.log(
|
|
353
|
+
import_safe.default.green(`\u2705 Registered command "${commandData.name}" globally.`)
|
|
354
|
+
);
|
|
355
|
+
}).catch((error) => {
|
|
356
|
+
console.log(
|
|
357
|
+
import_safe.default.red(
|
|
358
|
+
`\u274C Failed to register command "${commandData.name}" globally.`
|
|
359
|
+
)
|
|
360
|
+
);
|
|
361
|
+
console.error(error);
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// src/handlers/command-handler/functions/handleCommands.ts
|
|
369
|
+
function handleCommands(commandHandler) {
|
|
370
|
+
const client = commandHandler._data.client;
|
|
371
|
+
const handler = commandHandler._data.commandKitInstance;
|
|
372
|
+
client.on("interactionCreate", async (interaction) => {
|
|
373
|
+
if (!interaction.isChatInputCommand() && !interaction.isContextMenuCommand())
|
|
374
|
+
return;
|
|
375
|
+
const targetCommand = commandHandler._data.commands.find(
|
|
376
|
+
(cmd) => cmd.data.name === interaction.commandName
|
|
377
|
+
);
|
|
378
|
+
if (!targetCommand)
|
|
379
|
+
return;
|
|
380
|
+
const { data, options, run, ...rest } = targetCommand;
|
|
381
|
+
const commandObj = {
|
|
382
|
+
data: targetCommand.data,
|
|
383
|
+
options: targetCommand.options,
|
|
384
|
+
...rest
|
|
385
|
+
};
|
|
386
|
+
let canRun = true;
|
|
387
|
+
for (const validationFunction of commandHandler._data.customValidations) {
|
|
388
|
+
const stopValidationLoop = await validationFunction({
|
|
389
|
+
interaction,
|
|
390
|
+
client,
|
|
391
|
+
commandObj,
|
|
392
|
+
handler
|
|
393
|
+
});
|
|
394
|
+
if (stopValidationLoop) {
|
|
395
|
+
canRun = false;
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
if (!canRun)
|
|
400
|
+
return;
|
|
401
|
+
if (!commandHandler._data.skipBuiltInValidations) {
|
|
402
|
+
for (const validation of commandHandler._data.builtInValidations) {
|
|
403
|
+
const stopValidationLoop = validation({
|
|
404
|
+
targetCommand,
|
|
405
|
+
interaction,
|
|
406
|
+
handlerData: commandHandler._data
|
|
407
|
+
});
|
|
408
|
+
if (stopValidationLoop) {
|
|
409
|
+
canRun = false;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (!canRun)
|
|
415
|
+
return;
|
|
416
|
+
targetCommand.run({ interaction, client, handler });
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// src/handlers/command-handler/CommandHandler.ts
|
|
421
|
+
var import_safe2 = __toESM(require("colors/safe"));
|
|
422
|
+
var CommandHandler = class {
|
|
423
|
+
_data;
|
|
424
|
+
constructor({ ...options }) {
|
|
425
|
+
this._data = {
|
|
426
|
+
...options,
|
|
427
|
+
builtInValidations: [],
|
|
428
|
+
commands: []
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
async init() {
|
|
432
|
+
await this.#buildCommands();
|
|
433
|
+
this.#buildValidations();
|
|
434
|
+
await this.#registerCommands();
|
|
435
|
+
this.#handleCommands();
|
|
436
|
+
}
|
|
437
|
+
async #buildCommands() {
|
|
438
|
+
const commandFilePaths = getFilePaths(this._data.commandsPath, true).filter(
|
|
439
|
+
(path3) => path3.endsWith(".js") || path3.endsWith(".ts")
|
|
440
|
+
);
|
|
441
|
+
for (const commandFilePath of commandFilePaths) {
|
|
442
|
+
const modulePath = toFileURL(commandFilePath);
|
|
443
|
+
let commandObj = await import(modulePath);
|
|
444
|
+
const compactFilePath = commandFilePath.split(process.cwd())[1] || commandFilePath;
|
|
445
|
+
if (commandObj.default)
|
|
446
|
+
commandObj = commandObj.default;
|
|
447
|
+
if (!commandObj.data) {
|
|
448
|
+
console.log(
|
|
449
|
+
import_safe2.default.yellow(
|
|
450
|
+
`\u23E9 Ignoring: Command ${compactFilePath} does not export "data".`
|
|
451
|
+
)
|
|
452
|
+
);
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
if (!commandObj.run) {
|
|
456
|
+
console.log(
|
|
457
|
+
import_safe2.default.yellow(`\u23E9 Ignoring: Command ${compactFilePath} does not export "run".`)
|
|
458
|
+
);
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
this._data.commands.push(commandObj);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
#buildValidations() {
|
|
465
|
+
for (const validationFunction of validations_default) {
|
|
466
|
+
this._data.builtInValidations.push(validationFunction);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
async #registerCommands() {
|
|
470
|
+
await registerCommands(this);
|
|
471
|
+
}
|
|
472
|
+
#handleCommands() {
|
|
473
|
+
handleCommands(this);
|
|
474
|
+
}
|
|
475
|
+
get commands() {
|
|
476
|
+
return this._data.commands;
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
// src/handlers/event-handler/EventHandler.ts
|
|
481
|
+
var import_safe3 = __toESM(require("colors/safe"));
|
|
482
|
+
var EventHandler = class {
|
|
483
|
+
#data;
|
|
484
|
+
constructor({ ...options }) {
|
|
485
|
+
this.#data = {
|
|
486
|
+
...options,
|
|
487
|
+
events: []
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
async init() {
|
|
491
|
+
await this.#buildEvents();
|
|
492
|
+
this.#registerEvents();
|
|
493
|
+
}
|
|
494
|
+
async #buildEvents() {
|
|
495
|
+
const eventFolderPaths = getFolderPaths(this.#data.eventsPath);
|
|
496
|
+
for (const eventFolderPath of eventFolderPaths) {
|
|
497
|
+
const eventName = eventFolderPath.replace(/\\/g, "/").split("/").pop();
|
|
498
|
+
const eventFilePaths = getFilePaths(eventFolderPath, true).filter(
|
|
499
|
+
(path3) => path3.endsWith(".js") || path3.endsWith(".ts")
|
|
500
|
+
);
|
|
501
|
+
const eventObj = {
|
|
502
|
+
name: eventName,
|
|
503
|
+
functions: []
|
|
504
|
+
};
|
|
505
|
+
this.#data.events.push(eventObj);
|
|
506
|
+
for (const eventFilePath of eventFilePaths) {
|
|
507
|
+
const modulePath = toFileURL(eventFilePath);
|
|
508
|
+
let eventFunction = (await import(modulePath)).default;
|
|
509
|
+
const compactFilePath = eventFilePath.split(process.cwd())[1] || eventFilePath;
|
|
510
|
+
if (typeof eventFunction !== "function") {
|
|
511
|
+
console.log(
|
|
512
|
+
import_safe3.default.yellow(
|
|
513
|
+
`\u23E9 Ignoring: Event ${compactFilePath} does not export a function.`
|
|
514
|
+
)
|
|
515
|
+
);
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
eventObj.functions.push(eventFunction);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
#registerEvents() {
|
|
523
|
+
const client = this.#data.client;
|
|
524
|
+
const handler = this.#data.commandKitInstance;
|
|
525
|
+
for (const eventObj of this.#data.events) {
|
|
526
|
+
client.on(eventObj.name, async (...params) => {
|
|
527
|
+
for (const eventFunction of eventObj.functions) {
|
|
528
|
+
const stopEventLoop = await eventFunction(...params, client, handler);
|
|
529
|
+
if (stopEventLoop) {
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
get events() {
|
|
537
|
+
return this.#data.events;
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
// src/handlers/validation-handler/ValidationHandler.ts
|
|
542
|
+
var import_safe4 = __toESM(require("colors/safe"));
|
|
543
|
+
var ValidationHandler = class {
|
|
544
|
+
#data;
|
|
545
|
+
constructor({ ...options }) {
|
|
546
|
+
this.#data = {
|
|
547
|
+
...options,
|
|
548
|
+
validations: []
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
async init() {
|
|
552
|
+
await this.#buildValidations();
|
|
553
|
+
}
|
|
554
|
+
async #buildValidations() {
|
|
555
|
+
const validationFilePaths = getFilePaths(this.#data.validationsPath, true).filter(
|
|
556
|
+
(path3) => path3.endsWith(".js") || path3.endsWith(".ts")
|
|
557
|
+
);
|
|
558
|
+
for (const validationFilePath of validationFilePaths) {
|
|
559
|
+
const modulePath = toFileURL(validationFilePath);
|
|
560
|
+
let validationFunction = (await import(modulePath)).default;
|
|
561
|
+
const compactFilePath = validationFilePath.split(process.cwd())[1] || validationFilePath;
|
|
562
|
+
if (typeof validationFunction !== "function") {
|
|
563
|
+
console.log(
|
|
564
|
+
import_safe4.default.yellow(
|
|
565
|
+
`\u23E9 Ignoring: Validation ${compactFilePath} does not export a function.`
|
|
566
|
+
)
|
|
567
|
+
);
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
this.#data.validations.push(validationFunction);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
get validations() {
|
|
574
|
+
return this.#data.validations;
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
// src/CommandKit.ts
|
|
579
|
+
var import_safe5 = __toESM(require("colors/safe"));
|
|
580
|
+
var CommandKit = class {
|
|
581
|
+
#data;
|
|
582
|
+
constructor({ ...options }) {
|
|
583
|
+
if (!options.client) {
|
|
584
|
+
throw new Error(import_safe5.default.red('"client" is required when instantiating CommandKit.'));
|
|
585
|
+
}
|
|
586
|
+
if (options.validationsPath && !options.commandsPath) {
|
|
587
|
+
throw new Error(
|
|
588
|
+
import_safe5.default.red('"commandsPath" is required when "validationsPath" is set.')
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
this.#data = {
|
|
592
|
+
...options,
|
|
593
|
+
commands: []
|
|
594
|
+
};
|
|
595
|
+
this.#init();
|
|
596
|
+
}
|
|
597
|
+
async #init() {
|
|
598
|
+
if (this.#data.eventsPath) {
|
|
599
|
+
const eventHandler = new EventHandler({
|
|
600
|
+
client: this.#data.client,
|
|
601
|
+
eventsPath: this.#data.eventsPath,
|
|
602
|
+
commandKitInstance: this
|
|
603
|
+
});
|
|
604
|
+
await eventHandler.init();
|
|
605
|
+
}
|
|
606
|
+
let validationFunctions = [];
|
|
607
|
+
if (this.#data.validationsPath) {
|
|
608
|
+
const validationHandler = new ValidationHandler({
|
|
609
|
+
validationsPath: this.#data.validationsPath
|
|
610
|
+
});
|
|
611
|
+
await validationHandler.init();
|
|
612
|
+
validationHandler.validations.forEach((v) => validationFunctions.push(v));
|
|
613
|
+
}
|
|
614
|
+
if (this.#data.commandsPath) {
|
|
615
|
+
const commandHandler = new CommandHandler({
|
|
616
|
+
client: this.#data.client,
|
|
617
|
+
commandsPath: this.#data.commandsPath,
|
|
618
|
+
devGuildIds: this.#data.devGuildIds || [],
|
|
619
|
+
devUserIds: this.#data.devUserIds || [],
|
|
620
|
+
devRoleIds: this.#data.devRoleIds || [],
|
|
621
|
+
customValidations: validationFunctions,
|
|
622
|
+
skipBuiltInValidations: this.#data.skipBuiltInValidations || false,
|
|
623
|
+
commandKitInstance: this
|
|
624
|
+
});
|
|
625
|
+
await commandHandler.init();
|
|
626
|
+
this.#data.commands = commandHandler.commands;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Returns all the commands that CommandKit is handling.
|
|
631
|
+
*
|
|
632
|
+
* @returns An array of command objects
|
|
633
|
+
*/
|
|
634
|
+
get commands() {
|
|
635
|
+
const commands = this.#data.commands.map((cmd) => {
|
|
636
|
+
const { run, ...command } = cmd;
|
|
637
|
+
return command;
|
|
638
|
+
});
|
|
639
|
+
return commands;
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
// src/types/index.ts
|
|
644
|
+
var CommandType = /* @__PURE__ */ ((CommandType2) => {
|
|
645
|
+
CommandType2[CommandType2["ChatInput"] = 1] = "ChatInput";
|
|
646
|
+
CommandType2[CommandType2["Message"] = 3] = "Message";
|
|
647
|
+
CommandType2[CommandType2["User"] = 2] = "User";
|
|
648
|
+
return CommandType2;
|
|
649
|
+
})(CommandType || {});
|
|
650
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
651
|
+
0 && (module.exports = {
|
|
652
|
+
CommandKit,
|
|
653
|
+
CommandType
|
|
654
|
+
});
|