Skip to Content
nyx logonyx guide

Schedules Overview

Reading time:3 min read

Schedules are objects that are ticked some time in the future, and can be used as a feature complete way of scheduling events in your bot, apart from Node's setInterval() API.

In essence, a Schedule object contains its ticking logic and its interval of when it wishes to be executed, which can be in cron format, in an amount of ms (number), or as a Date.

Quick example

Let's create a schedule that updates the activity with the member count every half hour:

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

class  extends  {
  protected readonly  = '*/30 * * * *';

  public async (: ) {
    // the discord.js Client of the bot that ticked this schedule
    const  = .();
    if (!.) return;

    const  = ...((, ) =>  + ., 0);
    await ..(`Members: ${}`);
  }
}

await .().(new ());

Given it uses the provided metadata, it'll work even if the same instance is used across multiple bots.

Schedule Job

When a schedule is added, a job will be created for it, which is responsible for notifying the framework when it is time to run it. It's a wrapper over a more "lower-level" component that will actually do the ticking, like a cron job, a timer, etc.

How the job is created is entirely customizable. By default, cron strings and Dates use the croner npm package, and millisecond intervals use a timer with millisecond precision.

There are multiple ways to get it, but given a schedule itself does not depend on a bot, you'll always need the bot:

const  = .().().();
const  = .();
const  = await .().();

It's the entry point for interacting with the schedule's ticking logic, and can be used to pause or resume it externally.

await .();
await .();

// A croner `Cron` for cron strings and Dates, or the current timeout for ms intervals
// Depending on this is fragile and not intended, but it doesn't change for the same major version
const  = .();

Metadata

When extending nyx with plugins, you may want to specify extra data for them. You can do so with the Metadata object, saved on the schedule.

While schedules internally save a Metadata, the return type of #getMeta() is a ReadonlyMetadata, meaning that external objects can read the object, but not modify it.

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

class  extends  {
  protected override readonly  = {
    : 'someValue',
  };
}

const  = new ();
const  = .();
const  = ['someKey']; // 'someValue'

For a more type-safe approach, you can use TypedFields. Check the Metadata page for more information.

On this page