From 84a0ce675440edc3be1185871a743c22817df59a Mon Sep 17 00:00:00 2001 From: MARCROCK22 Date: Thu, 27 Feb 2025 14:54:06 -0400 Subject: [PATCH] feat: onError createComponentCollector --- src/builders/types.ts | 4 ++++ src/components/handler.ts | 48 ++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/builders/types.ts b/src/builders/types.ts index e2c5317..18141d3 100644 --- a/src/builders/types.ts +++ b/src/builders/types.ts @@ -10,6 +10,9 @@ import type { BuilderSelectMenus } from './SelectMenu'; export type ComponentCallback< T extends ComponentInteraction | StringSelectMenuInteraction = ComponentInteraction | StringSelectMenuInteraction, > = (interaction: T, stop: ComponentStopCallback, refresh: ComponentRefreshCallback) => any; +export type ComponentOnErrorCallback< + T extends ComponentInteraction | StringSelectMenuInteraction = ComponentInteraction | StringSelectMenuInteraction, +> = (interaction: T, error: unknown, stop: ComponentStopCallback, refresh: ComponentRefreshCallback) => any; export type ComponentFilterCallback = (interaction: T) => any; export type ComponentStopCallback = ( reason: 'messageDelete' | 'channelDelete' | 'guildDelete' | 'idle' | 'timeout' | (string & {}) | undefined, @@ -30,4 +33,5 @@ export interface ListenerOptions { filter?: ComponentFilterCallback; onPass?: ComponentFilterCallback; onStop?: ComponentStopCallback; + onError?: ComponentOnErrorCallback; } diff --git a/src/components/handler.ts b/src/components/handler.ts index 9dae56e..2b3aca0 100644 --- a/src/components/handler.ts +++ b/src/components/handler.ts @@ -1,4 +1,11 @@ -import type { ComponentCallback, ListenerOptions, ModalSubmitCallback } from '../builders/types'; +import type { + ComponentCallback, + ComponentOnErrorCallback, + ComponentRefreshCallback, + ComponentStopCallback, + ListenerOptions, + ModalSubmitCallback, +} from '../builders/types'; import { LimitedCollection } from '../collection'; import { BaseCommand, type RegisteredMiddlewares, type UsingClient } from '../commands'; import type { FileLoaded } from '../commands/handler'; @@ -18,6 +25,7 @@ type COMPONENTS = { guildId: string | undefined; idle?: NodeJS.Timeout; timeout?: NodeJS.Timeout; + onError?: ComponentOnErrorCallback; __run: (customId: UserMatches, callback: ComponentCallback) => any; }; @@ -94,6 +102,7 @@ export class ComponentHandler extends BaseHandler { }); } }, + onError: options.onError, }); return { @@ -117,18 +126,31 @@ export class ComponentHandler extends BaseHandler { if (!(await row.options.filter(interaction))) return row.options.onPass?.(interaction); } row.idle?.refresh(); - await component.callback( - interaction, - reason => { - this.clearValue(id); - row.options?.onStop?.(reason ?? 'stop', () => { - this.createComponentCollector(row.messageId, row.channelId, row.guildId, row.options, row.components); - }); - }, - () => { - this.resetTimeouts(id); - }, - ); + + const stop: ComponentStopCallback = reason => { + this.clearValue(id); + row.options?.onStop?.(reason ?? 'stop', () => { + this.createComponentCollector(row.messageId, row.channelId, row.guildId, row.options, row.components); + }); + }; + + const refresh: ComponentRefreshCallback = () => { + this.resetTimeouts(id); + }; + + try { + await component.callback(interaction, stop, refresh); + } catch (err) { + try { + if (row.onError) { + await row.onError(interaction, err, stop, refresh); + } else { + this.client.logger.error('.components.onComponent', err); + } + } catch (err) { + this.client.logger.error('.components.onComponent', err); + } + } } hasComponent(id: string, customId: string) {