From e5765e37454b62a08ab4988ac956b62142cdbde8 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sat, 24 Jul 2021 14:05:58 +0700 Subject: [PATCH] refactor DB --- cmds/moderation/eventlog.js | 2 +- cmds/moderation/mute.js | 23 +-- cmds/moderation/src/configureMuteRole.js | 2 +- cmds/moderation/src/createInfraction.js | 2 +- cmds/moderation/src/muteSetting.js | 8 +- cmds/moderation/unmute.js | 22 +-- cmds/profile/servav.js | 2 +- cmds/utility/quoteotd.js | 6 +- cmds/utility/setfootq.js | 2 +- resources/classes.js | 11 +- resources/eventsLogger/guildMemberAdd.js | 4 +- resources/eventsLogger/guildMemberRemove.js | 4 +- resources/eventsLogger/guildMemberUpdate.js | 10 +- resources/eventsLogger/messageDelete.js | 8 +- resources/eventsLogger/messageUpdate.js | 8 +- resources/functions.js | 4 +- resources/structures.js | 161 ++++++++------------ 17 files changed, 128 insertions(+), 151 deletions(-) diff --git a/cmds/moderation/eventlog.js b/cmds/moderation/eventlog.js index 07cfc90..fd5551f 100644 --- a/cmds/moderation/eventlog.js +++ b/cmds/moderation/eventlog.js @@ -18,7 +18,7 @@ module.exports = class eventlog extends commando.Command { async run(msg, arg) { if (!msg.guild.DB) await msg.guild.dbLoad(); const set = parseDoubleDash(arg); - let eventChannels = msg.guild.DB.settings.eventChannels; + let eventChannels = msg.guild.DB.eventChannels; if (!set || set.length < 2 && set[0].length === 0) return trySend(this.client, msg, await resultEmbed(this)); let report = "", joinlog, leavelog, channellog, banlog, unbanlog, mesEdlog = { channel: undefined, ignore: [] }, invitelog, rolelog, guildlog, membernicklog, emotelog, memberroleslog, remove = false, [setMesEdIgnore, setMesDelIgnore] = [false, false], mesDellog = { channel: undefined, ignore: [] }; diff --git a/cmds/moderation/mute.js b/cmds/moderation/mute.js index b130442..ccd13f2 100644 --- a/cmds/moderation/mute.js +++ b/cmds/moderation/mute.js @@ -73,12 +73,12 @@ module.exports = class mute extends commando.Command { async run(msg, arg) { msg.channel.startTyping(); if (!msg.guild.DB) await msg.guild.dbLoad(); - const MOD = msg.guild.DB.moderation.settings, + const MOD = msg.guild.DB.settings, MUTE = MOD.mute || {}, args = parseDoubleDash(arg), mentions = parseComa(args?.shift()); - if (!MOD.mute) msg.guild.DB.moderation.settings.mute = {}; + if (!MOD.mute) msg.guild.DB.settings.mute = {}; let reason = "No reason provided", duration = {}, resultMsg = "", targetUsers = []; if (args?.[1]) { @@ -120,25 +120,28 @@ 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); else if (/already muted/.test(e.message)) already.push(EXEC.id); else trySend(msg.client, msg, e.message); continue; } - const emb = defaultEventLogEmbed(msg.guild); - emb.setTitle("You have been muted") - .setDescription("**Reason**\n" + reason) - .addField("At", defaultDateFormat(duration.invoked), true) - .addField("For", duration.duration?.strings.join(" ") || "Indefinite", true) - .addField("Until", duration.until ? defaultDateFormat(duration.until) : "Never", true); - EXEC.createDM().then(r => trySend(msg.client, r, emb)); + if (!EXEC.bot) { + const emb = defaultEventLogEmbed(msg.guild); + emb.setTitle("You have been muted") + .setDescription("**Reason**\n" + reason) + .addField("At", defaultDateFormat(duration.invoked), true) + .addField("For", duration.duration?.strings.join(" ") || "Indefinite", true) + .addField("Until", duration.until ? defaultDateFormat(duration.until) : "Never", true); + EXEC.createDM().then(r => trySend(msg.client, r, emb)); + } } infractionToDoc.executed = muted; infractionToDoc.aborted = already; infractionToDoc.failed = cant; - if (muted.length > 0) msg.guild.addInfraction(infractionToDoc); + if (muted.length > 0) await msg.guild.addInfraction(infractionToDoc); const NAME = msg.guild.id + "/" + infractionToDoc.infraction, newUnmuteSchedule = { diff --git a/cmds/moderation/src/configureMuteRole.js b/cmds/moderation/src/configureMuteRole.js index 0c7fe6d..a26ed83 100644 --- a/cmds/moderation/src/configureMuteRole.js +++ b/cmds/moderation/src/configureMuteRole.js @@ -31,7 +31,7 @@ async function detonate(msg, data) { const pleaseWait = await trySend(msg.client, msg, `Setting up for ${map.length} channel${map.length < 2 ? "" : "s"}... This message will be edited when done.`); data.permissions = 0; const ROLE = await msg.guild.roles.create({ data: data, reason: "Create Mute Role" }).catch(() => { }); - msg.guild.DB.moderation.settings.mute.role = ROLE.id; + msg.guild.DB.settings.mute.role = ROLE.id; let cant = []; if (ROLE) { for (const U of map) { diff --git a/cmds/moderation/src/createInfraction.js b/cmds/moderation/src/createInfraction.js index 976d883..ae66253 100644 --- a/cmds/moderation/src/createInfraction.js +++ b/cmds/moderation/src/createInfraction.js @@ -9,7 +9,7 @@ const { Message, User } = require("discord.js"); * @param {string} reason */ module.exports = (msg, targetUsers, punishment, reason) => { - let infractionCase = msg.guild.DB.moderation.infractions?.length; + let infractionCase = msg.guild.DB.infractions.size; return { infraction: infractionCase ? infractionCase++ : 1, by: targetUsers, diff --git a/cmds/moderation/src/muteSetting.js b/cmds/moderation/src/muteSetting.js index b6593b5..865af07 100644 --- a/cmds/moderation/src/muteSetting.js +++ b/cmds/moderation/src/muteSetting.js @@ -8,7 +8,7 @@ module.exports = (msg, arg) => { if (!msg.member.isAdmin) return trySend(msg.client, msg, msg.author + " you're not an Administrator <:nekohmLife:846371737644957786>"); const args = parseDash(arg); let setEmb = defaultImageEmbed(msg, null, "Mute Configuration"), - MUTE = msg.guild.DB.moderation.settings.mute || {}, + MUTE = msg.guild.DB.settings.mute || {}, duration, role, resultMsg = ""; @@ -22,18 +22,18 @@ module.exports = (msg, arg) => { continue; } if (key?.length > 0) role = getRole(msg.guild, key)?.id; - if (role === undefined) resultMsg += `No role found for: **${ARG}**\n`; else msg.guild.DB.moderation.settings.mute.role = role; + if (role === undefined) resultMsg += `No role found for: **${ARG}**\n`; else msg.guild.DB.settings.mute.role = role; } if (ARG.startsWith("d ")) { const D = ARG.slice(2).trim(); console.log(D); if (/^[\-\+]?\d{1,16}(?![^ymwdhs])[ymwdhs]?o?/i.test(D)) { duration = fn.duration(msg.createdAt, D); - msg.guild.DB.moderation.settings.mute.defaultDuration = duration; + msg.guild.DB.settings.mute.defaultDuration = duration; } else resultMsg += "Valid duration format: `number_[y|mo|w|d|h|m|s]`. Example: `69y420w5m72s3mo`"; } } - MUTE = msg.guild.DB.moderation.settings.mute; + MUTE = msg.guild.DB.settings.mute; msg.guild.setDb(msg.guild.DB); } diff --git a/cmds/moderation/unmute.js b/cmds/moderation/unmute.js index 8f4184c..697fdb5 100644 --- a/cmds/moderation/unmute.js +++ b/cmds/moderation/unmute.js @@ -19,6 +19,7 @@ module.exports = class unmute extends commando.Command { } async run(msg, arg) { + if (!msg.guild.DB) await msg.guild.dbLoad(); msg.channel.startTyping(); if (!arg) return trySend(msg.client, msg, this.details); const args = parseDoubleDash(arg), @@ -38,23 +39,24 @@ module.exports = class unmute extends commando.Command { for (const USER of targetUsers) { if (!USER.DB) await USER.dbLoad(); - const L = USER.getMutedIn(msg.guild.id); - if (!L.data) { notMuted.push(USER.id); continue } else { - await USER.unmute(msg.guild, msg.member, reason) - .then(() => { - success.push(USER.id); + await USER.unmute(msg.guild, msg.member, reason) + .then(() => { + success.push(USER.id); + if (!USER.bot) { const emb = defaultEventLogEmbed(msg.guild); emb.setTitle("You have been unmuted") .setDescription("**Reason**\n" + reason); USER.createDM().then(r => trySend(msg.client, r, emb)); - }) - .catch((e) => { - console.log(e); cant.push(USER.id) - }); - } + } + }) + .catch((e) => { + console.log(e); + if (/isn't muted in/.test(e.message)) return notMuted.push(USER.id); + cant.push(USER.id) + }); } let emb = defaultImageEmbed(msg, null, "Unmute"); diff --git a/cmds/profile/servav.js b/cmds/profile/servav.js index ce8de37..95de0f4 100644 --- a/cmds/profile/servav.js +++ b/cmds/profile/servav.js @@ -16,7 +16,7 @@ module.exports = class servav extends commando.Command { description: "Show server avatar." }); } - run(msg, arg) { + async run(msg, arg) { if (!msg.author.DB) await msg.author.dbLoad(); if (msg.guild && !msg.guild.DB) await msg.guild.dbLoad(); const server_ID = arg.split(/ +/)[0]; diff --git a/cmds/utility/quoteotd.js b/cmds/utility/quoteotd.js index eff129f..6be8ae3 100644 --- a/cmds/utility/quoteotd.js +++ b/cmds/utility/quoteotd.js @@ -29,13 +29,13 @@ module.exports = class quoteotd extends commando.Command { if (startW.startsWith('c ')) { data = arr.slice('c '.length).trim(); const CHAN = getChannel(msg, data, ["category", "voice"]); - msg.guild.DB.settings.quoteOTD.channel = CHAN.id; + msg.guild.DB.quoteOTD.channel = CHAN.id; result += `Channel set: **${CHAN.name}**\n`; continue; } if (startW.startsWith('t ')) { data = arr.slice('t '.length).trim(); - msg.guild.DB.settings.quoteOTD.footerText = data; + msg.guild.DB.quoteOTD.footerText = data; result += `Footer text set: \`${data}\`\n`; continue; } @@ -45,7 +45,7 @@ module.exports = class quoteotd extends commando.Command { result += 'Invalid icon URL provided!\n'; continue; } else { - msg.guild.DB.settings.quoteOTD.footerIcon = data; + msg.guild.DB.quoteOTD.footerIcon = data; result += `Footer icon set!\n`; continue; } diff --git a/cmds/utility/setfootq.js b/cmds/utility/setfootq.js index cb1a88a..72863c6 100644 --- a/cmds/utility/setfootq.js +++ b/cmds/utility/setfootq.js @@ -17,7 +17,7 @@ module.exports = class setfootq extends commando.Command { } async run(msg, args) { try { - let oldQ = msg.guild ? msg.guild.DB.settings.defaultEmbed : msg.author.DB.defaultEmbed; + let oldQ = msg.guild ? msg.guild.DB.defaultEmbed : msg.author.DB.defaultEmbed; if (!oldQ) oldQ = {}; const newQ = oldQ?.footerQuote; oldQ.footerQuote = args.trim(); diff --git a/resources/classes.js b/resources/classes.js index 21e4f6e..7eb2991 100644 --- a/resources/classes.js +++ b/resources/classes.js @@ -9,10 +9,13 @@ class TimedPunishment { * @param {{userID: string, duration: { invoked: DateTime, interval: Interval, until: DateTime, duration: { "object": DurationObject, strings: string[] } }, infraction: number, type: "ban" | "mute"}} data */ constructor(data = {}) { - this.userID = data.userID; - this.duration = data.duration || {}; - this.infraction = data.infraction; - this.type = data.type; + this.userID = data?.userID; + /** + * @type {{ invoked: DateTime, interval: Interval, until: DateTime, duration: { "object": DurationObject, strings: string[] } }} + */ + this.duration = data?.duration || {}; + this.infraction = data?.infraction; + this.type = data?.type; } setUserID(ID) { diff --git a/resources/eventsLogger/guildMemberAdd.js b/resources/eventsLogger/guildMemberAdd.js index 45447e3..869670a 100644 --- a/resources/eventsLogger/guildMemberAdd.js +++ b/resources/eventsLogger/guildMemberAdd.js @@ -12,8 +12,8 @@ const { GuildMember } = require("discord.js"), * @returns */ module.exports = (member) => { - if (member.guild.DB.settings.eventChannels?.join) { - const log = getChannel(member, member.guild.DB.settings.eventChannels.join); + if (member.guild.DB.eventChannels?.join) { + const log = getChannel(member, member.guild.DB.eventChannels.join); if (!log) return; const emb = defaultEventLogEmbed(member.guild); emb diff --git a/resources/eventsLogger/guildMemberRemove.js b/resources/eventsLogger/guildMemberRemove.js index 8c9fd58..be4e557 100644 --- a/resources/eventsLogger/guildMemberRemove.js +++ b/resources/eventsLogger/guildMemberRemove.js @@ -12,8 +12,8 @@ Settings.defaultZone = "utc"; * @returns */ module.exports = (member) => { - if (member.guild.DB.settings.eventChannels?.leave) { - const log = getChannel(member, member.guild.DB.settings.eventChannels.leave); + if (member.guild.DB.eventChannels?.leave) { + const log = getChannel(member, member.guild.DB.eventChannels.leave); if (!log) return; const emb = defaultEventLogEmbed(member.guild), RO = member.roles.cache.sort((a, b) => b.position - a.position).map(r => r.id).slice(0, -1), diff --git a/resources/eventsLogger/guildMemberUpdate.js b/resources/eventsLogger/guildMemberUpdate.js index 3fcfa56..b3c6478 100644 --- a/resources/eventsLogger/guildMemberUpdate.js +++ b/resources/eventsLogger/guildMemberUpdate.js @@ -10,7 +10,7 @@ const getColor = require("../getColor"); * @returns */ module.exports = (memberold, membernew) => { - if (!membernew.guild.DB.settings.eventChannels?.memberRole && !membernew.guild.DB.settings.eventChannels?.member) { + if (!membernew.guild.DB.eventChannels?.memberRole && !membernew.guild.DB.eventChannels?.member) { if (membernew.user.DB.cachedAvatarURL != membernew.user.displayAvatarURL({ format: "png", size: 4096, dynamic: true })) { membernew.user.DB.cachedAvatarURL = membernew.user.displayAvatarURL({ format: "png", size: 4096, dynamic: true }); }; @@ -22,8 +22,8 @@ module.exports = (memberold, membernew) => { emb.setTitle("Profile `" + memberold.user.tag + "` updated") .setColor(getColor("blue")); if (oldAV) thumbMes += "This embed's thumbnail is the user's old avatar.\n"; - if (membernew.guild.DB.settings.eventChannels?.memberRole) { - log = getChannel(membernew, membernew.guild.DB.settings.eventChannels.memberRole); + if (membernew.guild.DB.eventChannels?.memberRole) { + log = getChannel(membernew, membernew.guild.DB.eventChannels.memberRole); if (membernew.roles.cache.size > memberold.roles.cache.size) { emb.addField("Role added", ("<@&" + membernew.roles.cache.difference(memberold.roles.cache).sort((a, b) => b.position - a.position).map(r => r.id).join(">, <@&") + ">").slice(0, 2048)) .setDescription("**Old roles**\n" + (memberold.roles.cache.size > 1 ? "<@&" + memberold.roles.cache.sort((a, b) => b.position - a.position).map(r => r.id).slice(0, -1).join(">, <@&") + ">" : "`[NONE]`")); @@ -33,8 +33,8 @@ module.exports = (memberold, membernew) => { .setDescription("**Current roles**\n" + (membernew.roles.cache.size > 1 ? "<@&" + membernew.roles.cache.sort((a, b) => b.position - a.position).map(r => r.id).slice(0, -1).join(">, <@&") + ">" : "`[NONE]`")); } } - if (membernew.guild.DB.settings.eventChannels?.member && membernew.roles.cache.size === memberold.roles.cache.size) { - log = getChannel(membernew, membernew.guild.DB.settings.eventChannels.member); + if (membernew.guild.DB.eventChannels?.member && membernew.roles.cache.size === memberold.roles.cache.size) { + log = getChannel(membernew, membernew.guild.DB.eventChannels.member); if (membernew.displayName !== memberold.displayName) { emb.addField("Nickname", "Changed from `" + memberold.displayName + "` to `" + membernew.displayName + "`"); } diff --git a/resources/eventsLogger/messageDelete.js b/resources/eventsLogger/messageDelete.js index 6cbaa6d..8add7d0 100644 --- a/resources/eventsLogger/messageDelete.js +++ b/resources/eventsLogger/messageDelete.js @@ -11,11 +11,11 @@ const getColor = require("../getColor"); */ module.exports = async (msg) => { if (msg.partial) return; - const ignored = msg.guild.DB.settings.eventChannels.mesDel?.ignore?.includes(msg.channel.id) ?? false; + const ignored = msg.guild.DB.eventChannels.mesDel?.ignore?.includes(msg.channel.id) ?? false; let check = false; - if (msg.channel.id === msg.guild.DB.settings.eventChannels.mesDel?.channel && msg.author ? msg.author !== msg.client.user : false && ignored === false) check = true; - if (msg.guild.DB.settings.eventChannels.mesDel?.channel !== msg.channel.id && ignored === false || check) { - const log = getChannel(msg, msg.guild.DB.settings.eventChannels.mesDel?.channel); + if (msg.channel.id === msg.guild.DB.eventChannels.mesDel?.channel && msg.author ? msg.author !== msg.client.user : false && ignored === false) check = true; + if (msg.guild.DB.eventChannels.mesDel?.channel !== msg.channel.id && ignored === false || check) { + const log = getChannel(msg, msg.guild.DB.eventChannels.mesDel?.channel); if (!log || !msg.author) return; const emb = defaultEventLogEmbed(msg.guild); emb.setColor(getColor("yellow")) diff --git a/resources/eventsLogger/messageUpdate.js b/resources/eventsLogger/messageUpdate.js index 5b1c2ad..1613220 100644 --- a/resources/eventsLogger/messageUpdate.js +++ b/resources/eventsLogger/messageUpdate.js @@ -14,11 +14,11 @@ module.exports = async (msgold, msgnew) => { if (msgnew.partial) msgnew = await msgnew.fetch(); if (msgnew.partial) return; if (msgnew.content === msgold.content) return; - const ignored = msgnew.guild.DB.settings.eventChannels.mesEd?.ignore?.includes(msgnew.channel.id) || false; + const ignored = msgnew.guild.DB.eventChannels.mesEd?.ignore?.includes(msgnew.channel.id) || false; let check = false; - if (msgnew.channel.id === msgnew.guild.DB.settings.eventChannels.mesEd?.channel && msgnew.author ? msgnew.author !== msgnew.client.user : false && ignored === false) check = true; - if (msgnew.guild.DB.settings.eventChannels.mesEd?.channel !== msgnew.channel.id && ignored === false || check) { - const log = getChannel(msgnew, msgnew.guild.DB.settings.eventChannels.mesEd?.channel); + if (msgnew.channel.id === msgnew.guild.DB.eventChannels.mesEd?.channel && msgnew.author ? msgnew.author !== msgnew.client.user : false && ignored === false) check = true; + if (msgnew.guild.DB.eventChannels.mesEd?.channel !== msgnew.channel.id && ignored === false || check) { + const log = getChannel(msgnew, msgnew.guild.DB.eventChannels.mesEd?.channel); if (!log || !msgnew.author) return; const emb = defaultEventLogEmbed(msgnew.guild); emb diff --git a/resources/functions.js b/resources/functions.js index f5624ae..2f7501b 100644 --- a/resources/functions.js +++ b/resources/functions.js @@ -439,7 +439,7 @@ function defaultEventLogEmbed(guild) { .setColor(getColor(C.displayColor)) .setAuthor(guild.name) .setFooter((guild.DB?.settings?.defaultEmbed?.footerQuote ? - guild.DB.settings.defaultEmbed.footerQuote : ""), guild.iconURL({ format: "png", size: 128, dynamic: true })) + guild.DB.defaultEmbed.footerQuote : ""), guild.iconURL({ format: "png", size: 128, dynamic: true })) .setTimestamp(new Date()); } @@ -511,7 +511,7 @@ async function getUser(msg, key, inGuild = false) { if (/^\d{17,19}$/.test(use)) { const ret = msg.client.users.cache.get(use); if (ret) return ret; else return msg.client.users.fetch(use); - } else if (inGuild) return getMember(msg.guild, use)?.[0].user; + } else if (inGuild) return getMember(msg.guild, use)[0]?.user; } function getRole(guild, key) { diff --git a/resources/structures.js b/resources/structures.js index a3a72f4..2dbba59 100644 --- a/resources/structures.js +++ b/resources/structures.js @@ -14,46 +14,33 @@ 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); - r = r?.DB; if (!r) r = {}; + if (!r.eventChannels) r.eventChannels = {}; if (!r.settings) r.settings = {}; - if (!r.moderation) r.moderation = {}; - if (!r.settings.eventChannels) r.settings.eventChannels = {}; - if (!r.moderation.settings) r.moderation.settings = {}; let infractions = new Map(), timedPunishments = new Map(); - if (r.moderation.infractions) - for (const U in r.moderation.infractions) - infractions.set(U, r.moderation.infractions[U]); - if (r.moderation.timedPunishments) - for (const U in r.moderation.timedPunishments) - timedPunishments.set(U, new TimedPunishment(r.moderation.timedPunishments[U])); - r.moderation.infractions = infractions; - r.moderation.timedPunishments = timedPunishments; + if (r.infractions) + for (const U in r.infractions) + infractions.set(U, r.infractions[U]); + if (r.timedPunishments) + for (const U in r.timedPunishments) { + const tr = new TimedPunishment(r.timedPunishments[U]); + console.log(timedPunishments.set(U, tr)); + } + r.infractions = infractions; + r.timedPunishments = timedPunishments; return this.DB = r; }); } - async setDb(Db, empty = false) { - if (typeof Db !== "object") throw new TypeError("Expected 'object'; Got '" + typeof Db + "'"); - if (Db === {} && !empty) throw new TypeError("Empty!"); - return database.collection("Guild").updateOne({ document: this.id }, { $set: { DB: Db }, $setOnInsert: { document: this.id } }, + async setDb(query, set) { + return database.collection("Guild").updateOne({ document: this.id }, { $set: { [query]: set }, $setOnInsert: { document: this.id } }, { upsert: true }).then((r, e) => { if (e) return errLog(e, null, this.client); - return this.DB = Db; + return this.DB[query] = set; }); } - /** - * @param {object} data - Data to set - * @returns - */ - async refreshDb(data) { - if (!this.DB) await this.dbLoad(); - if (data) for (const D in data) if (this.DB[D]) this.DB[D] = data[D]; - return this.setDb(this.DB); - } - /** * Get user infractions * @param {string} userID - User ID @@ -61,7 +48,7 @@ Structures.extend("Guild", u => { */ async getInfractions(userID) { let ret = [] - for (const [k, v] of this.DB.moderation.infractions) + for (const [k, v] of this.DB.infractions) if (v.by.map(r => r.id).includes(userID)) ret.push(v); return ret; } @@ -69,42 +56,46 @@ Structures.extend("Guild", u => { async addInfraction(add) { try { if (!this.DB) await this.dbLoad(); - this.DB.moderation.infractions.set(add.infraction, add); - return this.setDb(this.DB); + console.log("SETTING INF"); + const ret = this.DB.infractions.set(add.infraction, add); + console.log(ret); + await this.setDb("infractions", this.DB.infractions); + return ret; } catch (e) { } } async setQuoteOTD(set) { if (!this.DB) await this.dbLoad(); - this.DB.settings.quoteOTD = set; - return this.setDb(this.DB); + this.DB.quoteOTD = set; + return this.setDb("quoteOTD", this.DB.quoteOTD); } async setEventChannels(set) { if (!this.DB) await this.dbLoad(); - this.DB.settings.eventChannels = set; - return this.setDb(this.DB); + this.DB.eventChannels = set; + return this.setDb("eventChannels", this.DB.eventChannels); } async setDefaultEmbed(set) { if (!this.DB) await this.dbLoad(); - this.DB.settings.defaultEmbed = set; - return this.setDb(this.DB); + this.DB.defaultEmbed = set; + return this.setDb("defaultEmbed", this.DB.defaultEmbed); } async setModerationSettings(set) { if (!this.DB) await this.dbLoad(); - this.DB.moderation.settings = set; - return this.setDb(this.DB); + this.DB.settings = set; + return this.setDb("settings", this.DB.settings); } /** * @param {TimedPunishment} Punishment * @returns {Map} */ - setTimedPunishment(Punishment) { - const ret = this.DB.moderation.timedPunishments.set(Punishment.userID + "/" + Punishment.type, Punishment); - this.setDb(this.DB); + 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); return ret; } @@ -114,7 +105,8 @@ Structures.extend("Guild", u => { * @returns */ getTimedPunishment(userID, type) { - return this.DB.moderation.timedPunishments.get(userID + "/" + type); + console.log("GET TIMEDPUNISHMENT"); + return this.DB.timedPunishments.get(userID + "/" + type); } /** @@ -123,7 +115,7 @@ Structures.extend("Guild", u => { */ searchTimedPunishment(userID) { let ret = []; - for (const [k, v] of this.DB.moderation.timedPunishments) if (v.userID === userID) ret.push(v); + for (const [k, v] of this.DB.timedPunishments) if (v.userID === userID) ret.push(v); return ret; } @@ -133,9 +125,11 @@ Structures.extend("Guild", u => { * @param {"mute"|"ban"} type * @returns {boolean} */ - removeTimedPunishment(userID, type) { - const ret = this.DB.moderation.timedPunishments.delete(userID + "/" + type) - this.setDb(this.DB); + 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; } } @@ -151,7 +145,6 @@ Structures.extend("User", u => { async dbLoad() { return database.collection("User").findOne({ document: this.id }).then((r, e) => { if (e) return errLog(e, null, this.client); - r = r?.DB; if (!r) r = {}; if (!r.F) r.F = "<:pepewhysobLife:853237646666891274>"; if (!r.cachedAvatarURL) r.cachedAvatarURL = this.displayAvatarURL({ format: "png", size: 4096, dynamic: true }); @@ -160,48 +153,36 @@ Structures.extend("User", u => { }); } - async setDb(Db, empty = false) { - if (typeof Db !== "object") throw new TypeError("Expected 'object'; Got '" + typeof Db + "'"); - if (Db === {} && !empty) throw new TypeError("Empty!"); - return database.collection("User").updateOne({ document: this.id }, { $set: { DB: Db }, $setOnInsert: { document: this.id } }, + async setDb(query, set) { + 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); - return this.DB = Db; + return this.DB[query] = set; }); } - /** - * @param {object} data - Data to set - * @returns - */ - async refreshDb(data) { - if (!this.DB) await this.dbLoad(); - if (data) for (const D in data) if (this.DB[D]) this.DB[D] = data[D]; - return this.setDb(this.DB); - } - async setF(string) { if (!this.DB) await this.dbLoad(); this.DB.F = string; - return this.setDb(this.DB); + return this.setDb("F", this.DB.F); } async setInteractions(count) { if (!this.DB) await this.dbLoad(); this.DB.interactions = count; - return this.setDb(this.DB); + return this.setDb("interactions", this.DB.interactions); } async setDescription(set) { if (!this.DB) await this.dbLoad(); this.DB.description = set; - return this.setDb(this.DB); + return this.setDb("description", this.DB.description); } async setDefaultEmbed(set) { if (!this.DB) await this.dbLoad(); this.DB.defaultEmbed = set; - return this.setDb(this.DB); + return this.setDb("defaultEmbed", this.DB.defaultEmbed); } /** @@ -220,13 +201,13 @@ 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" }); - return { set: guild.setTimedPunishment(TP), existing: MC } + return { set: await guild.setTimedPunishment(TP), existing: MC } } async unmute(guild, moderator, reason) { if (!guild || !(guild instanceof Guild)) throw new TypeError("Guild is " + typeof guild); if (!guild.DB) await guild.dbLoad(); - const MC = suild.getTimedPunishment(this.id, "mute"); + const MC = guild.getTimedPunishment(this.id, "mute"); if (!MC) throw new Error(this.tag + " isn't muted in " + guild.name); const MEM = guild.member(this); if (MEM) { @@ -306,26 +287,14 @@ Structures.extend("GuildMember", u => { }); } - async setDb(Db, empty = false) { - if (typeof Db !== "object") throw new TypeError("Expected 'object'; Got '" + typeof Db + "'"); - if (Db === {} && !empty) throw new TypeError("Empty!"); - return database.collection("GuildMember").updateOne({ document: this.id }, { $set: { DB: Db }, $setOnInsert: { document: this.id } }, + async setDb(query, set) { + 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); - return this.DB = Db; + return this.DB[query] = set; }); } - /** - * @param {object} data - Data to set - * @returns - */ - async refreshDb(data) { - if (!this.DB) await this.dbLoad(); - if (data) for (const D in data) if (this.DB[D]) this.DB[D] = data[D]; - return this.setDb(this.DB); - } - async infractions() { return this.guild.getInfractions(this.id); } @@ -343,21 +312,21 @@ Structures.extend("GuildMember", u => { const ROLES = this.roles.cache.filter((r) => !r.managed).map(r => r.id); if (data.saveTakenRoles && ROLES?.length > 0) { console.log("populating takenRoles M"); - this.DB.takenRoles = ROLES; + this.DB.muted.takenRoles = ROLES; } - this.DB.muteRole = this.guild.DB.moderation.settings.mute.role; + this.DB.muted.muteRole = this.guild.DB.mute.role; try { if (ROLES?.length > 0) await this.roles.remove(ROLES, reason); - await this.roles.add(this.DB.muteRole, reason); - this.setDb(this.DB); + await this.roles.add(this.DB.muted.muteRole, reason); + await this.setDb("muted", this.DB.muted); return true; } catch (e) { - if (this.DB.takenRoles?.length > 0) await this.roles.add(this.DB.takenRoles, reason).catch(() => { }); - if (this.DB.muteRole) await this.roles.remove(this.DB.muteRole, reason).catch(() => { }); + if (this.DB.muted.takenRoles?.length > 0) await this.roles.add(this.DB.muted.takenRoles, reason).catch(() => { }); + if (this.DB.muted.muteRole) await this.roles.remove(this.DB.muted.muteRole, reason).catch(() => { }); console.log("clear takenRoles M"); - this.DB.takenRoles = []; - this.DB.muteRole = undefined; + this.DB.muted.takenRoles = []; + this.DB.muted.muteRole = undefined; throw e; } } @@ -365,12 +334,12 @@ Structures.extend("GuildMember", u => { async unmute(reason) { if (!this.DB) await this.dbLoad(); try { - if (this.DB.takenRoles.length > 0) await this.roles.add(this.DB.takenRoles, reason); - if (this.DB.muteRole) await this.roles.remove(this.DB.muteRole, 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); console.log("clear takenRoles UM"); - this.DB.takenRoles = []; - this.DB.muteRole = undefined; - this.setDb(this.DB); + this.DB.muted.takenRoles = []; + this.DB.muted.muteRole = undefined; + await this.setDb("muted", this.DB.muted); return true; } catch (e) { throw e;