scheduler: TESTING

This commit is contained in:
Neko-Life 2021-07-27 14:04:54 +07:00
parent a2d7f8d3f5
commit 19f4be3e9c
4 changed files with 15 additions and 10 deletions

View file

@ -21,7 +21,7 @@ async function createSchedule(client, { guildID, userID, type, until }) {
else if (type === "ban") path = "./unbanSc.js"; else if (type === "ban") path = "./unbanSc.js";
else throw new TypeError("Invalid type: " + type); else throw new TypeError("Invalid type: " + type);
if (typeof until === "string") until = new Date(until); if (typeof until === "string") until = new Date(until);
const NAME = guildID + "/" + userID + "/" + type, const NAME = [guildID, userID, type].join("/"),
SC = { SC = {
name: NAME, name: NAME,
path: join(__dirname, path), path: join(__dirname, path),
@ -47,9 +47,11 @@ async function createSchedule(client, { guildID, userID, type, until }) {
async function init(client) { async function init(client) {
const jobs = await col.find({}).toArray(); const jobs = await col.find({}).toArray();
console.log(jobs);
jobManager = scheduler(client, jobs); jobManager = scheduler(client, jobs);
jobManager.start(); jobManager.start();
jobs.forEach((v) => {
if (v.date.valueOf() < new Date().valueOf()) jobManager.run(v.name);
});
} }
module.exports = { createSchedule, init } module.exports = { createSchedule, init }

View file

@ -22,7 +22,7 @@ function intervalToDuration(interval) {
if (strings.length > 0) { if (strings.length > 0) {
if (strings.length > 1) strings[strings.length - 2] += " and"; if (strings.length > 1) strings[strings.length - 2] += " and";
return { object, strings }; return { object, strings };
} else console.log(interval, object); }
}; };
/** /**

View file

@ -17,7 +17,6 @@ function scheduler(client, jobs = []) {
jobs: jobs, jobs: jobs,
workerMessageHandler: ({ message }) => { workerMessageHandler: ({ message }) => {
const NAME = message; const NAME = message;
console.log(NAME);
if (!NAME[0] || !NAME[1] || !NAME[2]) throw new Error("Value undefined!"); if (!NAME[0] || !NAME[1] || !NAME[2]) throw new Error("Value undefined!");
return execPunishmentSchedule(client, NAME[0], NAME[1], NAME[2]); return execPunishmentSchedule(client, NAME[0], NAME[1], NAME[2]);
}, },

View file

@ -5,6 +5,8 @@ const { Structures, Guild, GuildMember, BanOptions } = require("discord.js"),
{ errLog, defaultEventLogEmbed, defaultDateFormat, trySend } = require("./functions"); { errLog, defaultEventLogEmbed, defaultDateFormat, trySend } = require("./functions");
const { createSchedule } = require("../cmds/moderation/src/createSchedule"); const { createSchedule } = require("../cmds/moderation/src/createSchedule");
const { TimedPunishment } = require("./classes"); const { TimedPunishment } = require("./classes");
const { database } = require("../database/mongo");
const col = database.collection("Schedule");
Structures.extend("Guild", u => { Structures.extend("Guild", u => {
return class Guild extends u { return class Guild extends u {
@ -26,10 +28,9 @@ Structures.extend("Guild", u => {
} }
if (r.timedPunishments) if (r.timedPunishments)
for (const U in r.timedPunishments) { for (const U in r.timedPunishments) {
console.log(r.timedPunishments[U]);
const tr = new TimedPunishment(r.timedPunishments[U]); const tr = new TimedPunishment(r.timedPunishments[U]);
tr.setDataDuration(tr.duration.invoked, tr.duration.until); tr.setDataDuration(tr.duration.invoked, tr.duration.until);
timedPunishments.set(this.id + "/" + tr.userID + "/" + tr.type); timedPunishments.set(tr.userID + "/" + tr.type, tr);
} }
r.infractions = infractions; r.infractions = infractions;
r.timedPunishments = timedPunishments; r.timedPunishments = timedPunishments;
@ -98,7 +99,7 @@ Structures.extend("Guild", u => {
async setTimedPunishment(Punishment) { async setTimedPunishment(Punishment) {
console.log("SET TIMED PUNISHMENT"); console.log("SET TIMED PUNISHMENT");
const ret = this.DB.timedPunishments.set(Punishment.userID + "/" + Punishment.type, Punishment); const ret = this.DB.timedPunishments.set(Punishment.userID + "/" + Punishment.type, Punishment);
console.log(await this.setDb("timedPunishments", this.DB.timedPunishments)); await this.setDb("timedPunishments", this.DB.timedPunishments);
return ret; return ret;
} }
@ -159,7 +160,6 @@ Structures.extend("User", u => {
return database.collection("User").updateOne({ document: this.id }, { $set: { [query]: set }, $setOnInsert: { document: this.id } }, return database.collection("User").updateOne({ document: this.id }, { $set: { [query]: set }, $setOnInsert: { document: this.id } },
{ upsert: true }).then((r, e) => { { upsert: true }).then((r, e) => {
if (e) return errLog(e, null, this.client); if (e) return errLog(e, null, this.client);
console.log(set);
return this.DB[query] = set; return this.DB[query] = set;
}); });
} }
@ -259,6 +259,7 @@ Structures.extend("User", u => {
throw new Error("You can't mute someone with higher position than you <:nekokekLife:852865942530949160>"); throw new Error("You can't mute someone with higher position than you <:nekokekLife:852865942530949160>");
await MEM.unmute(reason); await MEM.unmute(reason);
} }
await col.deleteOne({ document: [guild.id, this.id, "mute"].join("/") }).then(() => console.log("DELETED")).catch(e => errLog(e, null, client));
return guild.removeTimedPunishment(this.id, "mute"); return guild.removeTimedPunishment(this.id, "mute");
} }
@ -315,6 +316,7 @@ Structures.extend("User", u => {
this.createDM().then(r => trySend(this.client, r, emb)); 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));
return guild.removeTimedPunishment(this.id, "ban"); return guild.removeTimedPunishment(this.id, "ban");
} }
@ -375,7 +377,6 @@ Structures.extend("GuildMember", u => {
async dbLoad() { async dbLoad() {
return database.collection("GuildMember").findOne({ document: this.id }).then((r, e) => { return database.collection("GuildMember").findOne({ document: this.id }).then((r, e) => {
if (e) return errLog(e, null, this.client); if (e) return errLog(e, null, this.client);
r = r?.DB;
if (!r) r = {}; if (!r) r = {};
return this.DB = r; return this.DB = r;
}); });
@ -385,7 +386,6 @@ Structures.extend("GuildMember", u => {
return database.collection("GuildMember").updateOne({ document: this.id }, { $set: { [query]: set }, $setOnInsert: { document: this.id } }, return database.collection("GuildMember").updateOne({ document: this.id }, { $set: { [query]: set }, $setOnInsert: { document: this.id } },
{ upsert: true }).then((r, e) => { { upsert: true }).then((r, e) => {
if (e) return errLog(e, null, this.client); if (e) return errLog(e, null, this.client);
console.log(set);
return this.DB[query] = set; return this.DB[query] = set;
}); });
} }
@ -411,11 +411,14 @@ Structures.extend("GuildMember", u => {
this.DB.muted.takenRoles = ROLES; this.DB.muted.takenRoles = ROLES;
} }
this.DB.muted.muteRole = this.guild.DB.settings.mute.role; this.DB.muted.muteRole = this.guild.DB.settings.mute.role;
console.log(this.DB.muted.takenRoles);
try { try {
if (ROLES?.length > 0) await this.roles.remove(ROLES, reason); if (ROLES?.length > 0) await this.roles.remove(ROLES, reason);
await this.roles.add(this.DB.muted.muteRole, reason); await this.roles.add(this.DB.muted.muteRole, reason);
if (!this.DB.muted.takenRoles) this.DB.muted.takenRoles = [];
await this.setDb("muted", this.DB.muted); await this.setDb("muted", this.DB.muted);
console.log(this.DB);
return true; return true;
} catch (e) { } catch (e) {
if (this.DB.muted.takenRoles?.length > 0) await this.roles.add(this.DB.muted.takenRoles, reason).catch(() => { }); if (this.DB.muted.takenRoles?.length > 0) await this.roles.add(this.DB.muted.takenRoles, reason).catch(() => { });
@ -430,6 +433,7 @@ Structures.extend("GuildMember", u => {
async unmute(reason) { async unmute(reason) {
if (!this.DB) await this.dbLoad(); if (!this.DB) await this.dbLoad();
try { try {
console.log(this.DB);
if (this.DB.muted.takenRoles.length > 0) await this.roles.add(this.DB.muted.takenRoles, reason); if (this.DB.muted.takenRoles.length > 0) await this.roles.add(this.DB.muted.takenRoles, reason);
if (this.DB.muted.muteRole) await this.roles.remove(this.DB.muted.muteRole, reason); if (this.DB.muted.muteRole) await this.roles.remove(this.DB.muted.muteRole, reason);
console.log("clear takenRoles UM"); console.log("clear takenRoles UM");