Skip to content

Package

plugin-tasks v0.1.0-next.1

Changelog

seedcordseedcord

The whole Discord bot, typed end to end

Website · Guide · Reference · Discord

npm node license

About

@seedcord/plugin-tasks runs your bot's code on a schedule. A task with a cron pattern runs on that schedule, and any task can run once at a later time, like an unban a day from now.

Install it when your bot needs recurring work or a job that runs later. It works with a gateway bot and with an http bot on your own server. On a Cloudflare Workers bot, attaching it is a compile error.

seedcord is in beta, so a minor version can break your bot. Each release's changelog says what changed and how to update.

Installation

pnpm add @seedcord/plugin-tasks

Usage

Attach the plugin in bot.ts and point it at the folder that holds your tasks:

// bot.ts
import { Seedcord } from '@seedcord/gateway';
import { Tasks } from '@seedcord/plugin-tasks';
import { GatewayIntentBits } from 'discord.js';

export default new Seedcord({
    bot: {
        clientOptions: { intents: [GatewayIntentBits.Guilds] },
        interactions: { path: './handlers' },
        commands: { path: './commands' },
        events: { path: null }
    },
    subscribers: { path: null }
}).attach('tasks', Tasks, { path: './tasks', timezone: 'UTC' });

The plugin loads every class in tasks/ that has @RegisterTask. This one runs every five minutes:

// tasks/Heartbeat.ts
import { RegisterTask, Task } from '@seedcord/plugin-tasks/gateway';

@RegisterTask('*/5 * * * *')
export class Heartbeat extends Task {
    public async execute(): Promise<void> {
        this.logger.info(`${this.core.bot.client.guilds.cache.size} servers`);
    }
}

A typo in a literal pattern fails to compile. The Tasks guide teaches patterns and timezones. Scheduling jobs shows how to run a task once later and keep the job through a restart. Controlling tasks shows how to pause, run, and report tasks from a handler.