Skip to content

Commit 1eca3a7

Browse files
committed
feat: task scheduling
1 parent 424486e commit 1eca3a7

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

packages/gil/lib/structures/Task.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ import { Collection } from "@discordjs/collection";
22
import glob from "fast-glob";
33
import { GilClient } from "../GilClient";
44
import { Manager } from "./Manager";
5+
import Cron from "node-cron";
56

67
interface TaskOptions {
78
// The internal-safe name of the task
89
name: string;
9-
// The interval to run the task. You can put anything that https://github.com/breejs/bree supports.
10-
// For example, you can use crons like "0 0 * * *" to run the task every day at midnight.
10+
// A cron representing the interval at which the task should run.
1111
interval: string;
12+
// Whether the task should run immediately upon startup.
13+
runImmediately?: boolean;
1214
}
1315
export abstract class Task {
1416
public constructor(
1517
public readonly gil: GilClient,
1618
public readonly options: TaskOptions,
17-
) {}
19+
) { }
1820

1921
public abstract execute(): unknown | Promise<unknown>;
2022
}
@@ -48,6 +50,13 @@ export class TaskManager extends Manager {
4850
const createdTask: Task = new imported.default(this.gil);
4951
this.gil.logger.info(`Task ${createdTask.options.name} loaded.`);
5052
this.tasks.set(createdTask.options.name, createdTask);
53+
54+
if (createdTask.options.runImmediately) {
55+
this.gil.logger.info(`Running task ${createdTask.options.name} immediately.`);
56+
createdTask.execute();
57+
}
58+
59+
Cron.schedule(createdTask.options.interval, createdTask.execute.bind(createdTask));
5160
}
5261
}
5362
}

packages/gil/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@guildedjs/gil",
3-
"version": "0.6.3",
3+
"version": "0.6.4",
44
"description": "Framework for guilded.js that allows you to build bots with ease.",
55
"author": "Zaid \"Nico\" <[email protected]>",
66
"license": "MIT",
@@ -16,6 +16,7 @@
1616
"devDependencies": {
1717
"@types/better-sqlite3": "^7.6.9",
1818
"@types/mongoose": "^5.11.97",
19+
"@types/node-cron": "^3.0.11",
1920
"better-sqlite3": "^9.4.3",
2021
"dotenv": "^16.0.3",
2122
"mongoose": "^8.2.3",
@@ -30,6 +31,7 @@
3031
"fast-glob": "^3.3.2",
3132
"guilded.js": "workspace:*",
3233
"lexure": "^0.17.0",
34+
"node-cron": "^3.0.3",
3335
"typed-emitter": "^2.1.0"
3436
},
3537
"contributors": [

0 commit comments

Comments
 (0)