From 702c7198330409e4a55a5bfac8ae590b727c741f Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Wed, 14 Jul 2021 13:05:48 +0700 Subject: [PATCH] First step --- cmds/moderation/mute.js | 36 ++++------ resources/structures.js | 152 +++++++++++++++++++++------------------- 2 files changed, 92 insertions(+), 96 deletions(-) diff --git a/cmds/moderation/mute.js b/cmds/moderation/mute.js index e5eb08d..97bc2fd 100644 --- a/cmds/moderation/mute.js +++ b/cmds/moderation/mute.js @@ -57,8 +57,8 @@ module.exports = class mute extends commando.Command { */ async run(msg, arg) { if (!msg.guild.dbLoaded) msg.guild.dbLoad(); - const muteSettingsDoc = msg.guild.moderation.mute, - defaultDurationDoc = muteSettingsDoc.defaultDuration, + const MOD = msg.guild.moderation, + MUTE = MOD.mute || { defaultDuration: {} }, args = parseDoubleDash(arg), mentions = parseComa(args.shift()), durationRegExp = /\d+(?![^ymwdhs])[ymwdhs]?o?/gi, @@ -71,14 +71,7 @@ module.exports = class mute extends commando.Command { minute: invokedAt.getMinutes(), second: invokedAt.getSeconds() }; - let theSettingUp = { - role: undefined, - defaultDuration: { - date: undefined, - string: undefined - } - }, - durationHasSet = false, + let durationHasSet = false, settingUp = false, settingRole = false, settingRoleHasSet = false, @@ -141,7 +134,7 @@ module.exports = class mute extends commando.Command { const key = cleanMentionID(argument); let role = getRole(msg.guild, key)?.id; if (role || /^none$/i.test(key)) { - theSettingUp.role = role; + MUTE.role = role; } else { resultMsg += `No role found for: **${argument}**\n`; } @@ -149,15 +142,15 @@ module.exports = class mute extends commando.Command { } } } - const roleConfCheck = msg.guild.roles.cache.get(muteSettingsDoc?.role); + const roleConfCheck = msg.guild.roles.cache.get(MUTE?.role); if (!roleConfCheck && !settingUp) { resultMsg += `No mute role configured! Run \`${msg.guild.commandPrefix}${this.name} --settings <--role --> [--duration --\` to set it up.`; } let untilDate = new Date(String(duration.year), String(duration.month), String(duration.date), String(duration.hour), String(duration.minute), String(duration.second)); if (untilDate.toString() === "Invalid Date") untilDate = "Indefinite"; - if (untilDate?.toUTCString() === invokedAt.toUTCString() && !settingDuration) { - if (defaultDurationDoc?.date?.valueOf() > 0) { - untilDate = new Date(invokedAt.valueOf() + defaultDurationDoc.date.valueOf() - 1000); + if (untilDate?.toUTCString?.() === invokedAt.toUTCString() && !settingDuration) { + if (MUTE.defaultDuration.date?.valueOf() > 0) { + untilDate = new Date(invokedAt.valueOf() + MUTE.defaultDuration.date.valueOf() - 1000); } else { untilDate = "Indefinite"; } @@ -196,19 +189,14 @@ module.exports = class mute extends commando.Command { } if (settingDuration && !settingDurationHasSet && timeForMessage.length > 0) { settingDurationHasSet = true; - theSettingUp.defaultDuration.date = elapsedTime, - theSettingUp.defaultDuration.string = timeForMessage.join(" "); + MUTE.defaultDuration.date = elapsedTime, + MUTE.defaultDuration.string = timeForMessage.join(" "); } } if (settingUp || !roleConfCheck && !settingUp) { - if (settingRoleHasSet) { - await col.updateOne({ document: msg.guild.id }, { $set: { "moderation.settings.mute.role": theSettingUp.role } }, { upsert: true }).catch(e => { return trySend(this.client, msg, "```js\n" + e.stack + "```") }); + if (settingDurationHasSet || settingRoleHasSet) { + MOD.mute = MUTE; } - if (durationHasSet) { - await col.updateOne({ document: msg.guild.id }, { $set: { "moderation.settings.mute.defaultDuration": theSettingUp.defaultDuration } }, { upsert: true }).catch(e => { return trySend(this.client, msg, "```js\n" + e.stack + "```") }); - } - const defaultDurationDoc = muteSettingsDoc?.defaultDuration, - roleDoc = muteSettingsDoc?.role; let settings = defaultImageEmbed(msg); settings .setTitle("Mute Configuration") diff --git a/resources/structures.js b/resources/structures.js index e6cef10..f28e35d 100644 --- a/resources/structures.js +++ b/resources/structures.js @@ -8,22 +8,25 @@ Structures.extend("Guild", g => { return class Guild extends g { constructor(client, data) { super(client, data); - this.dbLoaded = false; } async dbLoad() { - return database.collection("Guild").findOne({ document: this.id }).then((r, e) => { + return database.collection("Guild").findOne({ document: this.id }, (e, r) => { if (e) return errLog(e, null, this.client); - this.infractions = r?.moderation?.infractions || []; - this.moderation = r?.moderation?.settings || {}; - this.defaultEmbed = r?.settings?.defaultEmbed || {}; - this.quoteOTD = r?.settings?.quoteOTD || {}; - this.eventChannels = r?.settings?.eventChannels || {}; - - return this.dbLoaded = true; + 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 Error("Empty!"); + return database.collection("Guild").updateOne({ document: this.id }, { $set: Db, $setOnInsert: { document: this.id } }, + { upsert: true }, (e) => { + if (e) return errLog(e, null, this.client); + return this.DB = Db; + }); + } + /** * Get user infractions * @param {String} get - User ID @@ -31,11 +34,10 @@ Structures.extend("Guild", g => { */ async getInfractions(get) { try { - const r = await database.collection("Guild").findOne({ document: this.id }); - this.infractions = r?.moderation?.infractions; + if (!this.DB) await this.dbLoad(); let found = []; - if (this.infractions.length > 0) { - for (const inf of this.infractions) { + if (this.DB.moderation?.infractions?.length > 0) { + for (const inf of this.DB.moderation.infractions) { for (const user of inf.by) { if (user.id === get) { found.push(inf); @@ -50,46 +52,35 @@ Structures.extend("Guild", g => { async addInfraction(add) { try { - const r = await database.collection("Guild").findOne({ document: this.id }); - this.infractions = r?.moderation?.infractions; - return database.collection("Guild").updateOne({ document: this.id }, { $push: { "moderation.infractions": add } }, (e) => { - if (e) return errLog(e, null, this.client); - this.infractions.push(add); - return true; - }); + if (!this.DB) await this.dbLoad(); + if (!this.DB.moderation?.infractions) this.DB.moderation.infractions = []; + this.DB.moderation.infractions.push(add); + return this.setDb(this.DB); } catch (e) { } } async setQuoteOTD(set) { - return database.collection("Guild").updateOne({ document: this.id }, { $set: { "settings.quoteOTD": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { - if (e) return errLog(e, null, this.client); - this.quoteOTD = set; - return true; - }); + if (!this.DB) await this.dbLoad(); + this.DB.settings.quoteOTD = set; + return this.setDb(this.DB); } async setEventChannels(set) { - return database.collection("Guild").updateOne({ document: this.id }, { $set: { "settings.eventChannels": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { - if (e) return errLog(e, null, this.client); - this.eventChannels = set; - return true; - }); + if (!this.DB) await this.dbLoad(); + this.DB.settings.eventChannels = set; + return this.setDb(this.DB); } async setDefaultEmbed(set) { - return database.collection("Guild").updateOne({ document: this.id }, { $set: { "settings.defaultEmbed": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { - if (e) return errLog(e, null, this.client); - this.defaultEmbed = set; - return true; - }); + if (!this.DB) await this.dbLoad(); + this.DB.settings.defaultEmbed = set; + return this.setDb(this.DB); } async setModerationSettings(set) { - return database.collection("Guild").updateOne({ document: this.id }, { $set: { "moderation.settings": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { - if (e) return errLog(e, null, this.client); - this.moderation = set; - return true; - }); + if (!this.DB) await this.dbLoad(); + this.DB.moderation.settings = set; + return this.setDb(this.DB); } } }); @@ -98,53 +89,49 @@ Structures.extend("User", u => { return class User extends u { constructor(client, data) { super(client, data); - this.dbLoaded = false; this.cutie = true; - this.F = "F"; - } - - async setF(string) { - return database.collection("User").updateOne({ document: this.id }, { $set: { F: string }, $setOnInsert: { document: this.id } }, { upsert: true }, (e, r) => { - if (e) return errLog(e, null, this.client); - this.F = string; - return true; - }); } async dbLoad() { - return database.collection("User").findOne({ document: this.id }).then((r, e) => { + return database.collection("User").findOne({ document: this.id }, (e, r) => { if (e) return errLog(e, null, this.client); - this.defaultEmbed = r?.settings?.defaultEmbed || {}; - this.cachedAvatarURL = this.displayAvatarURL({ format: "png", size: 4096, dynamic: true }); - this.interactions = r?.interactions || {}; - this.description = r?.description; - this.F = r?.F; - return this.dbLoaded = true; + if (!r.F) r.F = "F"; + 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 Error("Empty!"); + return database.collection("User").updateOne({ document: this.id }, { $set: Db, $setOnInsert: { document: this.id } }, + { upsert: true }, (e) => { + if (e) return errLog(e, null, this.client); + return this.DB = Db; + }); + } + + async setF(string) { + if (!this.DB) await this.dbLoad(); + this.DB.F = string; + return this.setDb(this.DB); + } + async setInteractions(count) { - return database.collection("User").updateOne({ document: this.id }, { $set: { interactions: count }, $setOnInsert: { document: this.id } }, { upsert: true }, (e, r) => { - if (e) return errLog(e, null, this.client); - this.interactions = count; - return true; - }); + if (!this.DB) await this.dbLoad(); + this.DB.interactions = count; + return this.setDb(this.DB); } async setDescription(set) { - return database.collection("User").updateOne({ document: this.id }, { $set: { description: set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e, r) => { - if (e) return errLog(e, null, this.client); - this.description = set; - return true; - }); + if (!this.DB) await this.dbLoad(); + this.DB.description = set; + return this.setDb(this.DB); } async setDefaultEmbed(set) { - return database.collection("User").updateOne({ document: this.id }, { $set: { "settings.defaultEmbed": set }, $setOnInsert: { document: this.id } }, { upsert: true }, (e) => { - if (e) return errLog(e, null, this.client); - this.defaultEmbed = set; - return true; - }); + if (!this.DB) await this.dbLoad(); + this.DB.defaultEmbed = set; + return this.setDb(this.DB); } } }); @@ -200,8 +187,29 @@ Structures.extend("GuildMember", e => { super(client, data, guild); } + async dbLoad() { + return database.collection("GuildMember").findOne({ document: this.id }, (e, r) => { + if (e) return errLog(e, null, this.client); + 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 Error("Empty!"); + return database.collection("GuildMember").updateOne({ document: this.id }, { $set: Db, $setOnInsert: { document: this.id } }, + { upsert: true }, (e) => { + if (e) return errLog(e, null, this.client); + return this.DB = Db; + }); + } + async infractions() { return this.guild.getInfractions(this.id); } + + async mute() { + + } } }); \ No newline at end of file