diff options
author | jacob janzen <53062115+JacobJanzen@users.noreply.github.com> | 2024-12-11 18:13:16 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-11 18:13:16 -0600 |
commit | 6007a8bbfac47cfe9318541eede484c890489c6e (patch) | |
tree | dd607ea596d64e1921f6b9f9f02c2dce2f0120d9 /message-scheduler.js | |
parent | da5c9f9f0ba5c43873827f0d882e73610571fae0 (diff) |
Complete refactor (#1)
Diffstat (limited to 'message-scheduler.js')
-rw-r--r-- | message-scheduler.js | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/message-scheduler.js b/message-scheduler.js new file mode 100644 index 0000000..12f868b --- /dev/null +++ b/message-scheduler.js @@ -0,0 +1,92 @@ +import "dotenv/config"; +import cron from "node-cron"; +import { v4 as uuidv4 } from "uuid"; +import fs from "node:fs"; + +export class Message { + constructor(message, channel_id, crontab, repeat = true, id = null) { + this.id = id ? id : uuidv4(); + this.repeat = repeat; + + this.channel_id = channel_id; + this.message = message; + this.crontab = crontab; + } +} + +export class MessageSchedule { + constructor(state) { + this.state = state; + this.crons = {}; + + // read db file if there is one and start up all jobs + fs.readFile(state.job_db, "utf8", (err, data) => { + this.jobs = err ? {} : JSON.parse(data); + + Object.keys(this.jobs).forEach((job) => { + // fix incompatibility with older storage version + const repeat = this.jobs[job].repeat + ? this.jobs[job].repeat + : true; + this.jobs[job].id = job; + this.jobs[job].repeat = repeat; + + this.schedule(this.jobs[job]); + }); + }); + } + + save() { + const json = JSON.stringify(this.jobs); + fs.writeFile(this.state.job_db, json, "utf8", (err) => { + if (err) console.log(err); + }); + } + + schedule(message) { + const valid = cron.validate(message.crontab); + + if (valid) { + this.jobs[message.id] = message; + + this.crons[message.id] = cron.schedule( + message.crontab, + () => { + this.state.client.channels.cache + .get(message.channel_id) + .send(message.message); + if (!message.repeat) { + this.unschedule(message.id); + } + }, + { + scheduled: true, + timezone: process.env.TIMEZONE, + }, + ); + + this.save(); + } + + return valid; + } + + unschedule(id) { + var valid = true; + if (id in this.jobs) { + delete this.jobs[id]; + } else { + valid = false; + } + if (id in this.crons) { + this.crons[id].stop(); + delete this.crons[id]; + } else { + valid = false; + } + + this.save(); + + return valid; + } +} |