Skip to content

Commit 625ae0f

Browse files
committed
chore: prettier
1 parent 4c1e8a4 commit 625ae0f

File tree

24 files changed

+134
-40
lines changed

24 files changed

+134
-40
lines changed

apps/test-bot/src/commands/misc/confirmation.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ export const data: CommandData = {
1212
description: 'This is a confirm command.',
1313
};
1414

15-
const handleConfirm: OnButtonKitClick = async (interaction) => {
15+
const handleConfirm: OnButtonKitClick = async (interaction, context) => {
1616
await interaction.reply({
1717
content: 'The item was deleted successfully.',
1818
flags: MessageFlags.Ephemeral,
1919
});
20+
21+
context.dispose();
2022
};
2123

22-
const handleCancel: OnButtonKitClick = async (interaction) => {
24+
const handleCancel: OnButtonKitClick = async (interaction, context) => {
2325
await interaction.reply({
2426
content: 'The item was not deleted.',
2527
flags: MessageFlags.Ephemeral,
2628
});
29+
30+
context.dispose();
2731
};
2832

2933
export async function run({ interaction }: SlashCommandProps) {

apps/test-bot/src/commands/misc/prompt.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ export const data: CommandData = {
1313
description: 'This is a prompt command.',
1414
};
1515

16-
const handleSubmit: OnModalKitSubmit = async (interaction) => {
16+
const handleSubmit: OnModalKitSubmit = async (interaction, context) => {
1717
const name = interaction.fields.getTextInputValue('name');
1818
const description = interaction.fields.getTextInputValue('description');
1919

2020
await interaction.reply({
2121
content: `Name: ${name}\nDescription: ${description}`,
2222
flags: MessageFlags.Ephemeral,
2323
});
24+
25+
context.dispose();
2426
};
2527

2628
export async function run({ interaction }: SlashCommandProps) {

packages/commandkit/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@
4848
"@types/ms": "^0.7.34",
4949
"@types/node": "^22.10.2",
5050
"@types/yargs": "^17.0.32",
51-
"discord.js": "^14.16.3",
51+
"discord.js": "^14.17.3",
5252
"tsconfig": "workspace:*",
53-
"tsx": "^4.7.0",
54-
"typescript": "^5.7.2",
53+
"tsx": "^4.19.2",
54+
"typescript": "^5.7.3",
5555
"use-macro": "^1.1.0",
56-
"vitest": "^1.2.1"
56+
"vitest": "^3.0.5"
5757
},
5858
"peerDependencies": {
5959
"discord.js": "^14"
6060
},
6161
"engines": {
6262
"node": ">=22"
6363
}
64-
}
64+
}

packages/commandkit/src/CommandKit.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import EventEmitter from 'node:events';
2-
import { CommandHandler, EventHandler, ValidationHandler } from './handlers';
2+
import {
3+
CommandHandler,
4+
EventHandler,
5+
ValidationHandler,
6+
} from './legacy/handlers';
37
import type {
48
CommandKitData,
59
CommandKitOptions,

packages/commandkit/src/cli/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async function loadConfigInner(target: string) {
111111
*/
112112
// @ts-ignore
113113
const config = await import(`file://${target}`, {
114-
assert: isJSON ? { type: 'json' } : undefined,
114+
with: isJSON ? { type: 'json' } : undefined,
115115
}).then((conf) => conf.default || conf);
116116

117117
return config;

packages/commandkit/src/components/button/Button.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from './ButtonKit';
88
import { CommandKitElement } from '../common/element';
99
import { MaybeArray } from '../common/types';
10+
import { EventInterceptorErrorHandler } from '../common/EventInterceptor';
1011

1112
export type ButtonChildrenLike = string | number | boolean;
1213

@@ -19,6 +20,7 @@ export interface ButtonProps {
1920
url?: string;
2021
skuId?: string;
2122
onClick?: CommandKitButtonBuilderInteractionCollectorDispatch;
23+
onError?: EventInterceptorErrorHandler;
2224
options?: CommandKitButtonBuilderInteractionCollectorDispatchContextData;
2325
onEnd?: CommandKitButtonBuilderOnEnd;
2426
children?: MaybeArray<ButtonChildrenLike>;
@@ -84,5 +86,9 @@ export function Button(props: ButtonProps): CommandKitElement<'button-kit'> {
8486
button.onEnd(props.onEnd);
8587
}
8688

89+
if (props.onError) {
90+
button.onError(props.onError);
91+
}
92+
8793
return button;
8894
}

packages/commandkit/src/components/button/ButtonKit.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
getCommandKit,
1111
getContext,
1212
} from '../../context/async-context';
13-
import { EventInterceptorContextData } from '../common/EventInterceptor';
13+
import {
14+
EventInterceptorContextData,
15+
EventInterceptorErrorHandler,
16+
} from '../common/EventInterceptor';
1417

1518
export type ButtonKitPredicate = (
1619
interaction: ButtonInteraction,
@@ -35,6 +38,7 @@ export type OnButtonKitEnd = CommandKitButtonBuilderOnEnd;
3538
*/
3639
export type CommandKitButtonBuilderInteractionCollectorDispatch = (
3740
interaction: ButtonInteraction,
41+
context: ButtonKit,
3842
) => Awaitable<void>;
3943

4044
export type CommandKitButtonBuilderOnEnd = (reason: string) => Awaitable<void>;
@@ -49,6 +53,7 @@ export class ButtonKit extends ButtonBuilder {
4953
{
5054
autoReset: true,
5155
time: 5 * 60 * 1000,
56+
once: false,
5257
};
5358
#unsub: (() => void) | null = null;
5459

@@ -134,6 +139,24 @@ export class ButtonKit extends ButtonBuilder {
134139
return this;
135140
}
136141

142+
/**
143+
* Sets the handler to run when the interaction collector ends.
144+
* @param handler - The handler to run when the interaction collector ends.
145+
* @returns This instance of the modal builder.
146+
*/
147+
public onError(handler: EventInterceptorErrorHandler): this {
148+
if (!handler) {
149+
throw new TypeError(
150+
'Cannot setup "onError" without a handler function parameter.',
151+
);
152+
}
153+
154+
this.#contextData ??= {};
155+
this.#contextData.onError = handler;
156+
157+
return this;
158+
}
159+
137160
/**
138161
* Sets a filter for the interaction collector.
139162
* @param predicate The filter to use for the interaction collector
@@ -190,7 +213,7 @@ export class ButtonKit extends ButtonBuilder {
190213

191214
if (!handler) return this.#unsub?.();
192215

193-
return handler(interaction);
216+
return handler(interaction, this);
194217
},
195218
this.#contextData,
196219
);

packages/commandkit/src/components/common/EventInterceptor.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Awaitable, Client, ClientEvents } from 'discord.js';
1+
import { Awaitable, Client, ClientEvents, Events } from 'discord.js';
22

33
export interface EventInterceptorContextData<E extends keyof ClientEvents> {
44
/**
@@ -13,12 +13,22 @@ export interface EventInterceptorContextData<E extends keyof ClientEvents> {
1313
* If the collector should automatically reset the timer when a button is clicked.
1414
*/
1515
autoReset?: boolean;
16+
/**
17+
* Whether the collector should run only once.
18+
*/
19+
once?: boolean;
1620
/**
1721
* The handler to run when the collector ends.
1822
*/
1923
onEnd?: (reason: string) => Awaitable<void>;
24+
/**
25+
* The handler to run upon an error.
26+
*/
27+
onError?: EventInterceptorErrorHandler;
2028
}
2129

30+
export type EventInterceptorErrorHandler = (error: Error) => Awaitable<void>;
31+
2232
export class EventInterceptor {
2333
private subscribers = new Map<
2434
keyof ClientEvents,
@@ -205,7 +215,19 @@ export class EventInterceptor {
205215
continue;
206216
}
207217

208-
await subscriber(...args);
218+
try {
219+
await subscriber(...args);
220+
} catch (e) {
221+
if (options.onError) {
222+
await options.onError(<Error>e);
223+
} else {
224+
throw e;
225+
}
226+
} finally {
227+
if (options.once) {
228+
this.unsubscribe(event, subscriber, 'once');
229+
}
230+
}
209231
}
210232
}
211233
};

packages/commandkit/src/components/modal/Modal.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {
77
OnModalKitEnd,
88
OnModalKitSubmit,
99
} from './ModalKit';
10+
import { EventInterceptorErrorHandler } from '../common/EventInterceptor';
1011

1112
export interface ModalProps {
1213
customId?: string;
1314
title: string;
1415
children?: MaybeArray<TextInputBuilder | ActionRowBuilder>;
1516
onSubmit?: OnModalKitSubmit;
1617
onEnd?: OnModalKitEnd;
18+
onError?: EventInterceptorErrorHandler;
1719
options?: CommandKitModalBuilderInteractionCollectorDispatchContextData;
1820
}
1921

@@ -60,6 +62,10 @@ export function Modal(props: ModalProps): CommandKitElement<'modal'> {
6062
modal.onEnd(props.onEnd);
6163
}
6264

65+
if (props.onError) {
66+
modal.onError(props.onError);
67+
}
68+
6369
return modal;
6470
}
6571

packages/commandkit/src/components/modal/ModalKit.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import {
99
getCommandKit,
1010
getContext,
1111
} from '../../context/async-context';
12-
import { EventInterceptorContextData } from '../common/EventInterceptor';
12+
import {
13+
EventInterceptorContextData,
14+
EventInterceptorErrorHandler,
15+
} from '../common/EventInterceptor';
1316

1417
export type ModalKitPredicate = (
1518
interaction: ModalSubmitInteraction,
@@ -34,6 +37,7 @@ export type OnModalKitEnd = CommandKitModalBuilderOnEnd;
3437
*/
3538
export type CommandKitModalBuilderInteractionCollectorDispatch = (
3639
interaction: ModalSubmitInteraction,
40+
context: ModalKit,
3741
) => Awaitable<void>;
3842

3943
export type CommandKitModalBuilderOnEnd = (reason: string) => Awaitable<void>;
@@ -46,8 +50,9 @@ export class ModalKit extends ModalBuilder {
4650
null;
4751
#contextData: CommandKitModalBuilderInteractionCollectorDispatchContextData | null =
4852
{
49-
autoReset: true,
53+
autoReset: false,
5054
time: 5 * 60 * 1000,
55+
once: true,
5156
};
5257
#unsub: (() => void) | null = null;
5358

@@ -123,6 +128,24 @@ export class ModalKit extends ModalBuilder {
123128
return this;
124129
}
125130

131+
/**
132+
* Sets the handler to run when the interaction collector ends.
133+
* @param handler - The handler to run when the interaction collector ends.
134+
* @returns This instance of the modal builder.
135+
*/
136+
public onError(handler: EventInterceptorErrorHandler): this {
137+
if (!handler) {
138+
throw new TypeError(
139+
'Cannot setup "onError" without a handler function parameter.',
140+
);
141+
}
142+
143+
this.#contextData ??= {};
144+
this.#contextData.onError = handler;
145+
146+
return this;
147+
}
148+
126149
/**
127150
* Sets a filter for the interaction collector.
128151
* @param predicate - The filter to use for the interaction collector.
@@ -174,7 +197,7 @@ export class ModalKit extends ModalBuilder {
174197

175198
if (!handler) return this.#unsub?.();
176199

177-
return handler(interaction);
200+
return handler(interaction, this);
178201
},
179202
this.#contextData,
180203
);

0 commit comments

Comments
 (0)