@@ -34,6 +34,26 @@ export function afterExecute(ctx: MiddlewareContext) {
34
34
}
35
35
```
36
36
37
+ ## Stop command execution
38
+
39
+ You can stop a command from running by calling ` ctx.cancel() ` in the
40
+ ` beforeExecute ` function.
41
+
42
+ ``` ts title="src/app/commands/+middleware.ts"
43
+ import type { MiddlewareContext } from ' commandkit' ;
44
+
45
+ export function beforeExecute(ctx : MiddlewareContext ) {
46
+ if (ctx .interaction .user .id !== ' 1234567890' ) {
47
+ // Conditionally stop command execution
48
+ console .log (` ${ctx .commandName } will not be executed! ` );
49
+ ctx .cancel ();
50
+ }
51
+
52
+ // Continue with command execution
53
+ console .log (` ${ctx .commandName } will be executed! ` );
54
+ }
55
+ ```
56
+
37
57
## Middleware types
38
58
39
59
### Directory-scoped middleware
@@ -43,7 +63,7 @@ middleware to all commands within that directory and its
43
63
subdirectories.
44
64
45
65
``` ts title="src/app/commands/(Moderation)/+middleware.ts"
46
- import { MiddlewareContext } from ' commandkit' ;
66
+ import type { MiddlewareContext } from ' commandkit' ;
47
67
48
68
export function beforeExecute(ctx : MiddlewareContext ) {
49
69
// This middleware will run before any moderation command
@@ -62,7 +82,7 @@ For command-specific middleware, create a file named
62
82
command file name.
63
83
64
84
``` ts title="src/app/commands/+ban.middleware.ts"
65
- import { MiddlewareContext } from ' commandkit' ;
85
+ import type { MiddlewareContext } from ' commandkit' ;
66
86
67
87
export function beforeExecute(ctx : MiddlewareContext ) {
68
88
// This middleware only runs before the ban command
@@ -76,7 +96,7 @@ Create a `+global-middleware.ts` file in your commands directory to
76
96
apply middleware to every command in your entire Discord bot.
77
97
78
98
``` ts title="src/app/commands/+global-middleware.ts"
79
- import { MiddlewareContext } from ' commandkit' ;
99
+ import type { MiddlewareContext } from ' commandkit' ;
80
100
81
101
export function beforeExecute(ctx : MiddlewareContext ) {
82
102
// This middleware runs before ANY command in your bot
0 commit comments