mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 04:56:07 +00:00
feat: componentcommand & modalcommand default methods
This commit is contained in:
parent
44b0a6354f
commit
69867d22ca
@ -38,6 +38,7 @@ import type {
|
||||
ModalSubmitInteraction,
|
||||
UserCommandInteraction,
|
||||
} from '../structures';
|
||||
import type { ComponentCommand, ComponentContext, ModalCommand, ModalContext } from '../components';
|
||||
|
||||
export class BaseClient {
|
||||
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,
|
||||
options,
|
||||
);
|
||||
@ -349,6 +376,22 @@ export interface BaseClientOptions {
|
||||
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'> & {
|
||||
parse?: ('everyone' | 'roles' | 'users')[]; //nice types, d-api
|
||||
};
|
||||
|
@ -22,15 +22,9 @@ export abstract class ComponentCommand {
|
||||
}
|
||||
|
||||
onAfterRun?(context: ComponentContext, error: unknown | undefined): any;
|
||||
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);
|
||||
}
|
||||
onRunError?(context: ComponentContext, error: unknown): any;
|
||||
onMiddlewaresError?(context: ComponentContext, error: string): any;
|
||||
onInternalError?(client: UsingClient, error?: unknown): any;
|
||||
|
||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
||||
}
|
||||
|
@ -171,6 +171,15 @@ export class ComponentHandler extends BaseHandler {
|
||||
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;
|
||||
this.commands.push(component);
|
||||
}
|
||||
@ -224,7 +233,7 @@ export class ComponentHandler extends BaseHandler {
|
||||
return;
|
||||
}
|
||||
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);
|
||||
@ -232,19 +241,19 @@ export class ComponentHandler extends BaseHandler {
|
||||
return;
|
||||
}
|
||||
if ('error' in resultRunMiddlewares) {
|
||||
return i.onMiddlewaresError(context, resultRunMiddlewares.error ?? 'Unknown error');
|
||||
return i.onMiddlewaresError?.(context, resultRunMiddlewares.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
try {
|
||||
await i.run(context);
|
||||
await i.onAfterRun?.(context, undefined);
|
||||
} catch (error) {
|
||||
await i.onRunError(context, error);
|
||||
await i.onRunError?.(context, error);
|
||||
await i.onAfterRun?.(context, error);
|
||||
}
|
||||
} catch (error) {
|
||||
try {
|
||||
await i.onInternalError(this.client, error);
|
||||
await i.onInternalError?.(this.client, error);
|
||||
} catch {
|
||||
// supress error
|
||||
}
|
||||
@ -271,7 +280,7 @@ export class ComponentHandler extends BaseHandler {
|
||||
return;
|
||||
}
|
||||
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);
|
||||
@ -279,19 +288,19 @@ export class ComponentHandler extends BaseHandler {
|
||||
return;
|
||||
}
|
||||
if ('error' in resultRunMiddlewares) {
|
||||
return i.onMiddlewaresError(context, resultRunMiddlewares.error ?? 'Unknown error');
|
||||
return i.onMiddlewaresError?.(context, resultRunMiddlewares.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
try {
|
||||
await i.run(context);
|
||||
await i.onAfterRun?.(context, undefined);
|
||||
} catch (error) {
|
||||
await i.onRunError(context, error);
|
||||
await i.onRunError?.(context, error);
|
||||
await i.onAfterRun?.(context, error);
|
||||
}
|
||||
} catch (error) {
|
||||
try {
|
||||
await i.onInternalError(this.client, error);
|
||||
await i.onInternalError?.(this.client, error);
|
||||
} catch {
|
||||
// supress error
|
||||
}
|
||||
|
@ -14,13 +14,7 @@ export abstract class ModalCommand {
|
||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
||||
|
||||
onAfterRun?(context: ModalContext, error: unknown | undefined): any;
|
||||
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);
|
||||
}
|
||||
onRunError?(context: ModalContext, error: unknown): any;
|
||||
onMiddlewaresError?(context: ModalContext, error: string): any;
|
||||
onInternalError?(client: UsingClient, error?: unknown): any;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user