diff --git a/Main.js b/Main.js index fd459e1..f775b56 100644 --- a/Main.js +++ b/Main.js @@ -16,10 +16,9 @@ if (process.argv.includes("-d")) { const sqlite = require('sqlite'); const configFile = require('./config.json'); -const { errLog, trySend, noPerm, getUTCComparison, defaultEventLogEmbed, getChannel, getUser } = require('./resources/functions'); +const { errLog, trySend, defaultEventLogEmbed } = require('./resources/functions'); const { join } = require('path'); const getColor = require("./resources/getColor"); -const { timestampAt } = require("./resources/debug"); const requireAll = require("require-all"); const { chatAnswer } = require("./resources/shaChat"); @@ -199,22 +198,4 @@ process.on("uncaughtException", e => errLog(e, null, client)); process.on("unhandledRejection", e => errLog(e, null, client)); process.on("warning", e => errLog(e, null, client)); -async function execPunishmentSchedule([guildID, userID, type]) { - if (!guildID || !userID || !type) throw new TypeError("Undefined param!"); - let USER = client.users.resolve(userID); - if (!USER) USER = await client.users.fetch(userID); - if (!USER) throw new Error("Unknown user"); - const GUILD = client.guilds.resolve(guildID); - if (!GUILD) throw new Error("Unknown guild"); - if (!GUILD.DB) GUILD.dbLoad(); - const CL = GUILD.member(client.user); - let ret; - if (type === "mute") { - ret = await USER.unmute(GUILD, CL, "Punishment expired"); - } else { - ret = await USER.unban(GUILD, CL, "Punishment expired"); - } - return ret; -} - client.login(configFile.token); \ No newline at end of file diff --git a/cmds/moderation/mute.js b/cmds/moderation/mute.js index b3c2fb4..273b92d 100644 --- a/cmds/moderation/mute.js +++ b/cmds/moderation/mute.js @@ -120,7 +120,6 @@ module.exports = class mute extends commando.Command { try { const RES = await EXEC.mute(msg.guild, { duration: duration, infraction: infractionToDoc.infraction, moderator: msg.member }, reason); if (RES.infraction) infractionN.push(RES.infraction); - console.log(RES); muted.push(EXEC.id); } catch (e) { if (/Missing Permissions|someone with higher position/.test(e.message)) cant.push(EXEC.id); diff --git a/cmds/moderation/src/createSchedule.js b/cmds/moderation/src/createSchedule.js index 2f9712b..9eea2e1 100644 --- a/cmds/moderation/src/createSchedule.js +++ b/cmds/moderation/src/createSchedule.js @@ -4,7 +4,7 @@ const Bree = require("bree"); const { errLog } = require("../../../resources/functions"); const { join } = require("path"), - scheduler = require("../../../resources/scheduler"), + { scheduler } = require("../../../resources/scheduler"), { database } = require("../../../database/mongo"), col = database.collection("Schedule"); @@ -36,10 +36,10 @@ async function createSchedule(client, { guildID, userID, type, until }) { }; try { - await col.updateOne({ document: NAME }, { $set: SC, $setOnInsert: { document: NAME } }, { upsert: true }); - await jobManager.remove(NAME).catch(() => { }) + await jobManager.remove(NAME).catch(() => { }); jobManager.add(SC); jobManager.start(NAME); + return col.updateOne({ document: NAME }, { $set: SC, $setOnInsert: { document: NAME } }, { upsert: true }); } catch (e) { return errLog(e, null, client); } @@ -47,6 +47,9 @@ async function createSchedule(client, { guildID, userID, type, until }) { async function init(client) { const jobs = await col.find({}).toArray(); + console.log(jobs); jobManager = scheduler(client, jobs); jobManager.start(); -} \ No newline at end of file +} + +module.exports = { createSchedule } \ No newline at end of file diff --git a/cmds/moderation/src/unmuteSc.js b/cmds/moderation/src/unmuteSc.js index dcd2b45..71aab0c 100644 --- a/cmds/moderation/src/unmuteSc.js +++ b/cmds/moderation/src/unmuteSc.js @@ -1,4 +1,8 @@ 'use strict'; -const NAME = process.argv[2]?.split(/\//); +const { parentPort } = require("worker_threads"); +if (parentPort) { + const NAME = process.argv[2]?.split(/\//); + parentPort.postMessage(NAME); +}; \ No newline at end of file diff --git a/cmds/moderation/unmute.js b/cmds/moderation/unmute.js index 5ac3dda..27b725a 100644 --- a/cmds/moderation/unmute.js +++ b/cmds/moderation/unmute.js @@ -30,7 +30,6 @@ module.exports = class unmute extends commando.Command { } if (mentions?.length > 0) { const FR = await targetUser(msg, mentions, targetUsers, resultMsg); - console.log(FR); targetUsers = FR.targetUser; resultMsg = FR.resultMsg; } diff --git a/resources/scheduler.js b/resources/scheduler.js index e72ba6a..f168da7 100644 --- a/resources/scheduler.js +++ b/resources/scheduler.js @@ -11,12 +11,41 @@ const { errLog, trySend } = require("./functions"), * @param {object[]} jobs * @returns {Bree} */ -module.exports = (client, jobs = []) => { +function scheduler(client, jobs = []) { return new Bree({ - // logger: new cabin(), + logger: (data) => { + return trySend(client, schedulerLog, data); + }, root: false, jobs: jobs, - workerMessageHandler: (a) => trySend(client, schedulerLog, a), - errorHandler: (e, m) => errLog(e, null, client, false, `\`${m?.threadId}\` \`${m?.name}\``) + workerMessageHandler: ({ message }) => { + const NAME = message; + console.log(NAME); + if (!NAME[0] || !NAME[1] || !NAME[2]) throw new Error("Value undefined!"); + return execPunishmentSchedule(client, NAME[0], NAME[1], NAME[2]); + }, + errorHandler: (e, m) => { + return errLog(e, null, client, false, `\`${m?.threadId}\` \`${m?.name}\``) + } }); -} \ No newline at end of file +} + +async function execPunishmentSchedule(client, guildID, userID, type) { + if (!guildID || !userID || !type || !client) throw new TypeError("Undefined param!"); + const USER = await client.users.fetch(userID); + if (!USER) throw new Error("Unknown user"); + if (!USER.DB) await USER.dbLoad(); + const GUILD = await client.guilds.fetch(guildID); + if (!GUILD) throw new Error("Unknown guild"); + if (!GUILD.DB) await GUILD.dbLoad(); + const CL = GUILD.member(client.user); + let ret; + if (type === "mute") { + ret = await USER.unmute(GUILD, CL, "Punishment expired"); + } else { + ret = await USER.unban(GUILD, CL, "Punishment expired"); + } + return ret; +} + +module.exports = { scheduler, execPunishmentSchedule } \ No newline at end of file diff --git a/resources/structures.js b/resources/structures.js index ade8a33..fef40ca 100644 --- a/resources/structures.js +++ b/resources/structures.js @@ -3,6 +3,7 @@ const { Structures, Guild, GuildMember, BanOptions } = require("discord.js"), { database } = require("../database/mongo"), { errLog, defaultEventLogEmbed, defaultDateFormat, trySend } = require("./functions"); +const { createSchedule } = require("../cmds/moderation/src/createSchedule"); const { TimedPunishment } = require("./classes"); Structures.extend("Guild", u => { @@ -14,7 +15,6 @@ Structures.extend("Guild", u => { async dbLoad() { return database.collection("Guild").findOne({ document: this.id }).then((r, e) => { if (e) return errLog(e, null, this.client); - console.log(r); if (!r) r = {}; if (!r.eventChannels) r.eventChannels = {}; if (!r.settings) r.settings = {}; @@ -22,14 +22,13 @@ Structures.extend("Guild", u => { timedPunishments = new Map(); if (r.infractions) for (const U in r.infractions) { - console.log(r.infractions[U]); infractions.set(U, r.infractions[U]); } if (r.timedPunishments) for (const U in r.timedPunishments) { + console.log(r.timedPunishments[U]); const tr = new TimedPunishment(r.timedPunishments[U]); tr.setDataDuration(tr.duration.invoked, tr.duration.until); - console.log(timedPunishments.set(U, tr)); } r.infractions = infractions; r.timedPunishments = timedPunishments; @@ -62,9 +61,7 @@ Structures.extend("Guild", u => { if (!this.DB) await this.dbLoad(); console.log("SETTING INF"); const ret = this.DB.infractions.set(add.moderator.id + "/" + add.infraction, add); - console.log(ret); await this.setDb("infractions", this.DB.infractions); - console.log(ret); return ret; } catch (e) { } } @@ -100,7 +97,7 @@ Structures.extend("Guild", u => { async setTimedPunishment(Punishment) { console.log("SET TIMED PUNISHMENT"); const ret = this.DB.timedPunishments.set(Punishment.userID + "/" + Punishment.type, Punishment); - await this.setDb("timedPunishments", this.DB.timedPunishments); + console.log(await this.setDb("timedPunishments", this.DB.timedPunishments)); return ret; } @@ -133,7 +130,6 @@ Structures.extend("Guild", u => { async removeTimedPunishment(userID, type) { console.log("REMOVE TIMEDPUNISHMENT"); const ret = this.DB.timedPunishments.delete(userID + "/" + type); - console.log(ret); await this.setDb("timedPunishments", this.DB.timedPunishments); return ret; } @@ -162,6 +158,7 @@ Structures.extend("User", u => { return database.collection("User").updateOne({ document: this.id }, { $set: { [query]: set }, $setOnInsert: { document: this.id } }, { upsert: true }).then((r, e) => { if (e) return errLog(e, null, this.client); + console.log(set); return this.DB[query] = set; }); } @@ -225,6 +222,8 @@ Structures.extend("User", u => { const MC = guild.getTimedPunishment(this.id, "mute"), TP = new TimedPunishment({ userID: this.id, duration: data.duration, infraction: data.infraction, type: "mute" }); + if (data.duration.until) await createSchedule(guild.client, { guildID: guild.id, userID: this.id, type: "mute", until: data.duration.until?.toJSDate() }); + return { set: await guild.setTimedPunishment(TP), existing: MC } } @@ -294,6 +293,9 @@ Structures.extend("User", u => { const MC = guild.getTimedPunishment(this.id, "ban"), TP = new TimedPunishment({ userID: this.id, duration: data.duration, infraction: data.infraction, type: "ban" }); + + if (data.duration.until) await createSchedule(guild.client, { guildID: guild.id, userID: this.id, type: "ban", until: data.duration.until?.toJSDate() }); + return { set: await guild.setTimedPunishment(TP), existing: MC } } @@ -382,6 +384,7 @@ Structures.extend("GuildMember", u => { return database.collection("GuildMember").updateOne({ document: this.id }, { $set: { [query]: set }, $setOnInsert: { document: this.id } }, { upsert: true }).then((r, e) => { if (e) return errLog(e, null, this.client); + console.log(set); return this.DB[query] = set; }); }