fix: onFail callback

This commit is contained in:
MARCROCK22 2024-03-26 18:45:45 -04:00
parent 9320010161
commit a059959fb5
2 changed files with 8 additions and 13 deletions

View File

@ -19,6 +19,8 @@ export interface ComponentHandlerLike {
readonly commands: (ComponentCommand | ModalCommand)[];
readonly modals: Map<string, ModalSubmitCallback> | LimitedCollection<string, ModalSubmitCallback>;
onFail: ComponentHandler['onFail'];
createComponentCollector: ComponentHandler['createComponentCollector'];
hasModal: ComponentHandler['hasModal'];
@ -37,7 +39,7 @@ export interface ComponentHandlerLike {
}
export class ComponentHandler extends BaseHandler {
onFail?: OnFailCallback;
onFail: OnFailCallback = err => this.logger.warn('<Client>.components.onFail', err);
readonly values = new Map<string, COMPONENTS>();
// 10 minutes timeout, because discord dont send an event when the user cancel the modal
readonly modals = new LimitedCollection<string, ModalSubmitCallback>({ expire: 60e3 * 10 });
@ -51,10 +53,6 @@ export class ComponentHandler extends BaseHandler {
super(logger);
}
set OnFail(cb: OnFailCallback) {
this.onFail = cb;
}
createComponentCollector(messageId: string, options: ListenerOptions = {}) {
this.values.set(messageId, {
components: [],
@ -229,7 +227,7 @@ export class ComponentHandler extends BaseHandler {
break;
}
} catch (e) {
await this.onFail?.(e);
await this.onFail(e);
}
}
}
@ -242,7 +240,7 @@ export class ComponentHandler extends BaseHandler {
break;
}
} catch (e) {
await this.onFail?.(e);
await this.onFail(e);
}
}
}

View File

@ -26,18 +26,15 @@ export interface EventHandlerLike {
reload: EventHandler['reload'];
reloadAll: EventHandler['reloadAll'];
values: EventHandler['values'];
onFail: EventHandler['onFail'];
}
export class EventHandler extends BaseHandler {
protected onFail: OnFailCallback = err => this.logger.warn('<Client>.events.OnFail', err);
onFail: OnFailCallback = err => this.logger.warn('<Client>.events.onFail', err);
protected filter = (path: string) => path.endsWith('.js') || (!path.endsWith('.d.ts') && path.endsWith('.ts'));
values: Partial<Record<GatewayEvents, EventValue>> = {};
set OnFail(cb: OnFailCallback) {
this.onFail = cb;
}
async load(eventsDir: string) {
for (const i of await this.loadFilesK<ClientEvent>(await this.getFiles(eventsDir))) {
const instance = i.file;
@ -108,7 +105,7 @@ export class EventHandler extends BaseHandler {
const hook = await RawEvents[name]?.(client, packet as never);
await Event.run(...[hook, client, shardId]);
} catch (e) {
await this.onFail?.(e);
await this.onFail(e);
}
}