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