Skip to content

Commit ab53f72

Browse files
committed
feat: Add cron jobs
1 parent 774b95f commit ab53f72

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@swc/core": "^1.7.40",
2626
"@tailwindcss/cli": "^4.0.8",
2727
"chokidar": "^3.5.3",
28+
"cron": "^4.3.0",
2829
"discord.js": "^14.11.0",
2930
"dotenv": "^16.3.1",
3031
"express": "^5.0.1",

src/ps/handlers/cron.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { CronJob } from 'cron';
2+
3+
import type { Client } from 'ps-client';
4+
5+
// Timezones
6+
/** @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */
7+
enum TimeZone {
8+
IST = 'Asia/Kolkata',
9+
GMT = 'Etc/GMT',
10+
}
11+
12+
class PSCronJobManager {
13+
client: Client;
14+
readonly #jobs: Record<string, CronJob> = {};
15+
16+
constructor(client: Client) {
17+
this.client = client;
18+
}
19+
20+
register(id: string, cronTime: string, timeZone: TimeZone, callback: () => void): void {
21+
this.#jobs[id] = CronJob.from({ name: id, cronTime, onTick: callback, timeZone });
22+
}
23+
kill(): void {
24+
for (const jobId in this.#jobs) {
25+
this.#jobs[jobId].stop();
26+
}
27+
}
28+
}
29+
30+
export function startCron(client: Client): PSCronJobManager {
31+
const Jobs = new PSCronJobManager(client);
32+
33+
// TODO Move back to Hindi and remove 'CRON:'
34+
Jobs.register('hindi-automodchat-enable', '0 0 * * *', TimeZone.IST, () => {
35+
client.rooms.get('botdevelopment')?.send('CRON: /automodchat 10, +');
36+
});
37+
Jobs.register('hindi-automodchat-disable', '0 7 * * *', TimeZone.IST, () => {
38+
const room = client.rooms.get('botdevelopment');
39+
room?.send('CRON: /modchat ac');
40+
room?.send('CRON: /automodchat off');
41+
});
42+
43+
return Jobs;
44+
}

0 commit comments

Comments
 (0)