️⌚ Schedules
Schedules
are objects that will be 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.
It consists of:
- An
Identifier
, to uniquely identify it. - The
tick()
method, which will be called when it's ticked. - A
ScheduleInterval
, which can be a string on cron format, a number in milliseconds, or aDate
. - An optional
ScheduleFilter
, which can allow or deny its tick. - A
ReadonlyMetaCollection
, which contains meta information about the schedule, meant to be read by plugins. onRegister()
andonUnregister()
methods, which will be called when the schedule is registered and unregistered respectively.
Schedules aren't ticked by themselves, and rely on a job to do that. You can read more information about jobs on the 🔍 Schedule Execution Scheduler documentation.
info
Filters are not bot aware, meaning that you can reuse the same instance for many bots.
👷 Creation
You can create a schedule by either:
- Extending
AbstractSchedule
from@framework
(recommended). - Implementing the
Schedule
interface from@core
.
- Extending AbstractSchedule
- Implementing Schedule
import { AbstractSchedule } from '@nyx-discord/framework';
class MySchedule extends AbstractSchedule {
// Cron format (every half hour). Can also be a Date or number in ms.
protected readonly interval = '*/30 * * * *';
public async tick(meta: ScheduleTickMeta) {
const bot = meta.getBot();
bot.getLogger().log("Hello world!");
}
}
const schedule = new MySchedule();
await bot.getScheduleManager().addSchedule(schedule);
class MySchedule implements Schedule {
// ...
}
const schedule = new MySchedule();
await bot.getScheduleManager().addSchedule(schedule);