reowrk again

This commit is contained in:
Neko-Life 2021-07-27 10:09:07 +07:00
parent b228a296e8
commit 68fba58d9c
7 changed files with 57 additions and 39 deletions

21
Main.js
View file

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

View file

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

View file

@ -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();
}
}
module.exports = { createSchedule }

View file

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

View file

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

View file

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

View file

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