feat: Make Component#filter optional and add customId pass (#330)

* feat: enhance ComponentCommand and ModalCommand with optional customId and filter methods

* fix: lol

* fix: simplify conditional checks for customId in ComponentHandler

* fix: optional chain goes brrr

* feat: rework?
This commit is contained in:
Marcos Susaña 2025-02-17 20:43:09 -04:00 committed by GitHub
parent 008c2da719
commit f74f29e96a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 4 deletions

View File

@ -14,9 +14,17 @@ export interface ComponentCommand {
export abstract class ComponentCommand {
type = InteractionCommandType.COMPONENT;
abstract componentType: keyof ContextComponentCommandInteractionMap;
abstract filter(context: ComponentContext<typeof this.componentType>): Promise<boolean> | boolean;
customId?: string;
filter?(context: ComponentContext<typeof this.componentType>): Promise<boolean> | boolean;
abstract run(context: ComponentContext<typeof this.componentType>): any;
/** @internal */
async _filter(context: ComponentContext) {
const old = (await this.filter?.(context)) ?? true;
if (this.customId) return this.customId === context.customId && old;
return old;
}
middlewares: (keyof RegisteredMiddlewares)[] = [];
props!: ExtraProps;

View File

@ -310,7 +310,7 @@ export class ComponentHandler extends BaseHandler {
if (
i.type === InteractionCommandType.COMPONENT &&
i.cType === context.interaction.componentType &&
(await i.filter(context))
(await i._filter(context))
) {
context.command = i;
await this.execute(i, context);
@ -324,7 +324,7 @@ export class ComponentHandler extends BaseHandler {
async executeModal(context: ModalContext) {
for (const i of this.commands) {
try {
if (i.type === InteractionCommandType.MODAL && (await i.filter(context))) {
if (i.type === InteractionCommandType.MODAL && (await i._filter(context))) {
context.command = i;
await this.execute(i, context);
}

View File

@ -8,9 +8,17 @@ export interface ModalCommand {
export abstract class ModalCommand {
type = InteractionCommandType.MODAL;
abstract filter(context: ModalContext): Promise<boolean> | boolean;
filter?(context: ModalContext): Promise<boolean> | boolean;
customId?: string;
abstract run(context: ModalContext): any;
/** @internal */
async _filter(context: ModalContext) {
const old = (await this.filter?.(context)) ?? true;
if (this.customId) return this.customId === context.customId && old;
return old;
}
middlewares: (keyof RegisteredMiddlewares)[] = [];
props!: ExtraProps;