feat: componentcommand & modalcommand default methods

This commit is contained in:
MARCROCK22 2024-05-10 23:29:29 -04:00
parent 44b0a6354f
commit 69867d22ca
4 changed files with 66 additions and 26 deletions

View File

@ -38,6 +38,7 @@ import type {
ModalSubmitInteraction, ModalSubmitInteraction,
UserCommandInteraction, UserCommandInteraction,
} from '../structures'; } from '../structures';
import type { ComponentCommand, ComponentContext, ModalCommand, ModalContext } from '../components';
export class BaseClient { export class BaseClient {
rest!: ApiHandler; rest!: ApiHandler;
@ -117,6 +118,32 @@ export class BaseClient {
}, },
}, },
}, },
components: {
defaults: {
onRunError(context: ComponentContext, error: unknown): any {
context.client.logger.fatal('ComponentCommand.<onRunError>', context.author.id, error);
},
onMiddlewaresError(context: ComponentContext, error: string): any {
context.client.logger.fatal('ComponentCommand.<onMiddlewaresError>', context.author.id, error);
},
onInternalError(client: UsingClient, error?: unknown): any {
client.logger.fatal(error);
},
},
},
modals: {
defaults: {
onRunError(context: ModalContext, error: unknown): any {
context.client.logger.fatal('ComponentCommand.<onRunError>', context.author.id, error);
},
onMiddlewaresError(context: ModalContext, error: string): any {
context.client.logger.fatal('ComponentCommand.<onMiddlewaresError>', context.author.id, error);
},
onInternalError(client: UsingClient, error?: unknown): any {
client.logger.fatal(error);
},
},
},
} satisfies BaseClientOptions, } satisfies BaseClientOptions,
options, options,
); );
@ -349,6 +376,22 @@ export interface BaseClientOptions {
onAfterRun?: Command['onAfterRun']; onAfterRun?: Command['onAfterRun'];
}; };
}; };
components?: {
defaults?: {
onRunError?: ComponentCommand['onRunError'];
onInternalError?: ComponentCommand['onInternalError'];
onMiddlewaresError?: ComponentCommand['onMiddlewaresError'];
onAfterRun?: ComponentCommand['onAfterRun'];
};
};
modals?: {
defaults?: {
onRunError?: ModalCommand['onRunError'];
onInternalError?: ModalCommand['onInternalError'];
onMiddlewaresError?: ModalCommand['onMiddlewaresError'];
onAfterRun?: ModalCommand['onAfterRun'];
};
};
allowedMentions?: Omit<NonNullable<RESTPostAPIChannelMessageJSONBody['allowed_mentions']>, 'parse'> & { allowedMentions?: Omit<NonNullable<RESTPostAPIChannelMessageJSONBody['allowed_mentions']>, 'parse'> & {
parse?: ('everyone' | 'roles' | 'users')[]; //nice types, d-api parse?: ('everyone' | 'roles' | 'users')[]; //nice types, d-api
}; };

View File

@ -22,15 +22,9 @@ export abstract class ComponentCommand {
} }
onAfterRun?(context: ComponentContext, error: unknown | undefined): any; onAfterRun?(context: ComponentContext, error: unknown | undefined): any;
onRunError(context: ComponentContext, error: unknown): any { onRunError?(context: ComponentContext, error: unknown): any;
context.client.logger.fatal('ComponentCommand.<onRunError>', context.author.id, error); onMiddlewaresError?(context: ComponentContext, error: string): any;
} onInternalError?(client: UsingClient, error?: unknown): any;
onMiddlewaresError(context: ComponentContext, error: string): any {
context.client.logger.fatal('ComponentCommand.<onMiddlewaresError>', context.author.id, error);
}
onInternalError(client: UsingClient, error?: unknown): any {
client.logger.fatal(error);
}
middlewares: (keyof RegisteredMiddlewares)[] = []; middlewares: (keyof RegisteredMiddlewares)[] = [];
} }

View File

@ -171,6 +171,15 @@ export class ComponentHandler extends BaseHandler {
continue; continue;
} }
if (!(component instanceof ModalCommand) && !(component instanceof ComponentCommand)) continue; if (!(component instanceof ModalCommand) && !(component instanceof ComponentCommand)) continue;
if (component instanceof ModalCommand) {
component.onInternalError ??= this.client.options?.modals?.defaults?.onInternalError;
component.onMiddlewaresError ??= this.client.options?.modals?.defaults?.onMiddlewaresError;
component.onRunError ??= this.client.options?.modals?.defaults?.onRunError;
} else {
component.onInternalError ??= this.client.options?.components?.defaults?.onInternalError;
component.onMiddlewaresError ??= this.client.options?.components?.defaults?.onMiddlewaresError;
component.onRunError ??= this.client.options?.components?.defaults?.onRunError;
}
component.__filePath = paths[i].path; component.__filePath = paths[i].path;
this.commands.push(component); this.commands.push(component);
} }
@ -224,7 +233,7 @@ export class ComponentHandler extends BaseHandler {
return; return;
} }
if ('error' in resultRunGlobalMiddlewares) { if ('error' in resultRunGlobalMiddlewares) {
return i.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error'); return i.onMiddlewaresError?.(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
} }
const resultRunMiddlewares = await BaseCommand.__runMiddlewares(context, i.middlewares, false); const resultRunMiddlewares = await BaseCommand.__runMiddlewares(context, i.middlewares, false);
@ -232,19 +241,19 @@ export class ComponentHandler extends BaseHandler {
return; return;
} }
if ('error' in resultRunMiddlewares) { if ('error' in resultRunMiddlewares) {
return i.onMiddlewaresError(context, resultRunMiddlewares.error ?? 'Unknown error'); return i.onMiddlewaresError?.(context, resultRunMiddlewares.error ?? 'Unknown error');
} }
try { try {
await i.run(context); await i.run(context);
await i.onAfterRun?.(context, undefined); await i.onAfterRun?.(context, undefined);
} catch (error) { } catch (error) {
await i.onRunError(context, error); await i.onRunError?.(context, error);
await i.onAfterRun?.(context, error); await i.onAfterRun?.(context, error);
} }
} catch (error) { } catch (error) {
try { try {
await i.onInternalError(this.client, error); await i.onInternalError?.(this.client, error);
} catch { } catch {
// supress error // supress error
} }
@ -271,7 +280,7 @@ export class ComponentHandler extends BaseHandler {
return; return;
} }
if ('error' in resultRunGlobalMiddlewares) { if ('error' in resultRunGlobalMiddlewares) {
return i.onMiddlewaresError(context, resultRunGlobalMiddlewares.error ?? 'Unknown error'); return i.onMiddlewaresError?.(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
} }
const resultRunMiddlewares = await BaseCommand.__runMiddlewares(context, i.middlewares, false); const resultRunMiddlewares = await BaseCommand.__runMiddlewares(context, i.middlewares, false);
@ -279,19 +288,19 @@ export class ComponentHandler extends BaseHandler {
return; return;
} }
if ('error' in resultRunMiddlewares) { if ('error' in resultRunMiddlewares) {
return i.onMiddlewaresError(context, resultRunMiddlewares.error ?? 'Unknown error'); return i.onMiddlewaresError?.(context, resultRunMiddlewares.error ?? 'Unknown error');
} }
try { try {
await i.run(context); await i.run(context);
await i.onAfterRun?.(context, undefined); await i.onAfterRun?.(context, undefined);
} catch (error) { } catch (error) {
await i.onRunError(context, error); await i.onRunError?.(context, error);
await i.onAfterRun?.(context, error); await i.onAfterRun?.(context, error);
} }
} catch (error) { } catch (error) {
try { try {
await i.onInternalError(this.client, error); await i.onInternalError?.(this.client, error);
} catch { } catch {
// supress error // supress error
} }

View File

@ -14,13 +14,7 @@ export abstract class ModalCommand {
middlewares: (keyof RegisteredMiddlewares)[] = []; middlewares: (keyof RegisteredMiddlewares)[] = [];
onAfterRun?(context: ModalContext, error: unknown | undefined): any; onAfterRun?(context: ModalContext, error: unknown | undefined): any;
onRunError(context: ModalContext, error: unknown): any { onRunError?(context: ModalContext, error: unknown): any;
context.client.logger.fatal('ComponentCommand.<onRunError>', context.author.id, error); onMiddlewaresError?(context: ModalContext, error: string): any;
} onInternalError?(client: UsingClient, error?: unknown): any;
onMiddlewaresError(context: ModalContext, error: string): any {
context.client.logger.fatal('ComponentCommand.<onMiddlewaresError>', context.author.id, error);
}
onInternalError(client: UsingClient, error?: unknown): any {
client.logger.fatal(error);
}
} }