Improve: Scheduler (debug)

This commit is contained in:
Neko-Life 2021-07-28 10:57:25 +07:00
parent 66b87fc3d7
commit 41a0e03528
5 changed files with 69 additions and 23 deletions

View file

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

View file

@ -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;
}

View file

@ -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}\``)

View file

@ -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");
}