Skip to content

Commit 0372931

Browse files
authored
Merge pull request #482 from underctrl-io/update-middlewares
Update middlewares
2 parents 871195f + 0935830 commit 0372931

File tree

70 files changed

+736
-554
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+736
-554
lines changed

apps/test-bot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"discord.js": "catalog:discordjs",
2222
"dotenv": "^16.4.7",
2323
"ms": "^2.1.3",
24-
"zod": "^3.25.56"
24+
"zod": "^4.1.1"
2525
},
2626
"devDependencies": {
2727
"@types/ms": "^2.1.0",

apps/test-bot/src/app/commands/(developer)/+middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Logger, MiddlewareContext } from 'commandkit';
1+
import { Logger, MiddlewareContext, stopMiddlewares } from 'commandkit';
22
import { MessageFlags } from 'discord.js';
33

44
export function beforeExecute(ctx: MiddlewareContext) {
@@ -16,7 +16,7 @@ export function beforeExecute(ctx: MiddlewareContext) {
1616
ctx.message.reply('You are not allowed to use this command.');
1717
}
1818

19-
ctx.cancel();
19+
stopMiddlewares();
2020
}
2121
}
2222

apps/test-bot/src/app/commands/(developer)/run-after.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { CommandData, after, ChatInputCommand } from 'commandkit';
1+
import {
2+
CommandData,
3+
unstable_after as after,
4+
ChatInputCommand,
5+
} from 'commandkit';
26

37
export const command: CommandData = {
48
name: 'run-after',

apps/test-bot/src/app/commands/(general)/+middleware.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Logger, MiddlewareContext } from 'commandkit';
1+
import { Logger, MiddlewareContext, stopMiddlewares } from 'commandkit';
22

33
export function beforeExecute(ctx: MiddlewareContext) {
44
Logger.info(
55
`Directory-scoped middleware: ${ctx.commandName} will be executed!`,
66
);
7+
// stopMiddlewares();
78
}
89

910
export function afterExecute(ctx: MiddlewareContext) {

apps/test-bot/src/app/commands/(general)/+ping.middleware.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
import { Logger, MiddlewareContext } from 'commandkit';
1+
import {
2+
unstable_after as after,
3+
Logger,
4+
type MiddlewareContext,
5+
stopMiddlewares,
6+
} from 'commandkit';
27

38
export function beforeExecute(ctx: MiddlewareContext) {
9+
// Logger.info(
10+
// `Command-scoped middleware: ${ctx.commandName} will be executed!`,
11+
// );
12+
13+
Logger.info(`Command-scoped middleware: ${ctx.commandName} will be stopped!`);
414
Logger.info(
5-
`Command-scoped middleware: ${ctx.commandName} will be executed!`,
15+
'None of the other beforeExecute middlewares are supposed to be executed',
616
);
17+
18+
after(() => {
19+
Logger.info(`after() has been called in command-scoped middleware: ping`);
20+
});
21+
22+
stopMiddlewares();
723
}
824

925
export function afterExecute(ctx: MiddlewareContext) {

apps/test-bot/src/app/commands/(general)/ping.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import {
1111
AutocompleteCommandContext,
1212
CommandMetadata,
1313
MessageCommandContext,
14+
stopMiddlewares,
15+
Logger,
16+
unstable_after as after,
17+
CommandMetadataFunction,
1418
} from 'commandkit';
1519

1620
export const command: CommandData = {
@@ -32,10 +36,25 @@ export const command: CommandData = {
3236
// guilds: ['1314834483660455938'],
3337
};
3438

35-
export const metadata: CommandMetadata = {
36-
userPermissions: 'Administrator',
37-
botPermissions: 'KickMembers',
38-
// guilds: ['1314834483660455938'],
39+
// export const metadata: CommandMetadata = {
40+
// // userPermissions: 'Administrator',
41+
// // botPermissions: 'KickMembers',
42+
// // guilds: ['1314834483660455938'],
43+
// aliases: [''],
44+
// userPermissions: 'Administrator',
45+
// botPermissions: 'KickMembers',
46+
// guilds: ['1314834483660455938'],
47+
// };
48+
49+
export const generateMetadata: CommandMetadataFunction = async () => {
50+
// Dynamically determine the metadata for the command
51+
52+
return {
53+
userPermissions: 'Administrator',
54+
botPermissions: ['KickMembers', 'BanMembers'],
55+
// guilds: ['1234567890', '1234567891'],
56+
aliases: ['p', 'pong'],
57+
};
3958
};
4059

4160
const tests = Array.from({ length: 10 }, (_, i) => ({
@@ -98,4 +117,8 @@ export async function chatInput({
98117
button.setDisabled(true);
99118
message.edit({ components: [row] });
100119
});
120+
121+
after(() => {
122+
Logger.debug('after called in ping');
123+
});
101124
}

apps/test-bot/src/app/commands/(interactions)/+middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Logger, MiddlewareContext } from 'commandkit';
1+
import { Logger, MiddlewareContext, stopMiddlewares } from 'commandkit';
22
import { MessageFlags } from 'discord.js';
33

44
export function beforeExecute(ctx: MiddlewareContext) {
@@ -18,7 +18,7 @@ export function beforeExecute(ctx: MiddlewareContext) {
1818
ctx.message.reply('You are not allowed to use this command.');
1919
}
2020

21-
ctx.cancel();
21+
stopMiddlewares();
2222
}
2323
}
2424

apps/test-bot/src/app/commands/(interactions)/confirmation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
OnButtonKitClick,
77
} from 'commandkit';
88
import { ButtonStyle, MessageFlags } from 'discord.js';
9-
import { AiConfig, AiCommand } from '@commandkit/ai';
9+
import { AiConfig, AiCommand, ToolParameterType } from '@commandkit/ai';
1010
import { z } from 'zod';
1111

1212
export const command: CommandData = {
@@ -15,7 +15,7 @@ export const command: CommandData = {
1515
};
1616

1717
export const aiConfig = {
18-
parameters: z.object({
18+
inputSchema: z.object({
1919
message: z
2020
.string()
2121
.describe('The message to be shown in the confirmation.'),

apps/test-bot/src/app/commands/(leveling)/xp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ChatInputCommandContext, CommandData } from 'commandkit';
2-
import { database } from '@/database/store.ts';
2+
import { database } from '@/database/store';
33
import { cacheTag } from '@commandkit/cache';
44
import { AiCommand, AiConfig } from '@commandkit/ai';
55
import { z } from 'zod';
@@ -11,7 +11,7 @@ export const command: CommandData = {
1111

1212
export const aiConfig = {
1313
description: 'Get the XP of a user in a guild.',
14-
parameters: z.object({
14+
inputSchema: z.object({
1515
guildId: z.string().describe('The ID of the guild.'),
1616
userId: z.string().describe('The ID of the user.'),
1717
}),

apps/test-bot/src/app/commands/+global-middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Logger, MiddlewareContext } from 'commandkit';
1+
import { Logger, MiddlewareContext, stopMiddlewares } from 'commandkit';
22

33
export function beforeExecute(ctx: MiddlewareContext) {
44
Logger.info(`Global middleware: ${ctx.commandName} will be executed!`);
5+
// ctx.message.reply('Global middleware: This command will not be executed!');
6+
// stopMiddlewares();
57
}
68

79
export function afterExecute(ctx: MiddlewareContext) {

0 commit comments

Comments
 (0)