Skip to content

Commit

Permalink
feat(scheduled-tasks): add error listeners (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
killbasa authored Nov 10, 2023
1 parent 95fe74c commit ccbe75e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/scheduled-tasks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ declare module '@sapphire/pieces' {
declare module 'discord.js' {
export interface ClientOptions {
tasks?: ScheduledTaskHandlerOptions;
/**
* If the the pre-included scheduled task error listeners should be loaded
* @default false
*/
loadScheduledTaskErrorListeners?: boolean;
}
}
15 changes: 15 additions & 0 deletions packages/scheduled-tasks/src/listeners/connectError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Listener } from '@sapphire/framework';
import { ScheduledTaskEvents } from '../lib/types/ScheduledTaskEvents';

export class PluginListener extends Listener<typeof ScheduledTaskEvents.ScheduledTaskStrategyConnectError> {
public constructor(context: Listener.Context) {
super(context, {
name: ScheduledTaskEvents.ScheduledTaskStrategyConnectError,
event: ScheduledTaskEvents.ScheduledTaskStrategyConnectError
});
}

public override run(error: unknown) {
this.container.logger.error(`[ScheduledTaskPlugin] Encountered an error when trying to connect to the Redis instance`, error);
}
}
20 changes: 20 additions & 0 deletions packages/scheduled-tasks/src/listeners/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Listener } from '@sapphire/framework';
import { ScheduledTaskEvents } from '../lib/types/ScheduledTaskEvents';

export class PluginListener extends Listener<typeof ScheduledTaskEvents.ScheduledTaskError> {
public constructor(context: Listener.Context) {
super(context, {
name: ScheduledTaskEvents.ScheduledTaskError,
event: ScheduledTaskEvents.ScheduledTaskError
});
}

public override run(error: unknown, task: string) {
let message = `Encountered error on scheduled task "${task}"`;

const piece = this.container.stores.get('scheduled-tasks').get(task);
if (piece) message = message.concat(` at path "${piece.location.full}"`);

this.container.logger.error(message, error);
}
}
15 changes: 15 additions & 0 deletions packages/scheduled-tasks/src/listeners/notFound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Listener } from '@sapphire/framework';
import { ScheduledTaskEvents } from '../lib/types/ScheduledTaskEvents';

export class PluginListener extends Listener<typeof ScheduledTaskEvents.ScheduledTaskNotFound> {
public constructor(context: Listener.Context) {
super(context, {
name: ScheduledTaskEvents.ScheduledTaskNotFound,
event: ScheduledTaskEvents.ScheduledTaskNotFound
});
}

public override run(task: string) {
this.container.logger.error(`[ScheduledTaskPlugin] There was no task found for "${task}"`);
}
}
7 changes: 6 additions & 1 deletion packages/scheduled-tasks/src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { container, Plugin, postInitialization, postLogin, preGenericsInitializa
import type { ClientOptions } from 'discord.js';
import { ScheduledTaskHandler } from './lib/ScheduledTaskHandler';
import { ScheduledTaskStore } from './lib/structures/ScheduledTaskStore';
import { join } from 'node:path';

/**
* A plugin for scheduling tasks in a SapphireClient.
Expand All @@ -21,8 +22,12 @@ export class ScheduledTasksPlugin extends Plugin {
/**
* @since 1.0.0
*/
public static [postInitialization](this: SapphireClient): void {
public static [postInitialization](this: SapphireClient, options: ClientOptions): void {
this.stores.register(new ScheduledTaskStore());

if (options.loadScheduledTaskErrorListeners === true) {
this.stores.get('listeners').registerPath(join(__dirname, 'listeners'));
}
}

/**
Expand Down

0 comments on commit ccbe75e

Please sign in to comment.