From 88a9712f730b33f51675cc1367685b690489d2ef Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Tue, 28 Jun 2022 17:37:51 +0300 Subject: [PATCH] Lockdown & unlockdown --- .../en_gb/general/en_gb_moderation.lang | 11 +++- .../components/infractions/Lockdown.js | 31 ++++++--- .../components/infractions/Unlockdown.js | 64 +++++++++++++++++-- 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/src/localization/en_gb/general/en_gb_moderation.lang b/src/localization/en_gb/general/en_gb_moderation.lang index 164fc94..a9ae118 100644 --- a/src/localization/en_gb/general/en_gb_moderation.lang +++ b/src/localization/en_gb/general/en_gb_moderation.lang @@ -88,4 +88,13 @@ Cannot edit duration of a non-timed infraction. //Nickname Command [INFRACTION_DESCRIPTIONNICKNAME] -**Old Name:** {old} | **New Name:** {new} \ No newline at end of file +**Old Name:** {old} | **New Name:** {new} + +[INFRACTION_UNLOCKDOWN_NOTLOCKED] +The channel isn't locked down. + +[INFRACTION_LOCKDOWN_EXISTING] +The channel is already locked down. +Use `/unlockdown` to unlock it. + +If you've manually reverted the lockdown, resolve case **#{case}**. \ No newline at end of file diff --git a/src/structure/components/infractions/Lockdown.js b/src/structure/components/infractions/Lockdown.js index 8ecfea2..3bbcf79 100644 --- a/src/structure/components/infractions/Lockdown.js +++ b/src/structure/components/infractions/Lockdown.js @@ -29,6 +29,11 @@ class LockdownInfraction extends Infraction { async execute() { + const existing = await this.client.moderationManager.findLatestInfraction('LOCKDOWN', this.target); + if (!existing._callbacked && !existing.resolved) { + return this._fail({ message: this.guild.format('INFRACTION_LOCKDOWN_EXISTING', { case: existing.case }) }); + } + const permissions = this.target.permissionOverwrites; const everyoneRole = this.guild.roles.everyone; @@ -39,8 +44,11 @@ class LockdownInfraction extends Infraction { const roleOrMember = permission.type === 'role' ? await this.guild.roles.fetch(permission.id) : await this.guild.members.fetch(permission.id); - if (roleOrMember && roleOrMember.permissions.has(allowedPermissions)) continue; - // console.log(`Doing ${permission.id}: ${permission.type}`); + if (roleOrMember && roleOrMember.permissions.has(allowedPermissions)) { + newOverwrites.push(permission); + continue; + } + //console.log(`Doing ${permission.id} (${roleOrMember.name || roleOrMember.tag}): ${permission.type}`); this.data.oldPermissions[permission.id] = { type: permission.type, @@ -57,14 +65,15 @@ class LockdownInfraction extends Infraction { this.data.oldPermissions[permission.id].permissions.SEND_MESSAGES = true; allowed.splice(allowed.indexOf('SEND_MESSAGES'), 1); denied.push('SEND_MESSAGES'); - } + } else if (permission.deny.has('SEND_MESSAGES')) this.data.oldPermissions[permission.id].permissions.SEND_MESSAGES = false; + else denied.push('SEND_MESSAGES'); + if (permission.allow.has('ADD_REACTIONS')) { this.data.oldPermissions[permission.id].permissions.ADD_REACTIONS = true; allowed.splice(allowed.indexOf('ADD_REACTIONS'), 1); denied.push('ADD_REACTIONS'); - } - if (permission.deny.has('SEND_MESSAGES')) this.data.oldPermissions[permission.id].permissions.SEND_MESSAGES = false; - if (permission.deny.has('ADD_REACTIONS')) this.data.oldPermissions[permission.id].permissions.ADD_REACTIONS = false; + } else if (permission.deny.has('ADD_REACTIONS')) this.data.oldPermissions[permission.id].permissions.ADD_REACTIONS = false; + else denied.push('ADD_REACTIONS'); newOverwrites.push({ @@ -110,9 +119,15 @@ class LockdownInfraction extends Infraction { // SEND_MESSAGES: true, // ADD_REACTIONS: true // }, { type: 1 }); - newOverwrites.push({ + const index = newOverwrites.findIndex((entry) => entry.id === this.client.user.id); + if (index >= 0) { + if (!newOverwrites[index].allow.has('SEND_MESSAGES')) + newOverwrites[index].allow.add('SEND_MESSAGES'); + if (newOverwrites[index].deny.has('SEND_MESSAGES')) + newOverwrites[index].deny.remove('SEND_MESSAGES'); + } else newOverwrites.push({ id: this.client.user.id, - allow: ['SEND_MESSAGES', 'ADD_REACTIONS'] + allow: ['SEND_MESSAGES'] }); await permissions.set(newOverwrites, this._reason); diff --git a/src/structure/components/infractions/Unlockdown.js b/src/structure/components/infractions/Unlockdown.js index c87b316..c0b7d55 100644 --- a/src/structure/components/infractions/Unlockdown.js +++ b/src/structure/components/infractions/Unlockdown.js @@ -12,7 +12,7 @@ class UnlockdownInfraction extends Infraction { type: opts.type, invoker: opts.invoker, executor: opts.executor.user, - target: opts.target.user, + target: opts.target, reason: opts.reason, guild: opts.guild, channel: opts.channel, @@ -22,17 +22,67 @@ class UnlockdownInfraction extends Infraction { hyperlink: opts.hyperlink }); - } async execute() { - const callback = await this.client.moderationManager.callbacks.filter((c) => { - return c.infraction.type === this.type - && c.infraction.target === this.targetId; - }).first(); + // const callback = await this.client.moderationManager.callbacks.filter((c) => { + // return c.infraction.type === this.type + // && c.infraction.target === this.targetId; + // }).first(); - if (!callback) await this.handle(); + let latest = null; + if (!this.data) { + latest = await this.client.moderationManager.findLatestInfraction('LOCKDOWN', this.target); + if (latest._callbacked || latest.resolved) return this._fail({ message: this.guild.format('INFRACTION_UNLOCKDOWN_NOTLOCKED') }); + } + + const data = latest?.data || this.data; + const oldPerms = data.oldPermissions; + const permissions = this.target.permissionOverwrites; + const overwrites = [...permissions.cache.values()]; + const newOverwrites = []; + + for (const permission of overwrites) { + + if (!oldPerms[permission.id]) { + newOverwrites.push(permission); + continue; + } + + const allowed = permission.allow.toArray(); + const denied = permission.deny.toArray(); + const old = oldPerms[permission.id].permissions; + + if (old.SEND_MESSAGES === null) denied.splice(denied.indexOf('SEND_MESSAGES'), 1); + if (old.ADD_REACTIONS === null) denied.splice(denied.indexOf('ADD_REACTIONS'), 1); + + if (old.SEND_MESSAGES) { + denied.splice(denied.indexOf('SEND_MESSAGES'), 1); + allowed.push('SEND_MESSAGES'); + } + if (old.ADD_REACTIONS) { + denied.splice(denied.indexOf('ADD_REACTIONS'), 1); + allowed.push('ADD_REACTIONS'); + } + + newOverwrites.push({ + id: permission.id, + allow: allowed, + deny: denied, + type: permission.type + }); + + } + + await permissions.set(newOverwrites, this._reason); + + if (latest) { + const callback = this.client.moderationManager.callbacks.get(latest.id); + await this.client.moderationManager.removeCallback(callback, true); + } + + await this.handle(); return this._succeed(); }