Skip to content

Commit bfe8236

Browse files
committed
feat: Trigger cron jobs on PS
1 parent ab53f72 commit bfe8236

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/cache/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { JudgementGame } from '@/discord/commands/judgement';
22
import type { Games } from '@/ps/games';
33
import type { BaseGame } from '@/ps/games/game';
4+
import type { PSCronJobManager } from '@/ps/handlers/cron';
45
import type { DiscCommand, PSCommand } from '@/types/chat';
56
import type { PSRoomConfig } from '@/types/ps';
67
import type { Timer } from '@/utils/timer';
@@ -18,6 +19,7 @@ export const PSAltCache: { [key: string]: { from: string; to: string; at: Date }
1819
export const PSSeenCache: { [key: string]: { at: Date; in: string[] } } = {};
1920

2021
export const PSQuoteRoomPrefs: { [key: string]: { room: string; at: Date } } = {};
22+
export const PSCronJobs: { manager: PSCronJobManager | null } = { manager: null };
2123

2224
// Games
2325
export const PSGames: { [key in keyof Games]?: Record<string, BaseGame> } = {};

src/ps/handlers/cron.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { CronJob } from 'cron';
22

3+
import { PSCronJobs } from '@/cache';
4+
35
import type { Client } from 'ps-client';
46

57
// Timezones
@@ -9,16 +11,11 @@ enum TimeZone {
911
GMT = 'Etc/GMT',
1012
}
1113

12-
class PSCronJobManager {
13-
client: Client;
14+
export class PSCronJobManager {
1415
readonly #jobs: Record<string, CronJob> = {};
1516

16-
constructor(client: Client) {
17-
this.client = client;
18-
}
19-
2017
register(id: string, cronTime: string, timeZone: TimeZone, callback: () => void): void {
21-
this.#jobs[id] = CronJob.from({ name: id, cronTime, onTick: callback, timeZone });
18+
this.#jobs[id] = CronJob.from({ name: id, cronTime, start: true, onTick: callback, timeZone });
2219
}
2320
kill(): void {
2421
for (const jobId in this.#jobs) {
@@ -27,8 +24,8 @@ class PSCronJobManager {
2724
}
2825
}
2926

30-
export function startCron(client: Client): PSCronJobManager {
31-
const Jobs = new PSCronJobManager(client);
27+
export function startPSCron(client: Client): PSCronJobManager {
28+
const Jobs = new PSCronJobManager();
3229

3330
// TODO Move back to Hindi and remove 'CRON:'
3431
Jobs.register('hindi-automodchat-enable', '0 0 * * *', TimeZone.IST, () => {
@@ -40,5 +37,9 @@ export function startCron(client: Client): PSCronJobManager {
4037
room?.send('CRON: /automodchat off');
4138
});
4239

40+
// Kill existing cron jobs
41+
PSCronJobs.manager?.kill();
42+
PSCronJobs.manager = Jobs;
43+
4344
return Jobs;
4445
}

src/ps/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Client } from 'ps-client';
33
import { password, rooms, username } from '@/config/ps';
44
import autoResHandler from '@/ps/handlers/autores';
55
import chatHandler from '@/ps/handlers/chat';
6+
import { startPSCron } from '@/ps/handlers/cron';
67
import { transformHTML } from '@/ps/handlers/html';
78
import interfaceHandler from '@/ps/handlers/interface';
89
import { joinHandler, leaveHandler, nickHandler } from '@/ps/handlers/joins';
@@ -24,4 +25,6 @@ PS.on('join', joinHandler);
2425
PS.on('name', nickHandler);
2526
PS.on('leave', leaveHandler);
2627

28+
startPSCron(PS);
29+
2730
export default PS;

0 commit comments

Comments
 (0)