Skip to content

Commit dd2da64

Browse files
committed
docs: document stop command execution for middleware
1 parent ce8c269 commit dd2da64

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

apps/website/docs/guide/02-commands/07-middlewares.mdx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@ export function afterExecute(ctx: MiddlewareContext) {
3434
}
3535
```
3636

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+
3757
## Middleware types
3858

3959
### Directory-scoped middleware
@@ -43,7 +63,7 @@ middleware to all commands within that directory and its
4363
subdirectories.
4464

4565
```ts title="src/app/commands/(Moderation)/+middleware.ts"
46-
import { MiddlewareContext } from 'commandkit';
66+
import type { MiddlewareContext } from 'commandkit';
4767

4868
export function beforeExecute(ctx: MiddlewareContext) {
4969
// This middleware will run before any moderation command
@@ -62,7 +82,7 @@ For command-specific middleware, create a file named
6282
command file name.
6383

6484
```ts title="src/app/commands/+ban.middleware.ts"
65-
import { MiddlewareContext } from 'commandkit';
85+
import type { MiddlewareContext } from 'commandkit';
6686

6787
export function beforeExecute(ctx: MiddlewareContext) {
6888
// This middleware only runs before the ban command
@@ -76,7 +96,7 @@ Create a `+global-middleware.ts` file in your commands directory to
7696
apply middleware to every command in your entire Discord bot.
7797

7898
```ts title="src/app/commands/+global-middleware.ts"
79-
import { MiddlewareContext } from 'commandkit';
99+
import type { MiddlewareContext } from 'commandkit';
80100

81101
export function beforeExecute(ctx: MiddlewareContext) {
82102
// This middleware runs before ANY command in your bot

0 commit comments

Comments
 (0)