commandkit 0.1.11-dev.20250306101654 → 0.1.11-dev.20250306152436
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.ts +110 -123
- package/dist/index.js +543 -288
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1975,14 +1975,23 @@ function findAppDirectory() {
|
|
|
1975
1975
|
}
|
|
1976
1976
|
function debounce(fn, ms2) {
|
|
1977
1977
|
let timer = null;
|
|
1978
|
+
let resolve = null;
|
|
1978
1979
|
return (...args) => {
|
|
1979
1980
|
if (timer) {
|
|
1980
1981
|
clearTimeout(timer);
|
|
1982
|
+
if (resolve) {
|
|
1983
|
+
resolve(null);
|
|
1984
|
+
}
|
|
1981
1985
|
}
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
timer =
|
|
1985
|
-
|
|
1986
|
+
return new Promise((res) => {
|
|
1987
|
+
resolve = res;
|
|
1988
|
+
timer = setTimeout(() => {
|
|
1989
|
+
const result = fn(...args);
|
|
1990
|
+
res(result);
|
|
1991
|
+
timer = null;
|
|
1992
|
+
resolve = null;
|
|
1993
|
+
}, ms2);
|
|
1994
|
+
});
|
|
1986
1995
|
};
|
|
1987
1996
|
}
|
|
1988
1997
|
var import_node_fs, import_node_path2, appDir;
|
|
@@ -2946,6 +2955,9 @@ var init_CommandRegistrar = __esm({
|
|
|
2946
2955
|
const commands = this.commandkit.commandHandler.getCommandsArray();
|
|
2947
2956
|
return commands.flatMap((cmd) => {
|
|
2948
2957
|
const json = "toJSON" in cmd.data.command ? cmd.data.command.toJSON() : cmd.data.command;
|
|
2958
|
+
if (cmd.subcommands && cmd.subcommands.length > 0 && (!json.type || json.type === import_discord9.ApplicationCommandType.ChatInput)) {
|
|
2959
|
+
this.processSubcommands(json, cmd.subcommands);
|
|
2960
|
+
}
|
|
2949
2961
|
const collections = [json];
|
|
2950
2962
|
if (cmd.data.userContextMenu && json.type !== import_discord9.ApplicationCommandType.User) {
|
|
2951
2963
|
collections.push({
|
|
@@ -2970,6 +2982,70 @@ var init_CommandRegistrar = __esm({
|
|
|
2970
2982
|
return collections;
|
|
2971
2983
|
});
|
|
2972
2984
|
}
|
|
2985
|
+
/**
|
|
2986
|
+
* Process and merge subcommands into the parent command's options
|
|
2987
|
+
*/
|
|
2988
|
+
processSubcommands(commandData, subcommands) {
|
|
2989
|
+
var _a, _b, _c;
|
|
2990
|
+
if (!commandData.options) {
|
|
2991
|
+
commandData.options = [];
|
|
2992
|
+
}
|
|
2993
|
+
const groupedSubcommands = /* @__PURE__ */ new Map();
|
|
2994
|
+
for (const subcommand of subcommands) {
|
|
2995
|
+
const group = subcommand.group || null;
|
|
2996
|
+
if (!groupedSubcommands.has(group)) {
|
|
2997
|
+
groupedSubcommands.set(group, []);
|
|
2998
|
+
}
|
|
2999
|
+
(_a = groupedSubcommands.get(group)) == null ? void 0 : _a.push(subcommand);
|
|
3000
|
+
}
|
|
3001
|
+
const directSubcommands = groupedSubcommands.get(null) || [];
|
|
3002
|
+
for (const subcommand of directSubcommands) {
|
|
3003
|
+
const subcommandData = this.commandkit.commandHandler.getSubcommandData(
|
|
3004
|
+
subcommand.id
|
|
3005
|
+
);
|
|
3006
|
+
if (subcommandData) {
|
|
3007
|
+
const subcommandOption = {
|
|
3008
|
+
type: 1,
|
|
3009
|
+
// SUB_COMMAND
|
|
3010
|
+
name: subcommand.name,
|
|
3011
|
+
description: subcommandData.description || "No description provided",
|
|
3012
|
+
options: subcommandData.options || [],
|
|
3013
|
+
name_localizations: subcommandData.name_localizations,
|
|
3014
|
+
description_localizations: subcommandData.description_localizations
|
|
3015
|
+
};
|
|
3016
|
+
commandData.options.push(subcommandOption);
|
|
3017
|
+
}
|
|
3018
|
+
}
|
|
3019
|
+
for (const [groupName, groupSubcommands] of groupedSubcommands.entries()) {
|
|
3020
|
+
if (groupName === null) continue;
|
|
3021
|
+
const subcommandGroupOption = {
|
|
3022
|
+
type: import_discord9.ApplicationCommandOptionType.SubcommandGroup,
|
|
3023
|
+
// SUB_COMMAND_GROUP
|
|
3024
|
+
name: groupName,
|
|
3025
|
+
description: `${groupName} commands`,
|
|
3026
|
+
options: []
|
|
3027
|
+
};
|
|
3028
|
+
for (const subcommand of groupSubcommands) {
|
|
3029
|
+
const subcommandData = this.commandkit.commandHandler.getSubcommandData(
|
|
3030
|
+
subcommand.id
|
|
3031
|
+
);
|
|
3032
|
+
if (subcommandData) {
|
|
3033
|
+
(_b = subcommandGroupOption.options) == null ? void 0 : _b.push({
|
|
3034
|
+
type: import_discord9.ApplicationCommandOptionType.Subcommand,
|
|
3035
|
+
// SUB_COMMAND
|
|
3036
|
+
name: subcommand.name,
|
|
3037
|
+
description: subcommandData.description || "No description provided",
|
|
3038
|
+
options: subcommandData.options || [],
|
|
3039
|
+
name_localizations: subcommandData.name_localizations,
|
|
3040
|
+
description_localizations: subcommandData.description_localizations
|
|
3041
|
+
});
|
|
3042
|
+
}
|
|
3043
|
+
}
|
|
3044
|
+
if ((((_c = subcommandGroupOption.options) == null ? void 0 : _c.length) || 0) > 0) {
|
|
3045
|
+
commandData.options.push(subcommandGroupOption);
|
|
3046
|
+
}
|
|
3047
|
+
}
|
|
3048
|
+
}
|
|
2973
3049
|
/**
|
|
2974
3050
|
* Registers loaded commands.
|
|
2975
3051
|
*/
|
|
@@ -3085,14 +3161,26 @@ var init_AppCommandHandler = __esm({
|
|
|
3085
3161
|
this.registrar = new CommandRegistrar(this.commandkit);
|
|
3086
3162
|
}
|
|
3087
3163
|
loadedCommands = new import_discord10.Collection();
|
|
3164
|
+
loadedSubCommands = new import_discord10.Collection();
|
|
3088
3165
|
loadedMiddlewares = new import_discord10.Collection();
|
|
3166
|
+
// Name-to-ID mapping for easier lookup
|
|
3167
|
+
commandNameToId = /* @__PURE__ */ new Map();
|
|
3168
|
+
subcommandPathToId = /* @__PURE__ */ new Map();
|
|
3089
3169
|
registrar;
|
|
3090
3170
|
onInteraction = null;
|
|
3091
3171
|
onMessageCreate = null;
|
|
3092
3172
|
onMessageUpdate = null;
|
|
3093
3173
|
getCommandsArray() {
|
|
3094
|
-
|
|
3095
|
-
|
|
3174
|
+
return Array.from(this.loadedCommands.values());
|
|
3175
|
+
}
|
|
3176
|
+
/**
|
|
3177
|
+
* Get subcommand data by ID for the command registrar
|
|
3178
|
+
*/
|
|
3179
|
+
getSubcommandData(id) {
|
|
3180
|
+
const subcommand = this.loadedSubCommands.get(id);
|
|
3181
|
+
if (!subcommand) return null;
|
|
3182
|
+
const commandData = subcommand.data.command;
|
|
3183
|
+
return "toJSON" in commandData ? commandData.toJSON() : commandData;
|
|
3096
3184
|
}
|
|
3097
3185
|
registerCommandHandler() {
|
|
3098
3186
|
this.onInteraction ??= async (interaction) => {
|
|
@@ -3104,9 +3192,9 @@ var init_AppCommandHandler = __esm({
|
|
|
3104
3192
|
if (success) return;
|
|
3105
3193
|
const isCommandLike = interaction.isCommand() || interaction.isAutocomplete() || interaction.isUserContextMenuCommand() || interaction.isMessageContextMenuCommand();
|
|
3106
3194
|
if (!isCommandLike) return;
|
|
3107
|
-
const
|
|
3108
|
-
if (!
|
|
3109
|
-
return this.runCommand(
|
|
3195
|
+
const prepared = await this.prepareCommandRun(interaction);
|
|
3196
|
+
if (!prepared) return;
|
|
3197
|
+
return this.runCommand(prepared, interaction);
|
|
3110
3198
|
};
|
|
3111
3199
|
this.onMessageCreate ??= async (message) => {
|
|
3112
3200
|
const success = await this.commandkit.plugins.execute(
|
|
@@ -3116,9 +3204,9 @@ var init_AppCommandHandler = __esm({
|
|
|
3116
3204
|
);
|
|
3117
3205
|
if (success) return;
|
|
3118
3206
|
if (message.author.bot) return;
|
|
3119
|
-
const
|
|
3120
|
-
if (!
|
|
3121
|
-
return this.runCommand(
|
|
3207
|
+
const prepared = await this.prepareCommandRun(message);
|
|
3208
|
+
if (!prepared) return;
|
|
3209
|
+
return this.runCommand(prepared, message);
|
|
3122
3210
|
};
|
|
3123
3211
|
this.onMessageUpdate ??= async (oldMessage, newMessage) => {
|
|
3124
3212
|
const success = await this.commandkit.plugins.execute(
|
|
@@ -3133,9 +3221,9 @@ var init_AppCommandHandler = __esm({
|
|
|
3133
3221
|
if (success) return;
|
|
3134
3222
|
if (oldMessage.partial || newMessage.partial) return;
|
|
3135
3223
|
if (oldMessage.author.bot) return;
|
|
3136
|
-
const
|
|
3137
|
-
if (!
|
|
3138
|
-
return this.runCommand(
|
|
3224
|
+
const prepared = await this.prepareCommandRun(newMessage);
|
|
3225
|
+
if (!prepared) return;
|
|
3226
|
+
return this.runCommand(prepared, newMessage);
|
|
3139
3227
|
};
|
|
3140
3228
|
this.commandkit.client.on(import_discord10.Events.InteractionCreate, this.onInteraction);
|
|
3141
3229
|
this.commandkit.client.on(import_discord10.Events.MessageCreate, this.onMessageCreate);
|
|
@@ -3155,7 +3243,7 @@ var init_AppCommandHandler = __esm({
|
|
|
3155
3243
|
}
|
|
3156
3244
|
return null;
|
|
3157
3245
|
}
|
|
3158
|
-
async runCommand(
|
|
3246
|
+
async runCommand(prepared, source) {
|
|
3159
3247
|
if (source instanceof import_discord10.Message && (source.editedTimestamp || source.partial)) {
|
|
3160
3248
|
return;
|
|
3161
3249
|
}
|
|
@@ -3166,13 +3254,14 @@ var init_AppCommandHandler = __esm({
|
|
|
3166
3254
|
message: source instanceof import_discord10.Message ? source : null,
|
|
3167
3255
|
forwarded: false
|
|
3168
3256
|
});
|
|
3169
|
-
for (const middleware of
|
|
3257
|
+
for (const middleware of prepared.middlewares) {
|
|
3170
3258
|
await middleware.data.beforeExecute(ctx);
|
|
3171
3259
|
}
|
|
3172
|
-
const
|
|
3260
|
+
const targetData = prepared.subcommand ? prepared.subcommand.data : prepared.command.data;
|
|
3261
|
+
const fn = targetData[executionMode];
|
|
3173
3262
|
if (!fn) {
|
|
3174
3263
|
Logger.warn(
|
|
3175
|
-
`Command ${
|
|
3264
|
+
`Command ${prepared.command.command.name}${prepared.subcommand ? "/" + prepared.subcommand.subcommand.name : ""} has no handler for ${executionMode}`
|
|
3176
3265
|
);
|
|
3177
3266
|
}
|
|
3178
3267
|
if (fn) {
|
|
@@ -3180,7 +3269,7 @@ var init_AppCommandHandler = __esm({
|
|
|
3180
3269
|
const executeCommand = /* @__PURE__ */ __name(async () => fn(ctx.clone()), "executeCommand");
|
|
3181
3270
|
const res = await this.commandkit.plugins.execute(
|
|
3182
3271
|
async (ctx2, plugin) => {
|
|
3183
|
-
return plugin.executeCommand(ctx2, source,
|
|
3272
|
+
return plugin.executeCommand(ctx2, source, prepared, executeCommand);
|
|
3184
3273
|
}
|
|
3185
3274
|
);
|
|
3186
3275
|
if (!res) {
|
|
@@ -3190,12 +3279,15 @@ var init_AppCommandHandler = __esm({
|
|
|
3190
3279
|
Logger.error(e);
|
|
3191
3280
|
}
|
|
3192
3281
|
}
|
|
3193
|
-
for (const middleware of
|
|
3282
|
+
for (const middleware of prepared.middlewares) {
|
|
3194
3283
|
await middleware.data.afterExecute(ctx);
|
|
3195
3284
|
}
|
|
3196
3285
|
}
|
|
3197
3286
|
async prepareCommandRun(source) {
|
|
3198
|
-
|
|
3287
|
+
var _a;
|
|
3288
|
+
let cmdName;
|
|
3289
|
+
let subcommandGroupName = null;
|
|
3290
|
+
let subcommandName = null;
|
|
3199
3291
|
let parser2;
|
|
3200
3292
|
if (source instanceof import_discord10.Message) {
|
|
3201
3293
|
if (source.author.bot) return null;
|
|
@@ -3204,13 +3296,14 @@ var init_AppCommandHandler = __esm({
|
|
|
3204
3296
|
source,
|
|
3205
3297
|
Array.isArray(prefix) ? prefix : [prefix],
|
|
3206
3298
|
(command) => {
|
|
3207
|
-
var
|
|
3208
|
-
const
|
|
3209
|
-
|
|
3210
|
-
|
|
3299
|
+
var _a2, _b;
|
|
3300
|
+
const commandId2 = this.commandNameToId.get(command);
|
|
3301
|
+
if (!commandId2) return null;
|
|
3302
|
+
const loadedCommand2 = this.loadedCommands.get(commandId2);
|
|
3211
3303
|
if (!loadedCommand2) return null;
|
|
3212
|
-
if (source.guildId && ((
|
|
3304
|
+
if (source.guildId && ((_a2 = loadedCommand2.guilds) == null ? void 0 : _a2.length) && !loadedCommand2.guilds.includes(source.guildId)) {
|
|
3213
3305
|
return null;
|
|
3306
|
+
}
|
|
3214
3307
|
const json = "toJSON" in loadedCommand2.data.command ? loadedCommand2.data.command.toJSON() : loadedCommand2.data.command;
|
|
3215
3308
|
return ((_b = json.options) == null ? void 0 : _b.reduce(
|
|
3216
3309
|
(acc, opt) => {
|
|
@@ -3222,7 +3315,17 @@ var init_AppCommandHandler = __esm({
|
|
|
3222
3315
|
}
|
|
3223
3316
|
);
|
|
3224
3317
|
try {
|
|
3225
|
-
|
|
3318
|
+
const fullCommand = parser2.getFullCommand();
|
|
3319
|
+
const parts = fullCommand.split(" ");
|
|
3320
|
+
cmdName = parts[0];
|
|
3321
|
+
if (parts.length > 1) {
|
|
3322
|
+
if (parts.length === 3) {
|
|
3323
|
+
subcommandGroupName = parts[1];
|
|
3324
|
+
subcommandName = parts[2];
|
|
3325
|
+
} else {
|
|
3326
|
+
subcommandName = parts[1];
|
|
3327
|
+
}
|
|
3328
|
+
}
|
|
3226
3329
|
} catch (e) {
|
|
3227
3330
|
if (isErrorType(e, CommandKitErrorCodes.InvalidCommandPrefix)) {
|
|
3228
3331
|
return null;
|
|
@@ -3232,31 +3335,63 @@ var init_AppCommandHandler = __esm({
|
|
|
3232
3335
|
}
|
|
3233
3336
|
} else {
|
|
3234
3337
|
if (!source.isCommand()) return null;
|
|
3235
|
-
|
|
3338
|
+
cmdName = source.commandName;
|
|
3236
3339
|
if (source.isChatInputCommand()) {
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
if (subcommandGroup) {
|
|
3240
|
-
cmd += ` ${subcommandGroup}`;
|
|
3241
|
-
}
|
|
3242
|
-
if (subcommand) {
|
|
3243
|
-
cmd += ` ${subcommand}`;
|
|
3244
|
-
}
|
|
3340
|
+
subcommandGroupName = source.options.getSubcommandGroup(false);
|
|
3341
|
+
subcommandName = source.options.getSubcommand(false);
|
|
3245
3342
|
}
|
|
3246
3343
|
}
|
|
3247
|
-
const
|
|
3248
|
-
|
|
3249
|
-
|
|
3344
|
+
const commandId = this.commandNameToId.get(cmdName);
|
|
3345
|
+
if (!commandId) return null;
|
|
3346
|
+
const loadedCommand = this.loadedCommands.get(commandId);
|
|
3250
3347
|
if (!loadedCommand) return null;
|
|
3348
|
+
if (source instanceof import_discord10.CommandInteraction && source.guildId && ((_a = loadedCommand.guilds) == null ? void 0 : _a.length) && !loadedCommand.guilds.includes(source.guildId)) {
|
|
3349
|
+
return null;
|
|
3350
|
+
}
|
|
3351
|
+
let loadedSubCommand = null;
|
|
3352
|
+
if (subcommandName) {
|
|
3353
|
+
let subcommandPath = `${loadedCommand.command.id}/${subcommandName}`;
|
|
3354
|
+
if (subcommandGroupName) {
|
|
3355
|
+
subcommandPath = `${loadedCommand.command.id}/${subcommandGroupName}/${subcommandName}`;
|
|
3356
|
+
}
|
|
3357
|
+
const subCommandId = this.subcommandPathToId.get(subcommandPath);
|
|
3358
|
+
if (subCommandId) {
|
|
3359
|
+
loadedSubCommand = this.loadedSubCommands.get(subCommandId) || null;
|
|
3360
|
+
}
|
|
3361
|
+
}
|
|
3362
|
+
const middlewares = [];
|
|
3363
|
+
for (const middlewareId of loadedCommand.command.middlewares) {
|
|
3364
|
+
const middleware = this.loadedMiddlewares.get(middlewareId);
|
|
3365
|
+
if (middleware) {
|
|
3366
|
+
middlewares.push(middleware);
|
|
3367
|
+
}
|
|
3368
|
+
}
|
|
3369
|
+
if (loadedSubCommand) {
|
|
3370
|
+
for (const middlewareId of loadedSubCommand.subcommand.middlewares) {
|
|
3371
|
+
const existingIndex = middlewares.findIndex(
|
|
3372
|
+
(m) => m.middleware.id === middlewareId
|
|
3373
|
+
);
|
|
3374
|
+
if (existingIndex === -1) {
|
|
3375
|
+
const middleware = this.loadedMiddlewares.get(middlewareId);
|
|
3376
|
+
if (middleware) {
|
|
3377
|
+
middlewares.push(middleware);
|
|
3378
|
+
}
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3381
|
+
}
|
|
3251
3382
|
return {
|
|
3252
3383
|
command: loadedCommand,
|
|
3253
|
-
|
|
3384
|
+
subcommand: loadedSubCommand,
|
|
3385
|
+
middlewares,
|
|
3254
3386
|
messageCommandParser: parser2
|
|
3255
3387
|
};
|
|
3256
3388
|
}
|
|
3257
3389
|
async reloadCommands() {
|
|
3258
3390
|
this.loadedCommands.clear();
|
|
3391
|
+
this.loadedSubCommands.clear();
|
|
3259
3392
|
this.loadedMiddlewares.clear();
|
|
3393
|
+
this.commandNameToId.clear();
|
|
3394
|
+
this.subcommandPathToId.clear();
|
|
3260
3395
|
await this.loadCommands();
|
|
3261
3396
|
}
|
|
3262
3397
|
async loadCommands() {
|
|
@@ -3264,9 +3399,33 @@ var init_AppCommandHandler = __esm({
|
|
|
3264
3399
|
if (!commandsRouter) {
|
|
3265
3400
|
throw new Error("Commands router has not yet initialized");
|
|
3266
3401
|
}
|
|
3267
|
-
const { commands,
|
|
3268
|
-
for (const [id,
|
|
3269
|
-
|
|
3402
|
+
const { commands, subcommands, middlewares } = commandsRouter.getData();
|
|
3403
|
+
for (const [id, middleware] of middlewares) {
|
|
3404
|
+
await this.loadMiddleware(id, middleware);
|
|
3405
|
+
}
|
|
3406
|
+
for (const [id, command] of commands) {
|
|
3407
|
+
await this.loadCommand(id, command);
|
|
3408
|
+
}
|
|
3409
|
+
for (const [id, subcommand] of subcommands) {
|
|
3410
|
+
await this.loadSubcommand(id, subcommand);
|
|
3411
|
+
}
|
|
3412
|
+
for (const loadedSubCommand of this.loadedSubCommands.values()) {
|
|
3413
|
+
const pathSegments = loadedSubCommand.subcommand.path.split("/");
|
|
3414
|
+
const parentCommandName = pathSegments[pathSegments.length - 3];
|
|
3415
|
+
for (const loadedCommand of this.loadedCommands.values()) {
|
|
3416
|
+
if (loadedCommand.command.name === parentCommandName) {
|
|
3417
|
+
if (!loadedCommand.subcommands) {
|
|
3418
|
+
loadedCommand.subcommands = [];
|
|
3419
|
+
}
|
|
3420
|
+
loadedCommand.subcommands.push(loadedSubCommand.subcommand);
|
|
3421
|
+
break;
|
|
3422
|
+
}
|
|
3423
|
+
}
|
|
3424
|
+
}
|
|
3425
|
+
}
|
|
3426
|
+
async loadMiddleware(id, middleware) {
|
|
3427
|
+
try {
|
|
3428
|
+
const data = await import(`${toFileURL(middleware.path)}?t=${Date.now()}`);
|
|
3270
3429
|
let handlerCount = 0;
|
|
3271
3430
|
for (const [key, validator] of Object.entries(middlewareDataSchema)) {
|
|
3272
3431
|
if (data[key] && !await validator(data[key])) {
|
|
@@ -3281,13 +3440,35 @@ var init_AppCommandHandler = __esm({
|
|
|
3281
3440
|
`Invalid export for middleware ${id}: at least one handler function must be provided`
|
|
3282
3441
|
);
|
|
3283
3442
|
}
|
|
3284
|
-
this.loadedMiddlewares.set(id, { middleware
|
|
3443
|
+
this.loadedMiddlewares.set(id, { middleware, data });
|
|
3444
|
+
} catch (error) {
|
|
3445
|
+
Logger.error(
|
|
3446
|
+
`Failed to load middleware ${middleware.name} (${id})`,
|
|
3447
|
+
error
|
|
3448
|
+
);
|
|
3285
3449
|
}
|
|
3286
|
-
|
|
3287
|
-
|
|
3450
|
+
}
|
|
3451
|
+
async loadCommand(id, command) {
|
|
3452
|
+
try {
|
|
3453
|
+
if (command.path === null) {
|
|
3454
|
+
this.loadedCommands.set(id, {
|
|
3455
|
+
command,
|
|
3456
|
+
data: {
|
|
3457
|
+
command: {
|
|
3458
|
+
name: command.name,
|
|
3459
|
+
description: `${command.name} commands`,
|
|
3460
|
+
type: 1
|
|
3461
|
+
// CHAT_INPUT
|
|
3462
|
+
}
|
|
3463
|
+
}
|
|
3464
|
+
});
|
|
3465
|
+
this.commandNameToId.set(command.name, id);
|
|
3466
|
+
return;
|
|
3467
|
+
}
|
|
3468
|
+
const data = await import(`${toFileURL(command.path)}?t=${Date.now()}`);
|
|
3288
3469
|
if (!data.command) {
|
|
3289
3470
|
throw new Error(
|
|
3290
|
-
`Invalid export for command ${name}: no command definition found`
|
|
3471
|
+
`Invalid export for command ${command.name}: no command definition found`
|
|
3291
3472
|
);
|
|
3292
3473
|
}
|
|
3293
3474
|
let handlerCount = 0;
|
|
@@ -3295,19 +3476,19 @@ var init_AppCommandHandler = __esm({
|
|
|
3295
3476
|
if (key !== "command" && data[key]) handlerCount++;
|
|
3296
3477
|
if (data[key] && !await validator(data[key])) {
|
|
3297
3478
|
throw new Error(
|
|
3298
|
-
`Invalid export for command ${name}: ${key} does not match expected value`
|
|
3479
|
+
`Invalid export for command ${command.name}: ${key} does not match expected value`
|
|
3299
3480
|
);
|
|
3300
3481
|
}
|
|
3301
3482
|
}
|
|
3302
3483
|
if (handlerCount === 0) {
|
|
3303
3484
|
throw new Error(
|
|
3304
|
-
`Invalid export for command ${name}: at least one handler function must be provided`
|
|
3485
|
+
`Invalid export for command ${command.name}: at least one handler function must be provided`
|
|
3305
3486
|
);
|
|
3306
3487
|
}
|
|
3307
3488
|
const localizedCommand = await this.applyLocalizations({
|
|
3308
3489
|
...data.command
|
|
3309
3490
|
});
|
|
3310
|
-
this.loadedCommands.set(
|
|
3491
|
+
this.loadedCommands.set(id, {
|
|
3311
3492
|
command,
|
|
3312
3493
|
guilds: data.guilds,
|
|
3313
3494
|
data: {
|
|
@@ -3315,6 +3496,60 @@ var init_AppCommandHandler = __esm({
|
|
|
3315
3496
|
command: localizedCommand
|
|
3316
3497
|
}
|
|
3317
3498
|
});
|
|
3499
|
+
this.commandNameToId.set(command.name, id);
|
|
3500
|
+
} catch (error) {
|
|
3501
|
+
Logger.error(`Failed to load command ${command.name} (${id})`, error);
|
|
3502
|
+
}
|
|
3503
|
+
}
|
|
3504
|
+
async loadSubcommand(id, subcommand) {
|
|
3505
|
+
try {
|
|
3506
|
+
const data = await import(`${toFileURL(subcommand.path)}?t=${Date.now()}`);
|
|
3507
|
+
if (!data.command) {
|
|
3508
|
+
throw new Error(
|
|
3509
|
+
`Invalid export for subcommand ${subcommand.name}: no command definition found`
|
|
3510
|
+
);
|
|
3511
|
+
}
|
|
3512
|
+
let handlerCount = 0;
|
|
3513
|
+
for (const [key, validator] of Object.entries(commandDataSchema)) {
|
|
3514
|
+
if (key !== "command" && data[key]) handlerCount++;
|
|
3515
|
+
if (data[key] && !await validator(data[key])) {
|
|
3516
|
+
throw new Error(
|
|
3517
|
+
`Invalid export for subcommand ${subcommand.name}: ${key} does not match expected value`
|
|
3518
|
+
);
|
|
3519
|
+
}
|
|
3520
|
+
}
|
|
3521
|
+
if (handlerCount === 0) {
|
|
3522
|
+
throw new Error(
|
|
3523
|
+
`Invalid export for subcommand ${subcommand.name}: at least one handler function must be provided`
|
|
3524
|
+
);
|
|
3525
|
+
}
|
|
3526
|
+
const localizedCommand = await this.applyLocalizations({
|
|
3527
|
+
...data.command
|
|
3528
|
+
});
|
|
3529
|
+
this.loadedSubCommands.set(id, {
|
|
3530
|
+
subcommand,
|
|
3531
|
+
data: {
|
|
3532
|
+
...data,
|
|
3533
|
+
command: localizedCommand
|
|
3534
|
+
}
|
|
3535
|
+
});
|
|
3536
|
+
const parentCommandId = subcommand.id.split("/")[0];
|
|
3537
|
+
if (subcommand.group) {
|
|
3538
|
+
this.subcommandPathToId.set(
|
|
3539
|
+
`${parentCommandId}/${subcommand.group}/${subcommand.name}`,
|
|
3540
|
+
id
|
|
3541
|
+
);
|
|
3542
|
+
} else {
|
|
3543
|
+
this.subcommandPathToId.set(
|
|
3544
|
+
`${parentCommandId}/${subcommand.name}`,
|
|
3545
|
+
id
|
|
3546
|
+
);
|
|
3547
|
+
}
|
|
3548
|
+
} catch (error) {
|
|
3549
|
+
Logger.error(
|
|
3550
|
+
`Failed to load subcommand ${subcommand.name} (${id})`,
|
|
3551
|
+
error
|
|
3552
|
+
);
|
|
3318
3553
|
}
|
|
3319
3554
|
}
|
|
3320
3555
|
async applyLocalizations(command) {
|
|
@@ -3400,249 +3635,257 @@ var init_AppCommandHandler = __esm({
|
|
|
3400
3635
|
});
|
|
3401
3636
|
|
|
3402
3637
|
// src/app/router/CommandsRouter.ts
|
|
3403
|
-
var import_node_fs2, import_promises3, import_node_path3, _CommandsRouter, CommandsRouter;
|
|
3638
|
+
var import_discord11, import_node_fs2, import_promises3, import_node_path3, import_node_crypto3, _CommandsRouter, CommandsRouter;
|
|
3404
3639
|
var init_CommandsRouter = __esm({
|
|
3405
3640
|
"src/app/router/CommandsRouter.ts"() {
|
|
3406
3641
|
"use strict";
|
|
3407
3642
|
init_cjs_shims();
|
|
3643
|
+
import_discord11 = require("discord.js");
|
|
3408
3644
|
import_node_fs2 = require("fs");
|
|
3409
3645
|
import_promises3 = require("fs/promises");
|
|
3410
3646
|
import_node_path3 = __toESM(require("path"));
|
|
3647
|
+
import_node_crypto3 = require("crypto");
|
|
3411
3648
|
_CommandsRouter = class _CommandsRouter {
|
|
3412
|
-
/**
|
|
3413
|
-
* Creates a new CommandsRouter instance
|
|
3414
|
-
* @param options - Configuration options for the router
|
|
3415
|
-
*/
|
|
3416
3649
|
constructor(options) {
|
|
3417
3650
|
this.options = options;
|
|
3418
|
-
if (!options.entrypoint) {
|
|
3419
|
-
throw new Error("Entrypoint directory must be provided");
|
|
3420
|
-
}
|
|
3421
|
-
options.matcher ??= {};
|
|
3422
|
-
options.matcher.command ??= /command\.(m|c)?(j|t)sx?$/;
|
|
3423
|
-
options.matcher.middleware ??= /middleware\.(m|c)?(j|t)sx?$/;
|
|
3424
3651
|
}
|
|
3425
|
-
commands =
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
* Gets the configured entrypoint directory
|
|
3429
|
-
*/
|
|
3652
|
+
commands = new import_discord11.Collection();
|
|
3653
|
+
subcommands = new import_discord11.Collection();
|
|
3654
|
+
middlewares = new import_discord11.Collection();
|
|
3430
3655
|
get entrypoint() {
|
|
3431
3656
|
return this.options.entrypoint;
|
|
3432
3657
|
}
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
*/
|
|
3436
|
-
get matchers() {
|
|
3437
|
-
return this.options.matcher;
|
|
3658
|
+
isValidPath() {
|
|
3659
|
+
return (0, import_node_fs2.existsSync)(this.entrypoint);
|
|
3438
3660
|
}
|
|
3439
|
-
/**
|
|
3440
|
-
* Returns the cached data of the commands and middleware
|
|
3441
|
-
*/
|
|
3442
3661
|
getData() {
|
|
3443
3662
|
return {
|
|
3444
3663
|
commands: this.commands,
|
|
3445
|
-
|
|
3664
|
+
subcommands: this.subcommands,
|
|
3665
|
+
middlewares: this.middlewares
|
|
3446
3666
|
};
|
|
3447
3667
|
}
|
|
3448
|
-
|
|
3449
|
-
* Checks if the entrypoint path is valid
|
|
3450
|
-
*/
|
|
3451
|
-
isValidPath() {
|
|
3452
|
-
return (0, import_node_fs2.existsSync)(this.entrypoint);
|
|
3453
|
-
}
|
|
3454
|
-
/**
|
|
3455
|
-
* Executes a matcher against a file path
|
|
3456
|
-
* @param matcher - The matcher to use
|
|
3457
|
-
* @param path - The file path to test
|
|
3458
|
-
* @returns boolean indicating if the path matches
|
|
3459
|
-
*/
|
|
3460
|
-
execMatcher(matcher, path3) {
|
|
3461
|
-
if (typeof matcher === "string") {
|
|
3462
|
-
return path3.endsWith(matcher);
|
|
3463
|
-
}
|
|
3464
|
-
if (matcher instanceof RegExp) {
|
|
3465
|
-
return matcher.test(path3);
|
|
3466
|
-
}
|
|
3467
|
-
return matcher(path3);
|
|
3468
|
-
}
|
|
3469
|
-
/**
|
|
3470
|
-
* Converts an absolute path to a path relative to the entrypoint
|
|
3471
|
-
* @param path - The absolute path to convert
|
|
3472
|
-
*/
|
|
3473
|
-
resolveRelativePath(path3) {
|
|
3474
|
-
const regex = new RegExp(`^${this.entrypoint}`);
|
|
3475
|
-
return path3.replace(regex, "");
|
|
3476
|
-
}
|
|
3477
|
-
/**
|
|
3478
|
-
* Matches a command by name or path segments
|
|
3479
|
-
* @param commandOrSegment - Command name or array of path segments
|
|
3480
|
-
* @returns Matched command and its middlewares, or null if no match
|
|
3481
|
-
*/
|
|
3482
|
-
match(commandOrSegment) {
|
|
3483
|
-
if (!(commandOrSegment == null ? void 0 : commandOrSegment.length)) return null;
|
|
3484
|
-
if (Array.isArray(commandOrSegment) && commandOrSegment.length === 1) {
|
|
3485
|
-
commandOrSegment = commandOrSegment[0];
|
|
3486
|
-
}
|
|
3487
|
-
if (!Array.isArray(commandOrSegment) && !commandOrSegment.includes(" ")) {
|
|
3488
|
-
const command2 = this.commands.get(commandOrSegment);
|
|
3489
|
-
if (!command2) return null;
|
|
3490
|
-
const middlewares2 = command2.middlewares.map((id) => this.middlewares.get(id)).filter((m) => !!m);
|
|
3491
|
-
return {
|
|
3492
|
-
command: command2,
|
|
3493
|
-
middlewares: middlewares2
|
|
3494
|
-
};
|
|
3495
|
-
}
|
|
3496
|
-
const segments = Array.isArray(commandOrSegment) ? commandOrSegment : commandOrSegment.split(" ");
|
|
3497
|
-
const command = Array.from(this.commands.values()).find((cmd) => {
|
|
3498
|
-
const commandSegments = cmd.parentSegments;
|
|
3499
|
-
if (commandSegments.length !== segments.length) return false;
|
|
3500
|
-
return commandSegments.every((segment, index) => {
|
|
3501
|
-
if (/^\[.+\]$/.test(segment)) return true;
|
|
3502
|
-
return segment === segments[index];
|
|
3503
|
-
});
|
|
3504
|
-
});
|
|
3505
|
-
if (!command) return null;
|
|
3506
|
-
const middlewares = command.middlewares.map((id) => this.middlewares.get(id)).filter((m) => !!m);
|
|
3668
|
+
toJSON() {
|
|
3507
3669
|
return {
|
|
3508
|
-
|
|
3509
|
-
|
|
3670
|
+
commands: Object.fromEntries(this.commands.entries()),
|
|
3671
|
+
subcommands: Object.fromEntries(this.subcommands.entries()),
|
|
3672
|
+
middlewares: Object.fromEntries(this.middlewares.entries())
|
|
3510
3673
|
};
|
|
3511
3674
|
}
|
|
3512
|
-
/**
|
|
3513
|
-
* Reloads the commands tree by re-scanning the entrypoint directory
|
|
3514
|
-
*/
|
|
3515
|
-
async reload() {
|
|
3516
|
-
this.clear();
|
|
3517
|
-
return this.scan();
|
|
3518
|
-
}
|
|
3519
|
-
/**
|
|
3520
|
-
* Clears the internal commands and middleware maps
|
|
3521
|
-
*/
|
|
3522
3675
|
clear() {
|
|
3523
3676
|
this.commands.clear();
|
|
3677
|
+
this.subcommands.clear();
|
|
3524
3678
|
this.middlewares.clear();
|
|
3525
3679
|
}
|
|
3526
|
-
|
|
3527
|
-
* Scans the entrypoint directory for commands and middleware
|
|
3528
|
-
* @returns Promise resolving to the complete commands tree
|
|
3529
|
-
*/
|
|
3530
|
-
async scan() {
|
|
3680
|
+
async reload() {
|
|
3531
3681
|
this.clear();
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
);
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
const
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
fullPath: file,
|
|
3547
|
-
parent: parsedPath.parent,
|
|
3548
|
-
parentSegments: parsedPath.parentSegments,
|
|
3549
|
-
category: parsedPath.category,
|
|
3550
|
-
middlewares: []
|
|
3551
|
-
};
|
|
3552
|
-
this.commands.set(parsedPath.name, command);
|
|
3682
|
+
return this.scan();
|
|
3683
|
+
}
|
|
3684
|
+
async scan() {
|
|
3685
|
+
await this.scanDirectory(this.entrypoint);
|
|
3686
|
+
return this.toJSON();
|
|
3687
|
+
}
|
|
3688
|
+
async scanDirectory(dir, parentPath = "", categories = []) {
|
|
3689
|
+
const entries = await (0, import_promises3.readdir)(dir);
|
|
3690
|
+
const directoryStats = /* @__PURE__ */ new Map();
|
|
3691
|
+
for (const entry of entries) {
|
|
3692
|
+
if (entry.startsWith("_")) continue;
|
|
3693
|
+
const fullPath = import_node_path3.default.join(dir, entry);
|
|
3694
|
+
const stats = await (0, import_promises3.stat)(fullPath);
|
|
3695
|
+
directoryStats.set(entry, stats.isDirectory());
|
|
3553
3696
|
}
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
const
|
|
3559
|
-
const
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
};
|
|
3567
|
-
this.middlewares.set(id, middleware);
|
|
3568
|
-
const isGlobalMiddleware = import_node_path3.default.parse(file).name === "middleware";
|
|
3569
|
-
const commands = Array.from(this.commands.values());
|
|
3570
|
-
for (const command of commands) {
|
|
3571
|
-
const commandDir = import_node_path3.default.dirname(command.path);
|
|
3572
|
-
if (isGlobalMiddleware) {
|
|
3573
|
-
if (commandDir === dirname || commandDir.startsWith(dirname + import_node_path3.default.sep)) {
|
|
3574
|
-
command.middlewares.push(id);
|
|
3575
|
-
}
|
|
3697
|
+
await this.processMiddlewares(dir, entries, parentPath, categories);
|
|
3698
|
+
for (const entry of entries) {
|
|
3699
|
+
if (entry.startsWith("_")) continue;
|
|
3700
|
+
if (entry.includes(".middleware.")) continue;
|
|
3701
|
+
const fullPath = import_node_path3.default.join(dir, entry);
|
|
3702
|
+
const isDirectory = directoryStats.get(entry) || false;
|
|
3703
|
+
if (isDirectory) {
|
|
3704
|
+
const { name, parsedCategories } = this.parseNameAndCategory(entry);
|
|
3705
|
+
const newCategories = [...categories, ...parsedCategories];
|
|
3706
|
+
const isCategoryContainer = /^\([^)]+\)$/.test(entry);
|
|
3707
|
+
if (isCategoryContainer) {
|
|
3708
|
+
await this.scanDirectory(fullPath, parentPath, newCategories);
|
|
3576
3709
|
} else {
|
|
3577
|
-
const
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3710
|
+
const newParentPath = parentPath ? `${parentPath}/${name}` : name;
|
|
3711
|
+
await this.processCommandDirectory(
|
|
3712
|
+
fullPath,
|
|
3713
|
+
name,
|
|
3714
|
+
newParentPath,
|
|
3715
|
+
newCategories
|
|
3716
|
+
);
|
|
3717
|
+
}
|
|
3718
|
+
} else {
|
|
3719
|
+
if (!/\.(m|c)?(j|t)sx?$/.test(entry)) {
|
|
3720
|
+
continue;
|
|
3721
|
+
}
|
|
3722
|
+
const extname = import_node_path3.default.extname(entry);
|
|
3723
|
+
const basename = import_node_path3.default.basename(entry, extname);
|
|
3724
|
+
if (basename === "index") {
|
|
3725
|
+
continue;
|
|
3726
|
+
}
|
|
3727
|
+
const { name, parsedCategories } = this.parseNameAndCategory(basename);
|
|
3728
|
+
const newCategories = [...categories, ...parsedCategories];
|
|
3729
|
+
if (parentPath) {
|
|
3730
|
+
await this.processSubCommand(
|
|
3731
|
+
fullPath,
|
|
3732
|
+
name,
|
|
3733
|
+
parentPath,
|
|
3734
|
+
newCategories
|
|
3735
|
+
);
|
|
3736
|
+
} else {
|
|
3737
|
+
await this.processCommand(fullPath, name, newCategories);
|
|
3582
3738
|
}
|
|
3583
3739
|
}
|
|
3584
3740
|
}
|
|
3585
|
-
return this.toJSON();
|
|
3586
3741
|
}
|
|
3587
|
-
|
|
3588
|
-
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
};
|
|
3596
|
-
}
|
|
3597
|
-
/**
|
|
3598
|
-
* Recursively scans a directory for files
|
|
3599
|
-
* @param dir - Directory to scan
|
|
3600
|
-
* @param entries - Accumulator for found files
|
|
3601
|
-
* @returns Promise resolving to array of file paths
|
|
3602
|
-
*/
|
|
3603
|
-
async scanDirectory(dir, entries) {
|
|
3604
|
-
const files = await (0, import_promises3.readdir)(dir, {
|
|
3605
|
-
withFileTypes: true
|
|
3606
|
-
});
|
|
3607
|
-
for (const file of files) {
|
|
3608
|
-
if (file.name === "node_modules" || file.parentPath.includes("node_modules")) {
|
|
3742
|
+
async processMiddlewares(dir, entries, parentPath, categories) {
|
|
3743
|
+
for (const entry of entries) {
|
|
3744
|
+
if (entry.startsWith("_")) continue;
|
|
3745
|
+
if (!entry.includes(".middleware.")) continue;
|
|
3746
|
+
const fullPath = import_node_path3.default.join(dir, entry);
|
|
3747
|
+
const stats = await (0, import_promises3.stat)(fullPath);
|
|
3748
|
+
if (stats.isDirectory()) continue;
|
|
3749
|
+
if (!/\.(m|c)?(j|t)sx?$/.test(entry)) {
|
|
3609
3750
|
continue;
|
|
3610
3751
|
}
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3752
|
+
const extname = import_node_path3.default.extname(entry);
|
|
3753
|
+
const basename = import_node_path3.default.basename(entry, extname);
|
|
3754
|
+
const middlewareSegments = basename.split(".middleware");
|
|
3755
|
+
const middlewareName = middlewareSegments[0];
|
|
3756
|
+
const { name, parsedCategories } = this.parseNameAndCategory(middlewareName);
|
|
3757
|
+
const newCategories = [...categories, ...parsedCategories];
|
|
3758
|
+
const category = newCategories.length ? newCategories.join(":") : null;
|
|
3759
|
+
const middleware = {
|
|
3760
|
+
name,
|
|
3761
|
+
id: (0, import_node_crypto3.randomUUID)(),
|
|
3762
|
+
// Using randomUUID for unique ID generation
|
|
3763
|
+
path: fullPath,
|
|
3764
|
+
category
|
|
3765
|
+
};
|
|
3766
|
+
this.middlewares.set(middleware.id, middleware);
|
|
3767
|
+
}
|
|
3768
|
+
}
|
|
3769
|
+
async processCommand(filePath, name, categories) {
|
|
3770
|
+
const category = categories.length ? categories.join(":") : null;
|
|
3771
|
+
const id = (0, import_node_crypto3.randomUUID)();
|
|
3772
|
+
const middlewares = this.findAssociatedMiddlewares(name, category);
|
|
3773
|
+
const command = {
|
|
3774
|
+
id,
|
|
3775
|
+
name,
|
|
3776
|
+
path: filePath,
|
|
3777
|
+
category,
|
|
3778
|
+
subcommands: [],
|
|
3779
|
+
middlewares
|
|
3780
|
+
};
|
|
3781
|
+
this.commands.set(id, command);
|
|
3782
|
+
}
|
|
3783
|
+
async processCommandDirectory(dirPath, name, parentPath, categories) {
|
|
3784
|
+
const category = categories.length ? categories.join(":") : null;
|
|
3785
|
+
const id = (0, import_node_crypto3.randomUUID)();
|
|
3786
|
+
let indexFile = null;
|
|
3787
|
+
const possibleExtensions = [
|
|
3788
|
+
".js",
|
|
3789
|
+
".ts",
|
|
3790
|
+
".jsx",
|
|
3791
|
+
".tsx",
|
|
3792
|
+
".mjs",
|
|
3793
|
+
".cjs",
|
|
3794
|
+
".mts",
|
|
3795
|
+
".cts"
|
|
3796
|
+
];
|
|
3797
|
+
for (const ext of possibleExtensions) {
|
|
3798
|
+
const testPath = import_node_path3.default.join(dirPath, `index${ext}`);
|
|
3799
|
+
if ((0, import_node_fs2.existsSync)(testPath)) {
|
|
3800
|
+
indexFile = testPath;
|
|
3801
|
+
break;
|
|
3802
|
+
}
|
|
3803
|
+
}
|
|
3804
|
+
const middlewares = this.findAssociatedMiddlewares(name, category);
|
|
3805
|
+
const entries = await (0, import_promises3.readdir)(dirPath);
|
|
3806
|
+
const subcommands = [];
|
|
3807
|
+
for (const entry of entries) {
|
|
3808
|
+
if (entry.startsWith("_") || entry.startsWith("index.")) continue;
|
|
3809
|
+
const fullPath = import_node_path3.default.join(dirPath, entry);
|
|
3810
|
+
const stats = await (0, import_promises3.stat)(fullPath);
|
|
3811
|
+
const isDirectory = stats.isDirectory();
|
|
3812
|
+
if (!isDirectory && !/\.(m|c)?(j|t)sx?$/.test(entry)) {
|
|
3614
3813
|
continue;
|
|
3615
3814
|
}
|
|
3616
|
-
|
|
3815
|
+
if (!isDirectory || isDirectory && !entry.includes(".")) {
|
|
3816
|
+
const extname = isDirectory ? "" : import_node_path3.default.extname(entry);
|
|
3817
|
+
const baseName = isDirectory ? entry : import_node_path3.default.basename(entry, extname);
|
|
3818
|
+
if (!baseName.includes(".middleware")) {
|
|
3819
|
+
const { name: subName } = this.parseNameAndCategory(baseName);
|
|
3820
|
+
subcommands.push(subName);
|
|
3821
|
+
}
|
|
3822
|
+
}
|
|
3617
3823
|
}
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3824
|
+
const command = {
|
|
3825
|
+
id,
|
|
3826
|
+
name,
|
|
3827
|
+
path: indexFile,
|
|
3828
|
+
// This can be null if no index file exists
|
|
3829
|
+
category,
|
|
3830
|
+
subcommands,
|
|
3831
|
+
middlewares
|
|
3832
|
+
};
|
|
3833
|
+
this.commands.set(id, command);
|
|
3834
|
+
await this.scanDirectory(dirPath, id, categories);
|
|
3629
3835
|
}
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
const
|
|
3635
|
-
|
|
3636
|
-
)
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3836
|
+
async processSubCommand(filePath, name, parentPath, categories) {
|
|
3837
|
+
if (!/\.(m|c)?(j|t)sx?$/.test(filePath)) {
|
|
3838
|
+
return;
|
|
3839
|
+
}
|
|
3840
|
+
const pathSegments = parentPath.split("/");
|
|
3841
|
+
let group = null;
|
|
3842
|
+
if (pathSegments.length > 1) {
|
|
3843
|
+
group = pathSegments[1];
|
|
3844
|
+
}
|
|
3845
|
+
const category = categories.length ? categories.join(":") : null;
|
|
3846
|
+
const id = (0, import_node_crypto3.randomUUID)();
|
|
3847
|
+
const middlewares = this.findAssociatedMiddlewares(name, category);
|
|
3848
|
+
const subcommand = {
|
|
3849
|
+
id,
|
|
3640
3850
|
name,
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3851
|
+
path: filePath,
|
|
3852
|
+
category,
|
|
3853
|
+
group,
|
|
3854
|
+
middlewares
|
|
3855
|
+
};
|
|
3856
|
+
this.subcommands.set(id, subcommand);
|
|
3857
|
+
}
|
|
3858
|
+
parseNameAndCategory(name) {
|
|
3859
|
+
const parsedCategories = [];
|
|
3860
|
+
const regex = /\(([^)]+)\)/g;
|
|
3861
|
+
let match;
|
|
3862
|
+
let cleanName = name;
|
|
3863
|
+
while ((match = regex.exec(name)) !== null) {
|
|
3864
|
+
parsedCategories.push(match[1]);
|
|
3865
|
+
cleanName = cleanName.replace(match[0], "");
|
|
3866
|
+
}
|
|
3867
|
+
return {
|
|
3868
|
+
name: cleanName.trim(),
|
|
3869
|
+
parsedCategories
|
|
3644
3870
|
};
|
|
3645
3871
|
}
|
|
3872
|
+
findAssociatedMiddlewares(commandName, category) {
|
|
3873
|
+
const middlewares = [];
|
|
3874
|
+
this.middlewares.forEach((middleware) => {
|
|
3875
|
+
if (middleware.name === "middleware") {
|
|
3876
|
+
middlewares.push(middleware.id);
|
|
3877
|
+
return;
|
|
3878
|
+
}
|
|
3879
|
+
if (middleware.name === commandName) {
|
|
3880
|
+
middlewares.push(middleware.id);
|
|
3881
|
+
return;
|
|
3882
|
+
}
|
|
3883
|
+
if (category && middleware.category === category) {
|
|
3884
|
+
middlewares.push(middleware.id);
|
|
3885
|
+
}
|
|
3886
|
+
});
|
|
3887
|
+
return middlewares;
|
|
3888
|
+
}
|
|
3646
3889
|
};
|
|
3647
3890
|
__name(_CommandsRouter, "CommandsRouter");
|
|
3648
3891
|
CommandsRouter = _CommandsRouter;
|
|
@@ -3994,12 +4237,13 @@ var init_loader = __esm({
|
|
|
3994
4237
|
// src/utils/dev-hooks.ts
|
|
3995
4238
|
function registerDevHooks(commandkit) {
|
|
3996
4239
|
if (!COMMANDKIT_IS_DEV) return;
|
|
3997
|
-
process.
|
|
3998
|
-
|
|
3999
|
-
const
|
|
4000
|
-
if (!
|
|
4001
|
-
|
|
4002
|
-
|
|
4240
|
+
process.on("message", (message) => {
|
|
4241
|
+
if (typeof message !== "object" || message === null) return;
|
|
4242
|
+
const { event, path: path3 } = message;
|
|
4243
|
+
if (!event) return;
|
|
4244
|
+
if (process.env.COMMANDKIT_DEBUG_HMR === "true") {
|
|
4245
|
+
Logger.info(`Received HMR event: ${event}${path3 ? ` for ${path3}` : ""}`);
|
|
4246
|
+
}
|
|
4003
4247
|
switch (event) {
|
|
4004
4248
|
case "reload-commands":
|
|
4005
4249
|
commandkit.commandHandler.reloadCommands();
|
|
@@ -4013,20 +4257,18 @@ function registerDevHooks(commandkit) {
|
|
|
4013
4257
|
}
|
|
4014
4258
|
});
|
|
4015
4259
|
}
|
|
4016
|
-
var EVENT_PATTERN;
|
|
4017
4260
|
var init_dev_hooks = __esm({
|
|
4018
4261
|
"src/utils/dev-hooks.ts"() {
|
|
4019
4262
|
"use strict";
|
|
4020
4263
|
init_cjs_shims();
|
|
4021
4264
|
init_Logger();
|
|
4022
4265
|
init_constants();
|
|
4023
|
-
EVENT_PATTERN = /^COMMANDKIT_EVENT=(\w+)(\|(.+))?$/;
|
|
4024
4266
|
__name(registerDevHooks, "registerDevHooks");
|
|
4025
4267
|
}
|
|
4026
4268
|
});
|
|
4027
4269
|
|
|
4028
4270
|
// src/CommandKit.ts
|
|
4029
|
-
var import_node_events,
|
|
4271
|
+
var import_node_events, import_discord12, import_node_path7, import_node_fs6, _started, _CommandKit_instances, init_fn, initCommands_fn, initEvents_fn, _CommandKit, CommandKit;
|
|
4030
4272
|
var init_CommandKit = __esm({
|
|
4031
4273
|
"src/CommandKit.ts"() {
|
|
4032
4274
|
"use strict";
|
|
@@ -4037,7 +4279,7 @@ var init_CommandKit = __esm({
|
|
|
4037
4279
|
init_MemoryCache();
|
|
4038
4280
|
init_components();
|
|
4039
4281
|
init_EventInterceptor();
|
|
4040
|
-
|
|
4282
|
+
import_discord12 = require("discord.js");
|
|
4041
4283
|
init_DefaultLocalizationStrategy();
|
|
4042
4284
|
init_utilities();
|
|
4043
4285
|
import_node_path7 = require("path");
|
|
@@ -4048,6 +4290,7 @@ var init_CommandKit = __esm({
|
|
|
4048
4290
|
init_loader();
|
|
4049
4291
|
init_constants();
|
|
4050
4292
|
init_dev_hooks();
|
|
4293
|
+
import_node_fs6 = require("fs");
|
|
4051
4294
|
_CommandKit = class _CommandKit extends import_node_events.default {
|
|
4052
4295
|
/**
|
|
4053
4296
|
* Create a new command and event handler with CommandKit.
|
|
@@ -4080,7 +4323,7 @@ var init_CommandKit = __esm({
|
|
|
4080
4323
|
__privateAdd(this, _started, false);
|
|
4081
4324
|
__publicField(this, "eventInterceptor");
|
|
4082
4325
|
__publicField(this, "config", {
|
|
4083
|
-
defaultLocale:
|
|
4326
|
+
defaultLocale: import_discord12.Locale.EnglishUS,
|
|
4084
4327
|
localizationStrategy: new DefaultLocalizationStrategy(this),
|
|
4085
4328
|
getMessageCommandPrefix: /* @__PURE__ */ __name(() => "!", "getMessageCommandPrefix")
|
|
4086
4329
|
});
|
|
@@ -4274,7 +4517,8 @@ var init_CommandKit = __esm({
|
|
|
4274
4517
|
}, "#init");
|
|
4275
4518
|
initCommands_fn = /* @__PURE__ */ __name(async function() {
|
|
4276
4519
|
if (this.commandsRouter.isValidPath()) {
|
|
4277
|
-
await this.commandsRouter.scan();
|
|
4520
|
+
const commands = await this.commandsRouter.scan();
|
|
4521
|
+
await (0, import_node_fs6.writeFileSync)("./commands.json", JSON.stringify(commands, null, 2));
|
|
4278
4522
|
}
|
|
4279
4523
|
await this.commandHandler.loadCommands();
|
|
4280
4524
|
}, "#initCommands");
|
|
@@ -4303,7 +4547,7 @@ var init_version = __esm({
|
|
|
4303
4547
|
"use strict";
|
|
4304
4548
|
init_cjs_shims();
|
|
4305
4549
|
version = /* @__MACRO__ $version */
|
|
4306
|
-
"0.1.11-dev.
|
|
4550
|
+
"0.1.11-dev.20250306152436";
|
|
4307
4551
|
}
|
|
4308
4552
|
});
|
|
4309
4553
|
|
|
@@ -4337,11 +4581,18 @@ var init_env = __esm({
|
|
|
4337
4581
|
});
|
|
4338
4582
|
|
|
4339
4583
|
// src/cli/app-process.ts
|
|
4584
|
+
function getStdio(supportsCommands) {
|
|
4585
|
+
if (supportsCommands) {
|
|
4586
|
+
return ["pipe", "pipe", "pipe", "ipc"];
|
|
4587
|
+
}
|
|
4588
|
+
return ["pipe", "pipe", "pipe"];
|
|
4589
|
+
}
|
|
4340
4590
|
function createAppProcess(fileName, cwd, isDev) {
|
|
4341
4591
|
var _a, _b;
|
|
4342
|
-
if (!(0,
|
|
4592
|
+
if (!(0, import_node_fs7.existsSync)((0, import_node_path8.join)(cwd, fileName))) {
|
|
4343
4593
|
panic(`Could not locate the entrypoint file: ${fileName}`);
|
|
4344
4594
|
}
|
|
4595
|
+
const stdio = getStdio(isDev);
|
|
4345
4596
|
const ps = (0, import_node_child_process.spawn)(
|
|
4346
4597
|
"node",
|
|
4347
4598
|
[
|
|
@@ -4353,14 +4604,14 @@ function createAppProcess(fileName, cwd, isDev) {
|
|
|
4353
4604
|
cwd,
|
|
4354
4605
|
windowsHide: true,
|
|
4355
4606
|
env: DevEnv(),
|
|
4356
|
-
stdio
|
|
4607
|
+
stdio
|
|
4357
4608
|
}
|
|
4358
4609
|
);
|
|
4359
4610
|
(_a = ps.stdout) == null ? void 0 : _a.pipe(process.stdout);
|
|
4360
4611
|
(_b = ps.stderr) == null ? void 0 : _b.pipe(process.stderr);
|
|
4361
4612
|
return ps;
|
|
4362
4613
|
}
|
|
4363
|
-
var import_node_child_process, import_node_path8,
|
|
4614
|
+
var import_node_child_process, import_node_path8, import_node_fs7;
|
|
4364
4615
|
var init_app_process = __esm({
|
|
4365
4616
|
"src/cli/app-process.ts"() {
|
|
4366
4617
|
"use strict";
|
|
@@ -4368,8 +4619,9 @@ var init_app_process = __esm({
|
|
|
4368
4619
|
import_node_child_process = require("child_process");
|
|
4369
4620
|
init_env();
|
|
4370
4621
|
import_node_path8 = require("path");
|
|
4371
|
-
|
|
4622
|
+
import_node_fs7 = require("fs");
|
|
4372
4623
|
init_common();
|
|
4624
|
+
__name(getStdio, "getStdio");
|
|
4373
4625
|
__name(createAppProcess, "createAppProcess");
|
|
4374
4626
|
}
|
|
4375
4627
|
});
|
|
@@ -4531,7 +4783,11 @@ async function buildAndStart(configPath, skipStart = false) {
|
|
|
4531
4783
|
esbuildPlugins: config.esbuildPlugins
|
|
4532
4784
|
});
|
|
4533
4785
|
if (skipStart) return null;
|
|
4534
|
-
|
|
4786
|
+
const ps = createAppProcess(mainFile, configPath, true);
|
|
4787
|
+
ps.on("message", (message) => {
|
|
4788
|
+
console.log(`Received message from child process: ${message}`);
|
|
4789
|
+
});
|
|
4790
|
+
return ps;
|
|
4535
4791
|
}
|
|
4536
4792
|
async function bootstrapDevelopmentServer(configPath) {
|
|
4537
4793
|
const start = performance.now();
|
|
@@ -4542,13 +4798,13 @@ async function bootstrapDevelopmentServer(configPath) {
|
|
|
4542
4798
|
let ps = null;
|
|
4543
4799
|
const performHMR = debounce(async (path3) => {
|
|
4544
4800
|
if (!path3 || !ps) return false;
|
|
4801
|
+
if (!ps.send) return false;
|
|
4545
4802
|
if (isCommandSource(path3)) {
|
|
4546
4803
|
console.log(
|
|
4547
4804
|
`${colors_default.cyanBright("Reloading command(s) at ")} ${colors_default.yellowBright(path3)}`
|
|
4548
4805
|
);
|
|
4549
4806
|
await buildAndStart(cwd, true);
|
|
4550
|
-
ps.
|
|
4551
|
-
`);
|
|
4807
|
+
ps.send({ event: "reload-commands", path: path3 });
|
|
4552
4808
|
return true;
|
|
4553
4809
|
}
|
|
4554
4810
|
if (isEventSource(path3)) {
|
|
@@ -4556,8 +4812,7 @@ async function bootstrapDevelopmentServer(configPath) {
|
|
|
4556
4812
|
`${colors_default.cyanBright("Reloading event(s) at ")} ${colors_default.yellowBright(path3)}`
|
|
4557
4813
|
);
|
|
4558
4814
|
await buildAndStart(cwd, true);
|
|
4559
|
-
ps.
|
|
4560
|
-
`);
|
|
4815
|
+
ps.send({ event: "reload-events", path: path3 });
|
|
4561
4816
|
return true;
|
|
4562
4817
|
}
|
|
4563
4818
|
if (isLocaleSource(path3)) {
|
|
@@ -4565,14 +4820,17 @@ async function bootstrapDevelopmentServer(configPath) {
|
|
|
4565
4820
|
`${colors_default.cyanBright("Reloading locale(s) at ")} ${colors_default.yellowBright(path3)}`
|
|
4566
4821
|
);
|
|
4567
4822
|
await buildAndStart(cwd, true);
|
|
4568
|
-
ps.
|
|
4569
|
-
`);
|
|
4823
|
+
ps.send({ event: "reload-locales", path: path3 });
|
|
4570
4824
|
return true;
|
|
4571
4825
|
}
|
|
4572
4826
|
return false;
|
|
4573
4827
|
}, 300);
|
|
4574
4828
|
const hmrHandler = /* @__PURE__ */ __name(async (path3) => {
|
|
4575
|
-
|
|
4829
|
+
const hmr = await performHMR(path3);
|
|
4830
|
+
if (hmr) return;
|
|
4831
|
+
console.log(
|
|
4832
|
+
`${colors_default.yellowBright("\u26A1\uFE0F Performing full restart due to the changes in")} ${colors_default.cyanBright(path3)}`
|
|
4833
|
+
);
|
|
4576
4834
|
ps == null ? void 0 : ps.kill();
|
|
4577
4835
|
ps = await buildAndStart(cwd);
|
|
4578
4836
|
}, "hmrHandler");
|
|
@@ -4587,18 +4845,15 @@ async function bootstrapDevelopmentServer(configPath) {
|
|
|
4587
4845
|
break;
|
|
4588
4846
|
case "rc":
|
|
4589
4847
|
console.log(`Received reload commands command, reloading...`);
|
|
4590
|
-
ps == null ? void 0 : ps.
|
|
4591
|
-
`);
|
|
4848
|
+
ps == null ? void 0 : ps.send({ event: "reload-commands" });
|
|
4592
4849
|
break;
|
|
4593
4850
|
case "re":
|
|
4594
4851
|
console.log(`Received reload events command, reloading...`);
|
|
4595
|
-
ps == null ? void 0 : ps.
|
|
4596
|
-
`);
|
|
4852
|
+
ps == null ? void 0 : ps.send({ event: "reload-events" });
|
|
4597
4853
|
break;
|
|
4598
4854
|
case "rl":
|
|
4599
4855
|
console.log(`Received reload locales command, reloading...`);
|
|
4600
|
-
ps == null ? void 0 : ps.
|
|
4601
|
-
`);
|
|
4856
|
+
ps == null ? void 0 : ps.send({ event: "reload-locales" });
|
|
4602
4857
|
break;
|
|
4603
4858
|
}
|
|
4604
4859
|
});
|
|
@@ -4606,7 +4861,7 @@ async function bootstrapDevelopmentServer(configPath) {
|
|
|
4606
4861
|
watcher.on("add", hmrHandler);
|
|
4607
4862
|
watcher.on("unlink", hmrHandler);
|
|
4608
4863
|
watcher.on("unlinkDir", (path3) => {
|
|
4609
|
-
const hasChild = (0,
|
|
4864
|
+
const hasChild = (0, import_node_fs8.readdirSync)(path3).length > 0;
|
|
4610
4865
|
if (hasChild) return hmrHandler(path3);
|
|
4611
4866
|
});
|
|
4612
4867
|
watcher.on("error", (e) => {
|
|
@@ -4625,7 +4880,7 @@ ${colors_default.yellowBright("re")} - Reload all events
|
|
|
4625
4880
|
${colors_default.yellowBright("rl")} - Reload all locales`
|
|
4626
4881
|
);
|
|
4627
4882
|
}
|
|
4628
|
-
var import_path2, import_chokidar,
|
|
4883
|
+
var import_path2, import_chokidar, import_node_fs8, isCommandSource, isEventSource, isLocaleSource;
|
|
4629
4884
|
var init_development = __esm({
|
|
4630
4885
|
"src/cli/development.ts"() {
|
|
4631
4886
|
"use strict";
|
|
@@ -4636,7 +4891,7 @@ var init_development = __esm({
|
|
|
4636
4891
|
init_app_process();
|
|
4637
4892
|
init_build();
|
|
4638
4893
|
import_chokidar = require("chokidar");
|
|
4639
|
-
|
|
4894
|
+
import_node_fs8 = require("fs");
|
|
4640
4895
|
init_utilities();
|
|
4641
4896
|
init_colors();
|
|
4642
4897
|
__name(buildAndStart, "buildAndStart");
|
|
@@ -4772,7 +5027,7 @@ export default async function on${name[0].toUpperCase() + name.slice(1)}() {
|
|
|
4772
5027
|
}
|
|
4773
5028
|
async function generateLocale(locale, commandName, customPath) {
|
|
4774
5029
|
const localeNames = Object.fromEntries(
|
|
4775
|
-
Object.entries(
|
|
5030
|
+
Object.entries(import_discord13.Locale).map(([k, v]) => [v, k])
|
|
4776
5031
|
);
|
|
4777
5032
|
if (!localeNames[locale]) {
|
|
4778
5033
|
panic(`Invalid locale: ${locale}`);
|
|
@@ -4804,14 +5059,14 @@ async function generateLocale(locale, commandName, customPath) {
|
|
|
4804
5059
|
)
|
|
4805
5060
|
);
|
|
4806
5061
|
}
|
|
4807
|
-
var import_promises6, import_path4,
|
|
5062
|
+
var import_promises6, import_path4, import_discord13, import_fs2, BASE_PATH, COMMANDS_DIR, EVENTS_DIR, LOCALES_DIR, formatPath;
|
|
4808
5063
|
var init_generators = __esm({
|
|
4809
5064
|
"src/cli/generators.ts"() {
|
|
4810
5065
|
"use strict";
|
|
4811
5066
|
init_cjs_shims();
|
|
4812
5067
|
import_promises6 = require("fs/promises");
|
|
4813
5068
|
import_path4 = require("path");
|
|
4814
|
-
|
|
5069
|
+
import_discord13 = require("discord.js");
|
|
4815
5070
|
init_common();
|
|
4816
5071
|
import_fs2 = require("fs");
|
|
4817
5072
|
init_colors();
|