Many bots have features that need to run periodically, such as uploading analytics data, reminders for users, birthdays, scheduled giveaways, undoing moderation actions, and more. Several implemented solutions exist for this, but as with many time-based processing attempts, they are often flawed and unreliable. This plugin is our solution, enabling you to schedule tasks and save them in services like Redis and SQS with ease.
- Full TypeScript support
- Includes ESM entrypoint
@sapphire/plugin-scheduled-tasks
depends on the following packages. Be sure to install these along with this package!
In case you want to use bullmq as your provider:
In case you want to use sqs as your provider:
You can use the following command to install this package along with bullmq
, or replace npm install
with your package manager of choice.
npm install @sapphire/plugin-scheduled-tasks @sapphire/framework bullmq
or with sqs
npm install @sapphire/plugin-scheduled-tasks @sapphire/framework sqs-consumer sqs-producer
This registers the necessary options and methods in the Sapphire client to be able to use the schedule plugin.
// Main bot file
// Be sure to register the plugin before instantiating the client.
import { ScheduledTaskRedisStrategy } from '@sapphire/plugin-scheduled-tasks/register-redis';
// Or if you want to use sqs
import { ScheduledTaskSQSStrategy } from '@sapphire/plugin-scheduled-tasks/register-sqs';
Then, you can pass the imported Strategy into the configuration options in your SapphireClient extension class or initializer. This will either be located in your new SapphireClient constructor call, or super in your constructor method if you use an extension class.
const options = {
...otherClientOptionsGoHere,
tasks: {
// Using bullmq (redis)
strategy: new ScheduledTaskRedisStrategy({
/* You can add your Bull options here, for example we can configure custom Redis connection options: */
bull: {
connection: {
port: 8888, // Defaults to 6379, but if your Redis server runs on another port configure it here
password: 'very-strong-password', // If your Redis server requires a password configure it here
host: 'localhost', // The host at which the redis server is found
db: 2 // Redis database number, defaults to 0 but can be any value between 0 and 15
}
}
}),
// Or with SQS
strategy: new ScheduledTaskSQSStrategy({
/* You can add your SQS options here */
})
}
};
In order to use the scheduled tasks anywhere other than a piece (commands, arguments, preconditions, etc.), you must first import the container
property of @sapphire/framework
. For pieces, you can simply use this.container.tasks
to access this plugin's methods.
This is a simple example that creates a task to be run in 2 seconds from a service.
import { container } from '@sapphire/framework';
export class MyAwesomeService {
public createAwesomeTask() {
container.tasks.create('name', { id: '123' }, 2000);
}
}
Here is an example mute command, demonstrating the use of this.container.tasks
from within a piece by omitting the explicit import.
// mute command
import { Command, CommandOptions, PieceContext } from '@sapphire/framework';
import type { Message } from 'discord.js';
export class MuteCommand extends Command {
public constructor(context: PieceContext) {
super(context, {
description: 'Mute a user'
});
}
public async run(message: Message) {
// create a task to unmute the user in 1 minute
this.container.tasks.create('unmute', { authorId: message.author.id }, 60_000);
}
}
Scheduled tasks use their own store, like other types of pieces. You can create a directory alongside your commands directory named scheduled-tasks
and place your tasks there, but they must inherit from ScheduledTask
, like so.
import { ScheduledTask } from '@sapphire/plugin-scheduled-tasks';
export class ManualTask extends ScheduledTask {
public constructor(context: ScheduledTask.Context, options: ScheduledTask.Options) {
super(context, options);
}
public async run(payload: unknown) {
this.container.logger.info('I ran!', payload);
}
}
declare module '@sapphire/plugin-scheduled-tasks' {
interface ScheduledTasks {
manual: never;
}
}
container.tasks.create('manual', payload, 5000);
Cron jobs are currently only supported by the Redis strategy.
import { ScheduledTask } from '@sapphire/plugin-scheduled-tasks';
export class CronTask extends ScheduledTask {
public constructor(context: ScheduledTask.Context, options: ScheduledTask.Options) {
super(context, {
...options,
cron: '0 * * * *'
});
}
public async run() {
this.container.logger.info('I run every hour at *:00');
}
}
declare module '@sapphire/plugin-scheduled-tasks' {
interface ScheduledTasks {
cron: never;
}
}
Cron & Interval tasks are loaded automatically.
import { ScheduledTask } from '@sapphire/plugin-scheduled-tasks';
export class IntervalTask extends ScheduledTask {
public constructor(context: ScheduledTask.Context, options: ScheduledTask.Options) {
super(context, {
...options,
interval: 60_000 // 60 seconds
});
}
public async run() {
this.container.logger.info('I run every minute');
}
}
declare module '@sapphire/plugin-scheduled-tasks' {
interface ScheduledTasks {
interval: never;
}
}
Cron & Interval tasks are loaded automatically.
Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!
We accept donations through Open Collective, Ko-fi, Paypal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.
Donate With | Address |
---|---|
Open Collective | Click Here |
Ko-fi | Click Here |
Patreon | Click Here |
PayPal | Click Here |
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!