From 41a0e035283e195ba433c9a3b99c65a614c9bfe3 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Wed, 28 Jul 2021 10:57:25 +0700 Subject: [PATCH] Improve: Scheduler (debug) --- cmds/moderation/src/createSchedule.js | 71 +++++++++++++++---- .../moderation/src/{unmuteSc.js => execSc.js} | 0 resources/functions.js | 10 ++- resources/scheduler.js | 7 +- resources/structures.js | 4 +- 5 files changed, 69 insertions(+), 23 deletions(-) rename cmds/moderation/src/{unmuteSc.js => execSc.js} (100%) diff --git a/cmds/moderation/src/createSchedule.js b/cmds/moderation/src/createSchedule.js index 4ab46b8..844044a 100644 --- a/cmds/moderation/src/createSchedule.js +++ b/cmds/moderation/src/createSchedule.js @@ -11,20 +11,19 @@ const { join } = require("path"), /** * @type {Bree} */ -let jobManager; +let jobManager, + jobs = []; async function createSchedule(client, { guildID, userID, type, until }) { if (!client || !guildID || !userID || !type || !until) throw new TypeError("Undefined params!"); + const CHK = new Date().valueOf(); + if (until.valueOf() < CHK) throw new RangeError("Schedule should be in the future! Not in the past!"); if (!jobManager) await init(client); - let path; - if (type === "mute") path = "./unmuteSc.js"; - else if (type === "ban") path = "./unbanSc.js"; - else throw new TypeError("Invalid type: " + type); if (typeof until === "string") until = new Date(until); const NAME = [guildID, userID, type].join("/"), SC = { name: NAME, - path: join(__dirname, path), + path: join(__dirname, "./execSc.js"), /** * @type {import("worker_threads").WorkerOptions} @@ -37,21 +36,63 @@ async function createSchedule(client, { guildID, userID, type, until }) { try { await jobManager.remove(NAME).catch(() => { }); - jobManager.add(SC); - jobManager.start(NAME); - return col.updateOne({ document: NAME }, { $set: SC, $setOnInsert: { document: NAME } }, { upsert: true }); + if ((until.valueOf() - CHK) < new Date(0, 0, 0, 24, 0, 0, 0)) { + jobManager.add(SC); + jobManager.start(NAME); + } + await col.updateOne({ document: NAME }, { $set: SC, $setOnInsert: { document: NAME } }, { upsert: true }); + return console.log("SCHEDULE " + NAME + " CREATED"); } catch (e) { return errLog(e, null, client); } } async function init(client) { - const jobs = await col.find({}).toArray(); + await jobLoad(); + jobManager = scheduler(client, jobs); - jobManager.start(); - jobs.forEach((v) => { - if (v.date.valueOf() < new Date().valueOf()) jobManager.run(v.name); - }); + + console.log("SCHEDULER INITIALIZED"); + return jobStart(); } -module.exports = { createSchedule, init } \ No newline at end of file +async function reset() { + await jobManager.stop().catch(console.error); + await jobManager.remove().catch(console.error); + + await jobLoad(); + jobManager.add(jobs); + console.log("SCHEDULER REFRESHED"); + return jobStart(); +} + +async function jobLoad() { + const CHK = new Date().valueOf(); + const CHK2 = new Date(0, 0, 0, 24, 0, 0, 0).valueOf(); + jobs = (await col.find({}).toArray()).filter((v) => (v.date.valueOf() - CHK) < CHK2); + + const rstjb = new Date(0, 0, 0, 23, 30, 0, 0).valueOf(); + const rsttm = { + name: "rsttm", + path: join(__dirname, "./execSc.js"), + worker: { + argv: ["rsttm"] + }, + timeout: rstjb + }; + jobs.push(rsttm); + console.log(jobs.length + " JOBS LOADED"); + return 1; +} + +function jobStart() { + const CHK = new Date().valueOf(); + jobManager.start(); + jobs.forEach((v) => { + if (v.date.valueOf() < CHK) jobManager.run(v.name); + }); + console.log("SCHEDULER STARTED"); + return 1; +} + +module.exports = { createSchedule, init, reset } \ No newline at end of file diff --git a/cmds/moderation/src/unmuteSc.js b/cmds/moderation/src/execSc.js similarity index 100% rename from cmds/moderation/src/unmuteSc.js rename to cmds/moderation/src/execSc.js diff --git a/resources/functions.js b/resources/functions.js index 4d33804..daf5a63 100644 --- a/resources/functions.js +++ b/resources/functions.js @@ -185,7 +185,7 @@ async function trySend(client, msgOrChannel, content, checkAd = true) { if (!client || !msgOrChannel || !content) return; if (typeof msgOrChannel === "string") msgOrChannel = client.channels.cache.get(msgOrChannel); if (!client.user.typingIn(msgOrChannel.channel || msgOrChannel)) { - console.log("TRYSEND: STARTING TYPING"); + console.log("STARTING TYPING"); (msgOrChannel.channel || msgOrChannel).startTyping(); } if (client.owners.includes(msgOrChannel.author)) { @@ -201,14 +201,18 @@ async function trySend(client, msgOrChannel, content, checkAd = true) { } if (!((msgOrChannel instanceof Message) || (msgOrChannel instanceof TextChannel) || (msgOrChannel instanceof DMChannel))) return errLog(e, null, client, false, "[TRYSEND] Invalid {msgOrChannel} type.```js\n" + JSON.stringify(msgOrChannel, (k, v) => v ?? undefined, 2) + "```"); const ret = await (msgOrChannel.channel || msgOrChannel).send(content).catch(/*msgOrChannel.channel ? noPerm(msgOrChannel) :*/ e => errLog(e, msgOrChannel, client)); - console.log("TRYSEND: STOPPING TYPING"); await (msgOrChannel.channel || msgOrChannel).stopTyping(); setTimeout(() => { if (client.user.typingIn(msgOrChannel.channel || msgOrChannel)) { - console.log("TRYSEND: STopping TYPING"); (msgOrChannel.channel || msgOrChannel).stopTyping(); } }, 2000); + setTimeout(() => { + if (client.user.typingIn(msgOrChannel.channel || msgOrChannel)) { + console.log("STOPPING TYPING"); + (msgOrChannel.channel || msgOrChannel).stopTyping(); + } + }, 5000); return ret; } diff --git a/resources/scheduler.js b/resources/scheduler.js index ef589b3..59924c6 100644 --- a/resources/scheduler.js +++ b/resources/scheduler.js @@ -3,6 +3,7 @@ const Bree = require("bree"); const cabin = require("cabin"); const { Client } = require("discord.js"); +const { reset } = require("../cmds/moderation/src/createSchedule"); const { errLog } = require("./functions"); /** @@ -16,9 +17,9 @@ function scheduler(client, jobs = []) { root: false, jobs: jobs, workerMessageHandler: ({ message }) => { - const NAME = message; - if (!NAME[0] || !NAME[1] || !NAME[2]) throw new Error("Value undefined!"); - return execPunishmentSchedule(client, NAME[0], NAME[1], NAME[2]); + if (!message[0] || !message[1] || !message[2]) throw new Error("Value undefined!"); + if (message === "rsttm") return reset(); + return execPunishmentSchedule(client, message[0], message[1], message[2]); }, errorHandler: (e, m) => { return errLog(e, null, client, false, `\`${m?.threadId}\` \`${m?.name}\``) diff --git a/resources/structures.js b/resources/structures.js index 9d58613..5d4f6a0 100644 --- a/resources/structures.js +++ b/resources/structures.js @@ -258,7 +258,7 @@ Structures.extend("User", u => { throw new Error("You can't mute someone with higher position than you <:nekokekLife:852865942530949160>"); await MEM.unmute(reason); } - await col.deleteOne({ document: [guild.id, this.id, "mute"].join("/") }).then(() => console.log("DELETED")).catch(e => errLog(e, null, client)); + await col.deleteOne({ document: [guild.id, this.id, "mute"].join("/") }).then(() => console.log("SCHEDULE " + [guild.id, this.id, "mute"].join("/") + " DELETED")).catch(e => errLog(e, null, client)); return guild.removeTimedPunishment(this.id, "mute"); } @@ -315,7 +315,7 @@ Structures.extend("User", u => { this.createDM().then(r => trySend(this.client, r, emb)); } - await col.deleteOne({ document: [guild.id, this.id, "ban"].join("/") }).then(() => console.log("DELETED")).catch(e => errLog(e, null, client)); + await col.deleteOne({ document: [guild.id, this.id, "ban"].join("/") }).then(() => console.log("SCHEDULE " + [guild.id, this.id, "ban"].join("/") + " DELETED")).catch(e => errLog(e, null, client)); return guild.removeTimedPunishment(this.id, "ban"); }