feat: onError createComponentCollector

This commit is contained in:
MARCROCK22 2025-02-27 14:54:06 -04:00
parent 7af24bf043
commit 84a0ce6754
2 changed files with 39 additions and 13 deletions

View File

@ -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<T = ComponentInteraction> = (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;
}

View File

@ -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 => {
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('<Client>.components.onComponent', err);
}
} catch (err) {
this.client.logger.error('<Client>.components.onComponent', err);
}
}
}
hasComponent(id: string, customId: string) {