@@ -2,19 +2,21 @@ import { Collection } from "@discordjs/collection";
2
2
import glob from "fast-glob" ;
3
3
import { GilClient } from "../GilClient" ;
4
4
import { Manager } from "./Manager" ;
5
+ import Cron from "node-cron" ;
5
6
6
7
interface TaskOptions {
7
8
// The internal-safe name of the task
8
9
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.
11
11
interval : string ;
12
+ // Whether the task should run immediately upon startup.
13
+ runImmediately ?: boolean ;
12
14
}
13
15
export abstract class Task {
14
16
public constructor (
15
17
public readonly gil : GilClient ,
16
18
public readonly options : TaskOptions ,
17
- ) { }
19
+ ) { }
18
20
19
21
public abstract execute ( ) : unknown | Promise < unknown > ;
20
22
}
@@ -48,6 +50,13 @@ export class TaskManager extends Manager {
48
50
const createdTask : Task = new imported . default ( this . gil ) ;
49
51
this . gil . logger . info ( `Task ${ createdTask . options . name } loaded.` ) ;
50
52
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 ) ) ;
51
60
}
52
61
}
53
62
}
0 commit comments