Skip to content

Commit 879ed6b

Browse files
committed
fix command-scoped middleware + document middlewares
1 parent 3fa55d9 commit 879ed6b

File tree

9 files changed

+125
-13
lines changed

9 files changed

+125
-13
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Logger, MiddlewareContext } from 'commandkit';
2+
3+
export function beforeExecute(ctx: MiddlewareContext) {
4+
Logger.info(
5+
`Directory-scoped middleware: ${ctx.commandName} will be executed!`,
6+
);
7+
}
8+
9+
export function afterExecute(ctx: MiddlewareContext) {
10+
Logger.info(
11+
`Directory-scoped middleware: ${ctx.commandName} has been executed!`,
12+
);
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Logger, MiddlewareContext } from 'commandkit';
2+
3+
export function beforeExecute(ctx: MiddlewareContext) {
4+
Logger.info(
5+
`Command-scoped middleware: ${ctx.commandName} will be executed!`,
6+
);
7+
}
8+
9+
export function afterExecute(ctx: MiddlewareContext) {
10+
Logger.info(
11+
`Command-scoped middleware: ${ctx.commandName} has been executed!`,
12+
);
13+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Logger, MiddlewareContext } from 'commandkit';
22

33
export function beforeExecute(ctx: MiddlewareContext) {
4-
Logger.info(`${ctx.commandName} will be executed!`);
4+
Logger.info(`Global middleware: ${ctx.commandName} will be executed!`);
55
}
66

77
export function afterExecute(ctx: MiddlewareContext) {
8-
Logger.info(`${ctx.commandName} has been executed!`);
8+
Logger.info(`Global middleware: ${ctx.commandName} has been executed!`);
99
}

apps/website/docs/guide/02-commands/04-message-commands.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ idea of message command options will be different compared to
4545
getting parsed data, you'll essentially be dealing with string
4646
'arguments'.
4747

48-
```ts title="src/app/commands/ping.ts"
48+
```ts title="src/app/commands/ping.ts" {8}
4949
import type { CommandData, MessageCommand } from 'commandkit';
5050

5151
export const command: CommandData = {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: Middlewares
3+
---
4+
5+
Middlewares in CommandKit allow you to execute code before and after
6+
command execution. This is incredibly powerful for implementing
7+
features like logging, authentication, permission checks, or any other
8+
cross-cutting concerns for your Discord bot.
9+
10+
CommandKit supports three different types of middleware files, each
11+
serving different scoping purposes for your commands.
12+
13+
## Basic middleware structure
14+
15+
All middleware files follow the same export pattern. You can export
16+
`beforeExecute` and `afterExecute` functions that will run at their
17+
respective times during command execution.
18+
19+
```ts title="src/app/commands/+middleware.ts"
20+
import { MiddlewareContext } from 'commandkit';
21+
22+
export function beforeExecute(ctx: MiddlewareContext) {
23+
// This function will be executed before the command is executed
24+
console.log(
25+
`User ${ctx.interaction.user.id} is about to execute a command`,
26+
);
27+
}
28+
29+
export function afterExecute(ctx: MiddlewareContext) {
30+
// This function will be executed after the command is executed
31+
console.log(
32+
`Command execution completed for user ${ctx.interaction.user.id}`,
33+
);
34+
}
35+
```
36+
37+
## Middleware types
38+
39+
### Directory-scoped middleware
40+
41+
Create a `+middleware.ts` file in any commands directory to apply
42+
middleware to all commands within that directory and its
43+
subdirectories.
44+
45+
```ts title="src/app/commands/(Moderation)/+middleware.ts"
46+
import { MiddlewareContext } from 'commandkit';
47+
48+
export function beforeExecute(ctx: MiddlewareContext) {
49+
// This middleware will run before any moderation command
50+
if (!ctx.interaction.member.permissions.has('KickMembers')) {
51+
throw new Error(
52+
'You need moderation permissions to use this command',
53+
);
54+
}
55+
}
56+
```
57+
58+
### Command-specific middleware
59+
60+
For command-specific middleware, create a file named
61+
`+<command-name>.middleware.ts` where `<command-name>` matches your
62+
command file name.
63+
64+
```ts title="src/app/commands/+ban.middleware.ts"
65+
import { MiddlewareContext } from 'commandkit';
66+
67+
export function beforeExecute(ctx: MiddlewareContext) {
68+
// This middleware only runs before the ban command
69+
console.log('Ban command is about to be executed');
70+
}
71+
```
72+
73+
### Global middleware
74+
75+
Create a `+global-middleware.ts` file in your commands directory to
76+
apply middleware to every command in your entire Discord bot.
77+
78+
```ts title="src/app/commands/+global-middleware.ts"
79+
import { MiddlewareContext } from 'commandkit';
80+
81+
export function beforeExecute(ctx: MiddlewareContext) {
82+
// This middleware runs before ANY command in your bot
83+
console.log(
84+
`Command executed by ${ctx.interaction.user.tag} in ${ctx.interaction.guild?.name || 'DMs'}`,
85+
);
86+
}
87+
```
88+
89+
:::tip
90+
91+
Middleware execution (both before and after) follows a hierarchy:
92+
directory-scoped middleware runs first, then command-specific
93+
middleware, and finally global middleware.
94+
95+
:::

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

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/commandkit/src/app/router/CommandsRouter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export class CommandsRouter {
270270
parentPath: entry.parentPath,
271271
global: GLOBAL_MIDDLEWARE_PATTERN.test(name),
272272
command: COMMAND_MIDDLEWARE_PATTERN.test(name)
273-
? name.split('.')[0] || null
273+
? name.match(COMMAND_MIDDLEWARE_PATTERN)?.[1] || null
274274
: null,
275275
};
276276

0 commit comments

Comments
 (0)