diff --git a/src/errorEvents/CoreCommandError.ts b/src/errorEvents/CoreCommandError.ts new file mode 100644 index 000000000..471281656 --- /dev/null +++ b/src/errorEvents/CoreCommandError.ts @@ -0,0 +1,14 @@ +import type { PieceContext } from '@sapphire/pieces'; +import { Event } from '../lib/structures/Event'; +import { CommandErrorPayload, Events } from '../lib/types/Events'; + +export class CoreEvent extends Event { + public constructor(context: PieceContext) { + super(context, { event: Events.CommandError }); + } + + public run(error: Error, context: CommandErrorPayload) { + const { name, path } = context.piece; + this.context.logger.error(`Encountered error on command "${name}" at path "${path}"`, error.stack); + } +} diff --git a/src/errorEvents/CoreEventError.ts b/src/errorEvents/CoreEventError.ts new file mode 100644 index 000000000..40815106b --- /dev/null +++ b/src/errorEvents/CoreEventError.ts @@ -0,0 +1,14 @@ +import type { PieceContext } from '@sapphire/pieces'; +import { Event } from '../lib/structures/Event'; +import { EventErrorPayload, Events } from '../lib/types/Events'; + +export class CoreEvent extends Event { + public constructor(context: PieceContext) { + super(context, { event: Events.EventError }); + } + + public run(error: Error, context: EventErrorPayload) { + const { name, event, path } = context.piece; + this.context.logger.error(`Encountered error on event listener "${name}" for event "${event}" at path "${path}"`, error.stack); + } +} diff --git a/src/lib/SapphireClient.ts b/src/lib/SapphireClient.ts index e57e73d0b..66e635ed6 100644 --- a/src/lib/SapphireClient.ts +++ b/src/lib/SapphireClient.ts @@ -68,6 +68,13 @@ export interface SapphireClientOptions { * @default { instance: new Logger(LogLevel.Info) } */ logger?: ClientLoggerOptions; + + /** + * If Sapphire should load our pre-included error event listeners that log any encountered errors to the [[SapphireClient.logger]] instance + * @since 1.0.0 + * @default true + */ + loadDefaultErrorEvents?: boolean; } /** @@ -199,6 +206,9 @@ export class SapphireClient extends Client { } this.logger = options.logger?.instance ?? new Logger(options.logger?.level ?? LogLevel.Info); + + Store.injectedContext.logger = this.logger; + this.fetchPrefix = options.fetchPrefix ?? (() => this.options.defaultPrefix ?? null); for (const plugin of SapphireClient.plugins.values(PluginHook.PreInitialization)) { @@ -212,6 +222,8 @@ export class SapphireClient extends Client { this.events = new EventStore().registerPath(join(__dirname, '..', 'events')); this.preconditions = new PreconditionStore().registerPath(join(__dirname, '..', 'preconditions')); + if (options.loadDefaultErrorEvents !== false) this.events.registerPath(join(__dirname, '..', 'errorEvents')); + this.stores = new Set(); this.registerStore(this.arguments) // .registerStore(this.commands) @@ -333,5 +345,6 @@ declare module 'discord.js' { declare module '@sapphire/pieces' { interface PieceContextExtras { client: SapphireClient; + logger: ILogger; } }