Task management plugin for CommandKit. Provides on-demand task creation and management with support for both static and dynamic tasks.
- Static Tasks: Define tasks in your codebase that run on schedules
- Dynamic Tasks: Create tasks on-demand from commands or events
- Multiple Drivers: Support for in-memory, SQLite, and BullMQ persistence
- HMR Support: Hot reload tasks during development
- Flexible Scheduling: Support for cron expressions, dates, and dynamic schedules
npm install @commandkit/tasksimport { defineConfig } from 'commandkit/config';
import { tasks } from '@commandkit/tasks';
export default defineConfig({
plugins: [tasks()],
});By default, the plugin will initialize the sqlite driver. You can set up a different driver by calling setDriver function from the @commandkit/tasks package. If you want to disable the default driver initialization behavior, you can pass initializeDefaultDriver: false to the tasks() options in your commandkit config.
import { setDriver } from '@commandkit/tasks';
import { SQLiteDriver } from '@commandkit/tasks/sqlite';
setDriver(new SQLiteDriver('./tasks.db'));Create a file in src/app/tasks/:
import { task } from '@commandkit/tasks';
export default task({
name: 'refresh-exchange-rate',
schedule: { type: 'cron', value: '0 0 * * *' }, // daily at midnight
async execute(ctx) {
// Fetch latest exchange rates
const rates = await fetchExchangeRates();
await updateDatabase(rates);
},
});import type { CommandData, ChatInputCommand } from 'commandkit';
import { ApplicationCommandOptionType } from 'discord.js';
import ms from 'ms';
import { createTask } from '@commandkit/tasks';
export const command: CommandData = {
name: 'remind-me',
description: 'Set a reminder',
options: [
{
name: 'time',
description: 'The time to remind after. Eg: 6h, 10m, 1d',
type: ApplicationCommandOptionType.String,
required: true,
},
{
name: 'message',
description: 'The message to remind about.',
type: ApplicationCommandOptionType.String,
required: true,
},
],
};
export const chatInput: ChatInputCommand = async (ctx) => {
const time = ctx.options.getString('time', true);
const message = ctx.options.getString('message', true);
const timeMs = Date.now() + ms(time as `${number}`);
await createTask({
name: 'reminder',
data: {
userId: ctx.interaction.user.id,
message,
channelId: ctx.interaction.channelId,
setAt: Date.now(),
},
schedule: {
type: 'date',
value: timeMs,
},
});
await ctx.interaction.reply(
`I will remind you <t:${Math.floor(timeMs / 1000)}:R> for \`${message}\``,
);
};interface TaskDefinition {
name: string;
schedule?: TaskSchedule;
prepare?: (ctx: TaskContext) => Promise<boolean>;
execute: (ctx: TaskContext) => Promise<void>;
}type TaskSchedule =
| { type: 'cron'; value: string; timezone?: string }
| { type: 'date'; value: Date | number; timezone?: string };Creates a task definition.
Creates a dynamic task.
Deletes a scheduled task.
Persistent job queue with recovery on restart.
import { SQLiteDriver } from '@commandkit/tasks/sqlite';
setDriver(new SQLiteDriver('./tasks.db'));Features:
- Jobs recoverable on restart
- Persistent job data
- Cron expression support via cron-parser
Distributed task scheduling with Redis.
import { BullMQDriver } from '@commandkit/tasks/bullmq';
setDriver(new BullMQDriver({
host: 'localhost',
port: 6379,
}));