update collectors

This commit is contained in:
MARCROCK22 2024-03-22 13:09:21 -04:00
parent 8914b8c24c
commit 3e4bd81a35

View File

@ -6,12 +6,12 @@ import type { ComponentInteraction, ModalSubmitInteraction } from '../structures
import { ComponentCommand, InteractionCommandType, ModalCommand } from './command'; import { ComponentCommand, InteractionCommandType, ModalCommand } from './command';
type COMPONENTS = { type COMPONENTS = {
components: Partial<Record<string, ComponentCallback>>; components: { match: string | string[] | RegExp; callback: ComponentCallback }[];
options?: ListenerOptions; options?: ListenerOptions;
messageId?: string; messageId?: string;
idle?: NodeJS.Timeout; idle?: NodeJS.Timeout;
timeout?: NodeJS.Timeout; timeout?: NodeJS.Timeout;
__run: (customId: string, callback: ComponentCallback) => any; __run: (customId: string | string[] | RegExp, callback: ComponentCallback) => any;
}; };
export class ComponentHandler extends BaseHandler { export class ComponentHandler extends BaseHandler {
@ -35,7 +35,7 @@ export class ComponentHandler extends BaseHandler {
createComponentCollector(messageId: string, options: ListenerOptions = {}) { createComponentCollector(messageId: string, options: ListenerOptions = {}) {
this.values.set(messageId, { this.values.set(messageId, {
components: {}, components: [],
options, options,
idle: options.idle idle: options.idle
? setTimeout(() => { ? setTimeout(() => {
@ -55,7 +55,10 @@ export class ComponentHandler extends BaseHandler {
: undefined, : undefined,
__run: (customId, callback) => { __run: (customId, callback) => {
if (this.values.has(messageId)) { if (this.values.has(messageId)) {
this.values.get(messageId)!.components[customId] = callback; this.values.get(messageId)!.components.push({
callback,
match: customId,
});
} }
}, },
}); });
@ -72,14 +75,18 @@ export class ComponentHandler extends BaseHandler {
} }
async onComponent(id: string, interaction: ComponentInteraction) { async onComponent(id: string, interaction: ComponentInteraction) {
const row = this.values.get(id); const row = this.values.get(id)!;
const component = row?.components?.[interaction.customId]; const component = row?.components?.find(x => {
if (typeof x.match === 'string') return x.match === interaction.customId;
if (Array.isArray(x.match)) return x.match.includes(interaction.customId);
return interaction.customId.match(x.match);
});
if (!component) return; if (!component) return;
if (row.options?.filter) { if (row.options?.filter) {
if (!(await row.options.filter(interaction))) return; if (!(await row.options.filter(interaction))) return;
} }
row.idle?.refresh(); row.idle?.refresh();
await component( await component.callback(
interaction, interaction,
reason => { reason => {
row.options?.onStop?.(reason ?? 'stop'); row.options?.onStop?.(reason ?? 'stop');
@ -92,7 +99,11 @@ export class ComponentHandler extends BaseHandler {
} }
hasComponent(id: string, customId: string) { hasComponent(id: string, customId: string) {
return this.values.get(id)?.components?.[customId]; return this.values.get(id)?.components?.some(x => {
if (typeof x.match === 'string' && x.match === customId) return true;
if (Array.isArray(x.match)) return x.match.includes(customId);
return customId.match(x.match);
});
} }
resetTimeouts(id: string) { resetTimeouts(id: string) {