commandkit 0.1.11-dev.20250308130523 → 0.1.11-dev.20250308183646

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 CHANGED
@@ -1354,6 +1354,7 @@ interface LoadedSubCommand {
1354
1354
  }
1355
1355
  type CommandBuilderLike = SlashCommandBuilder | ContextMenuCommandBuilder | Record<string, any>;
1356
1356
  declare class AppCommandHandler {
1357
+ #private;
1357
1358
  readonly commandkit: CommandKit;
1358
1359
  private loadedCommands;
1359
1360
  private loadedSubCommands;
package/dist/index.js CHANGED
@@ -3291,7 +3291,7 @@ var init_CommandRegistrar = __esm({
3291
3291
  });
3292
3292
 
3293
3293
  // src/app/handlers/AppCommandHandler.ts
3294
- var import_discord10, commandDataSchema, middlewareDataSchema, _AppCommandHandler, AppCommandHandler;
3294
+ var import_discord10, commandDataSchema, middlewareDataSchema, _AppCommandHandler_instances, finalizer_fn, _AppCommandHandler, AppCommandHandler;
3295
3295
  var init_AppCommandHandler = __esm({
3296
3296
  "src/app/handlers/AppCommandHandler.ts"() {
3297
3297
  "use strict";
@@ -3302,7 +3302,9 @@ var init_AppCommandHandler = __esm({
3302
3302
  init_MessageCommandParser();
3303
3303
  init_error_codes();
3304
3304
  init_CommandRegistrar();
3305
+ init_async_context();
3305
3306
  init_Logger();
3307
+ init_environment();
3306
3308
  commandDataSchema = {
3307
3309
  command: /* @__PURE__ */ __name((c) => c instanceof import_discord10.SlashCommandBuilder || c instanceof import_discord10.ContextMenuCommandBuilder || c && typeof c === "object", "command"),
3308
3310
  chatInput: /* @__PURE__ */ __name((c) => typeof c === "function", "chatInput"),
@@ -3318,18 +3320,19 @@ var init_AppCommandHandler = __esm({
3318
3320
  _AppCommandHandler = class _AppCommandHandler {
3319
3321
  constructor(commandkit) {
3320
3322
  this.commandkit = commandkit;
3323
+ __privateAdd(this, _AppCommandHandler_instances);
3324
+ __publicField(this, "loadedCommands", new import_discord10.Collection());
3325
+ __publicField(this, "loadedSubCommands", new import_discord10.Collection());
3326
+ __publicField(this, "loadedMiddlewares", new import_discord10.Collection());
3327
+ // Name-to-ID mapping for easier lookup
3328
+ __publicField(this, "commandNameToId", /* @__PURE__ */ new Map());
3329
+ __publicField(this, "subcommandPathToId", /* @__PURE__ */ new Map());
3330
+ __publicField(this, "registrar");
3331
+ __publicField(this, "onInteraction", null);
3332
+ __publicField(this, "onMessageCreate", null);
3333
+ __publicField(this, "onMessageUpdate", null);
3321
3334
  this.registrar = new CommandRegistrar(this.commandkit);
3322
3335
  }
3323
- loadedCommands = new import_discord10.Collection();
3324
- loadedSubCommands = new import_discord10.Collection();
3325
- loadedMiddlewares = new import_discord10.Collection();
3326
- // Name-to-ID mapping for easier lookup
3327
- commandNameToId = /* @__PURE__ */ new Map();
3328
- subcommandPathToId = /* @__PURE__ */ new Map();
3329
- registrar;
3330
- onInteraction = null;
3331
- onMessageCreate = null;
3332
- onMessageUpdate = null;
3333
3336
  getCommandsArray() {
3334
3337
  return Array.from(this.loadedCommands.values());
3335
3338
  }
@@ -3411,7 +3414,13 @@ var init_AppCommandHandler = __esm({
3411
3414
  }
3412
3415
  const executionMode = this.getExecutionMode(source);
3413
3416
  let runCommand = null;
3417
+ const env = new CommandKitEnvironment(this.commandkit);
3418
+ env.setType("COMMAND_HANDLER" /* CommandHandler */);
3419
+ env.variables.set("commandHandlerType", "app");
3420
+ env.variables.set("currentCommandName", prepared.command.command.name);
3421
+ env.variables.set("execHandlerKind", executionMode);
3414
3422
  const ctx = new MiddlewareContext(this.commandkit, {
3423
+ environment: env,
3415
3424
  executionMode,
3416
3425
  interaction: !(source instanceof import_discord10.Message) ? source : null,
3417
3426
  message: source instanceof import_discord10.Message ? source : null,
@@ -3434,8 +3443,25 @@ var init_AppCommandHandler = __esm({
3434
3443
  }
3435
3444
  if (fn) {
3436
3445
  try {
3437
- const _executeCommand = /* @__PURE__ */ __name(async () => fn(ctx.clone()), "_executeCommand");
3446
+ const _executeCommand = makeContextAwareFunction(
3447
+ env,
3448
+ async () => fn(ctx.clone()),
3449
+ __privateMethod(this, _AppCommandHandler_instances, finalizer_fn).bind(this)
3450
+ );
3438
3451
  const executeCommand = runCommand != null ? runCommand(_executeCommand) : _executeCommand;
3452
+ afterCommand((env2) => {
3453
+ const error = env2.getExecutionError();
3454
+ const marker = env2.getMarker();
3455
+ const time = `${env2.getExecutionTime().toFixed(2)}ms`;
3456
+ if (error) {
3457
+ Logger.error(
3458
+ `[${marker} - ${time}] Error executing command: ${error.stack || error}`
3459
+ );
3460
+ return;
3461
+ }
3462
+ Logger.info(`[${marker} - ${time}] Command executed successfully`);
3463
+ });
3464
+ env.markStart(prepared.command.command.name);
3439
3465
  const res = await this.commandkit.plugins.execute(
3440
3466
  async (ctx2, plugin) => {
3441
3467
  return plugin.executeCommand(ctx2, source, prepared, executeCommand);
@@ -3448,8 +3474,12 @@ var init_AppCommandHandler = __esm({
3448
3474
  Logger.error(e);
3449
3475
  }
3450
3476
  }
3451
- for (const middleware of prepared.middlewares) {
3452
- await middleware.data.afterExecute(ctx);
3477
+ try {
3478
+ for (const middleware of prepared.middlewares) {
3479
+ await middleware.data.afterExecute(ctx);
3480
+ }
3481
+ } finally {
3482
+ env.markEnd();
3453
3483
  }
3454
3484
  }
3455
3485
  async prepareCommandRun(source) {
@@ -3499,7 +3529,7 @@ var init_AppCommandHandler = __esm({
3499
3529
  if (isErrorType(e, CommandKitErrorCodes.InvalidCommandPrefix)) {
3500
3530
  return null;
3501
3531
  }
3502
- console.error(e);
3532
+ Logger.error(e);
3503
3533
  return null;
3504
3534
  }
3505
3535
  } else {
@@ -3792,6 +3822,12 @@ var init_AppCommandHandler = __esm({
3792
3822
  return command;
3793
3823
  }
3794
3824
  };
3825
+ _AppCommandHandler_instances = new WeakSet();
3826
+ finalizer_fn = /* @__PURE__ */ __name(async function() {
3827
+ const env = useEnvironment();
3828
+ await env.runDeferredFunctions();
3829
+ env.clearAllDeferredFunctions();
3830
+ }, "#finalizer");
3795
3831
  __name(_AppCommandHandler, "AppCommandHandler");
3796
3832
  AppCommandHandler = _AppCommandHandler;
3797
3833
  }
@@ -4617,6 +4653,7 @@ var init_CommandKit = __esm({
4617
4653
  }
4618
4654
  await this.loadPlugins();
4619
4655
  await __privateMethod(this, _CommandKit_instances, init_fn).call(this);
4656
+ this.commandHandler.registerCommandHandler();
4620
4657
  this.incrementClientListenersCount();
4621
4658
  if (token !== false && !this.options.client.isReady()) {
4622
4659
  this.client.once(import_discord12.Events.ClientReady, async () => {
@@ -4625,6 +4662,8 @@ var init_CommandKit = __esm({
4625
4662
  await this.options.client.login(
4626
4663
  token ?? process.env.TOKEN ?? process.env.DISCORD_TOKEN
4627
4664
  );
4665
+ } else if (this.options.client.isReady()) {
4666
+ await this.commandHandler.registrar.register();
4628
4667
  }
4629
4668
  __privateSet(this, _started, true);
4630
4669
  }
@@ -4829,7 +4868,7 @@ var init_version = __esm({
4829
4868
  "use strict";
4830
4869
  init_cjs_shims();
4831
4870
  version = /* @__MACRO__ $version */
4832
- "0.1.11-dev.20250308130523";
4871
+ "0.1.11-dev.20250308183646";
4833
4872
  }
4834
4873
  });
4835
4874