@@ -26,9 +26,18 @@ import { MessageCommandParser } from '../commands/MessageCommandParser';
26
26
import { CommandKitErrorCodes , isErrorType } from '../../utils/error-codes' ;
27
27
import { ParsedCommand , ParsedMiddleware , ParsedSubCommand } from '../router' ;
28
28
import { CommandRegistrar } from '../register/CommandRegistrar' ;
29
- import { GenericFunction } from '../../context/async-context' ;
29
+ import {
30
+ GenericFunction ,
31
+ makeContextAwareFunction ,
32
+ useEnvironment ,
33
+ } from '../../context/async-context' ;
30
34
import { Logger } from '../../logger/Logger' ;
31
35
import { AsyncFunction } from '../../cache' ;
36
+ import {
37
+ afterCommand ,
38
+ CommandKitEnvironment ,
39
+ CommandKitEnvironmentType ,
40
+ } from '../../context/environment' ;
32
41
33
42
export type RunCommand = < T extends AsyncFunction > ( fn : T ) => T ;
34
43
@@ -238,7 +247,14 @@ export class AppCommandHandler {
238
247
239
248
let runCommand : RunCommand | null = null ;
240
249
250
+ const env = new CommandKitEnvironment ( this . commandkit ) ;
251
+ env . setType ( CommandKitEnvironmentType . CommandHandler ) ;
252
+ env . variables . set ( 'commandHandlerType' , 'app' ) ;
253
+ env . variables . set ( 'currentCommandName' , prepared . command . command . name ) ;
254
+ env . variables . set ( 'execHandlerKind' , executionMode ) ;
255
+
241
256
const ctx = new MiddlewareContext ( this . commandkit , {
257
+ environment : env ,
242
258
executionMode,
243
259
interaction : ! ( source instanceof Message )
244
260
? ( source as ChatInputCommandInteraction )
@@ -271,11 +287,35 @@ export class AppCommandHandler {
271
287
272
288
if ( fn ) {
273
289
try {
274
- const _executeCommand = async ( ) => fn ( ctx . clone ( ) ) ;
290
+ const _executeCommand = makeContextAwareFunction (
291
+ env ,
292
+ async ( ) => fn ( ctx . clone ( ) ) ,
293
+ this . #finalizer. bind ( this ) ,
294
+ ) ;
295
+
275
296
const executeCommand =
276
297
runCommand != null
277
298
? ( runCommand as RunCommand ) ( _executeCommand )
278
299
: _executeCommand ;
300
+
301
+ afterCommand ( ( env ) => {
302
+ const error = env . getExecutionError ( ) ;
303
+ const marker = env . getMarker ( ) ;
304
+ const time = `${ env . getExecutionTime ( ) . toFixed ( 2 ) } ms` ;
305
+
306
+ if ( error ) {
307
+ Logger . error (
308
+ `[${ marker } - ${ time } ] Error executing command: ${ error . stack || error } ` ,
309
+ ) ;
310
+
311
+ return ;
312
+ }
313
+
314
+ Logger . info ( `[${ marker } - ${ time } ] Command executed successfully` ) ;
315
+ } ) ;
316
+
317
+ env . markStart ( prepared . command . command . name ) ;
318
+
279
319
const res = await this . commandkit . plugins . execute (
280
320
async ( ctx , plugin ) => {
281
321
return plugin . executeCommand ( ctx , source , prepared , executeCommand ) ;
@@ -290,12 +330,24 @@ export class AppCommandHandler {
290
330
}
291
331
}
292
332
293
- // Run middleware after command execution
294
- for ( const middleware of prepared . middlewares ) {
295
- await middleware . data . afterExecute ( ctx ) ;
333
+ try {
334
+ // Run middleware after command execution
335
+ for ( const middleware of prepared . middlewares ) {
336
+ await middleware . data . afterExecute ( ctx ) ;
337
+ }
338
+ } finally {
339
+ env . markEnd ( ) ;
296
340
}
297
341
}
298
342
343
+ async #finalizer( ) {
344
+ const env = useEnvironment ( ) ;
345
+
346
+ await env . runDeferredFunctions ( ) ;
347
+
348
+ env . clearAllDeferredFunctions ( ) ;
349
+ }
350
+
299
351
public async prepareCommandRun (
300
352
source : Interaction | Message ,
301
353
) : Promise < PreparedAppCommandExecution | null > {
@@ -367,7 +419,7 @@ export class AppCommandHandler {
367
419
if ( isErrorType ( e , CommandKitErrorCodes . InvalidCommandPrefix ) ) {
368
420
return null ;
369
421
}
370
- console . error ( e ) ;
422
+ Logger . error ( e ) ;
371
423
return null ;
372
424
}
373
425
} else {
0 commit comments