diff --git a/packages/gil/lib/structures/Task.ts b/packages/gil/lib/structures/Task.ts index e113191c..7709119c 100644 --- a/packages/gil/lib/structures/Task.ts +++ b/packages/gil/lib/structures/Task.ts @@ -2,19 +2,21 @@ import { Collection } from "@discordjs/collection"; import glob from "fast-glob"; import { GilClient } from "../GilClient"; import { Manager } from "./Manager"; +import Cron from "node-cron"; interface TaskOptions { // The internal-safe name of the task name: string; - // The interval to run the task. You can put anything that https://github.com/breejs/bree supports. - // For example, you can use crons like "0 0 * * *" to run the task every day at midnight. + // A cron representing the interval at which the task should run. interval: string; + // Whether the task should run immediately upon startup. + runImmediately?: boolean; } export abstract class Task { public constructor( public readonly gil: GilClient, public readonly options: TaskOptions, - ) {} + ) { } public abstract execute(): unknown | Promise; } @@ -48,6 +50,13 @@ export class TaskManager extends Manager { const createdTask: Task = new imported.default(this.gil); this.gil.logger.info(`Task ${createdTask.options.name} loaded.`); this.tasks.set(createdTask.options.name, createdTask); + + if (createdTask.options.runImmediately) { + this.gil.logger.info(`Running task ${createdTask.options.name} immediately.`); + createdTask.execute(); + } + + Cron.schedule(createdTask.options.interval, createdTask.execute.bind(createdTask)); } } } diff --git a/packages/gil/package.json b/packages/gil/package.json index 6145e016..85ee9f7e 100644 --- a/packages/gil/package.json +++ b/packages/gil/package.json @@ -1,6 +1,6 @@ { "name": "@guildedjs/gil", - "version": "0.6.3", + "version": "0.6.4", "description": "Framework for guilded.js that allows you to build bots with ease.", "author": "Zaid \"Nico\" ", "license": "MIT", @@ -16,6 +16,7 @@ "devDependencies": { "@types/better-sqlite3": "^7.6.9", "@types/mongoose": "^5.11.97", + "@types/node-cron": "^3.0.11", "better-sqlite3": "^9.4.3", "dotenv": "^16.0.3", "mongoose": "^8.2.3", @@ -30,6 +31,7 @@ "fast-glob": "^3.3.2", "guilded.js": "workspace:*", "lexure": "^0.17.0", + "node-cron": "^3.0.3", "typed-emitter": "^2.1.0" }, "contributors": [