|
| 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