Skip to Content
nyx logonyx guide

Schedules

Reading time:1 min read

The ScheduleLoader docs is the simplest loader. It scans a directory recursively for any class extending AbstractSchedule, instantiates them, and returns a flat Schedule[].

Folder structure

Any .js or .mjs file at any depth that exports a class extending AbstractSchedule will be picked up:

daily.js
weekly.js
prune-messages.js
archive-logs.js

Usage

By default, schedules are automatically registered via scheduleManager.addSchedule():

import {  } from '@nyx-discord/loaders';

await .({ , : './dist/schedules' });

Pass register: false to handle registration yourself:

import {  } from '@nyx-discord/loaders';

const  = await .({
  ,
  : './dist/schedules',
  : false,
});

for (const  of ) {
  await .().();
}

Writing schedules for the loader

import {  } from '@nyx-discord/djs';
import type {  } from '@nyx-discord/djs';

export class  extends  {
  protected readonly  = '0 0 * * *'; // Every day at midnight

  public async (: ) {
    // Send daily report...
  }
}

Schedules are instantiated with new Class() (no arguments). They don't receive a parent instance.

Alternatively, you can specify a static create(bot) factory:

import {  } from '@nyx-discord/djs';
import type { ,  } from '@nyx-discord/djs';

export class  extends  {
  protected readonly  = '0 0 * * *';

  public static (: ) {
    return new ();
  }

  // ...
}

The loader will call this method instead of the constructor if it exists. This allows you to access the bot instance, or add logic, for example to inject services or other dependencies with a DI container.

Edit on GitHub

Last updated on

On this page