From f74f29e96a27593709427d722494772acd8c340e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Mon, 17 Feb 2025 20:43:09 -0400 Subject: [PATCH] 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? --- src/components/componentcommand.ts | 10 +++++++++- src/components/handler.ts | 4 ++-- src/components/modalcommand.ts | 10 +++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/components/componentcommand.ts b/src/components/componentcommand.ts index e124657..a81aa23 100644 --- a/src/components/componentcommand.ts +++ b/src/components/componentcommand.ts @@ -14,9 +14,17 @@ export interface ComponentCommand { export abstract class ComponentCommand { type = InteractionCommandType.COMPONENT; abstract componentType: keyof ContextComponentCommandInteractionMap; - abstract filter(context: ComponentContext): Promise | boolean; + customId?: string; + filter?(context: ComponentContext): Promise | boolean; abstract run(context: ComponentContext): 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; diff --git a/src/components/handler.ts b/src/components/handler.ts index 89d1f93..9dae56e 100644 --- a/src/components/handler.ts +++ b/src/components/handler.ts @@ -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); } diff --git a/src/components/modalcommand.ts b/src/components/modalcommand.ts index 2f28be3..00448f9 100644 --- a/src/components/modalcommand.ts +++ b/src/components/modalcommand.ts @@ -8,9 +8,17 @@ export interface ModalCommand { export abstract class ModalCommand { type = InteractionCommandType.MODAL; - abstract filter(context: ModalContext): Promise | boolean; + filter?(context: ModalContext): Promise | 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;