From 7a0cad4bfeb244391215213cbc117cc3b5c1cf20 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Wed, 14 Jul 2021 14:14:38 +0700 Subject: [PATCH] Mute: Methods written --- resources/structures.js | 60 ++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/resources/structures.js b/resources/structures.js index f28e35d..2f7a97e 100644 --- a/resources/structures.js +++ b/resources/structures.js @@ -4,8 +4,8 @@ const { Structures } = require("discord.js"), { database } = require("../database/mongo"), { errLog } = require("./functions"); -Structures.extend("Guild", g => { - return class Guild extends g { +Structures.extend("Guild", u => { + return class Guild extends u { constructor(client, data) { super(client, data); } @@ -30,7 +30,7 @@ Structures.extend("Guild", g => { /** * Get user infractions * @param {String} get - User ID - * @returns {Promise} Infractions object + * @returns {Promise} Array of infractions objects */ async getInfractions(get) { try { @@ -136,8 +136,8 @@ Structures.extend("User", u => { } }); -Structures.extend("TextChannel", e => { - return class TextChannel extends e { +Structures.extend("TextChannel", u => { + return class TextChannel extends u { constructor(guild, data) { super(guild, data); this.lastMessagesID = []; @@ -152,8 +152,8 @@ Structures.extend("TextChannel", e => { } }); -Structures.extend("DMChannel", e => { - return class DMChannel extends e { +Structures.extend("DMChannel", u => { + return class DMChannel extends u { constructor(client, data) { super(client, data); this.lastMessagesID = []; @@ -181,8 +181,8 @@ Structures.extend("Message", e => { }; }); -Structures.extend("GuildMember", e => { - return class GuildMember extends e { +Structures.extend("GuildMember", u => { + return class GuildMember extends u { constructor(client, data, guild) { super(client, data, guild); } @@ -208,8 +208,48 @@ Structures.extend("GuildMember", e => { return this.guild.getInfractions(this.id); } - async mute() { + /** + * @param {Date | number} until + * @returns + */ + async mute(until) { + if (!this.DB) await this.dbLoad(); + if (!this.guild.DB) await this.guild.dbLoad(); + this.DB.takenRoles = this.roles.cache.map(r => r.id); + this.DB.muteRole = this.guild.DB.moderation.settings.mute.role; + if ((typeof until) === "number") until = new Date(until); + try { + const RM = await this.roles.remove(this.DB.takenRoles); + await RM.roles.add(this.DB.muteRole); + this.DB.muted = { + state: true, + until: until + }; + return this.setDb(this.DB); + } catch (e) { + if (this.DB.takenRoles?.length > 0) this.roles.add(this.DB.takenRoles).catch(() => { }); + this.DB.takenRoles = []; + this.DB.muteRole = undefined; + this.DB.muted = { state: false }; + const R = await this.setDb(this.DB); + if (e) throw e; + return R; + } + } + async unmute() { + if (!this.DB) await this.dbLoad(); + try { + const RM = await this.roles.add(this.DB.takenRoles); + await RM.roles.remove(this.DB.muteRole); + this.DB.takenRoles = []; + this.DB.muteRole = undefined; + this.DB.muted = { state: false }; + return this.setDb(this.DB); + } catch (e) { + if (e) throw e; + return; + } } } }); \ No newline at end of file