From 0eac1ab436fd1d22ec0454a2dca5dacb47a77787 Mon Sep 17 00:00:00 2001 From: Dragory Date: Sat, 12 Jan 2019 14:39:22 +0200 Subject: [PATCH] Mutes: clear mutes if the user is banned; add command for clearing mutes from banned users --- src/plugins/Mutes.ts | 46 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/plugins/Mutes.ts b/src/plugins/Mutes.ts index c93e9f17..9eed9424 100644 --- a/src/plugins/Mutes.ts +++ b/src/plugins/Mutes.ts @@ -1,13 +1,14 @@ -import { Member, TextableChannel } from "eris"; +import { Member, Message, TextableChannel, User } from "eris"; import { GuildCases } from "../data/GuildCases"; import moment from "moment-timezone"; import { ZeppelinPlugin } from "./ZeppelinPlugin"; import { GuildActions } from "../data/GuildActions"; import { GuildMutes } from "../data/GuildMutes"; -import { DBDateFormat, chunkMessageLines, stripObjectToScalars } from "../utils"; +import { DBDateFormat, chunkMessageLines, stripObjectToScalars, successMessage } from "../utils"; import humanizeDuration from "humanize-duration"; import { LogType } from "../data/LogType"; import { GuildLogs } from "../data/GuildLogs"; +import { decorators as d } from "knub"; export class MutesPlugin extends ZeppelinPlugin { public static pluginName = "mutes"; @@ -22,7 +23,18 @@ export class MutesPlugin extends ZeppelinPlugin { return { config: { mute_role: null - } + }, + permissions: { + cleanup: false + }, + overrides: [ + { + level: ">=100", + permissions: { + cleanup: true + } + } + ] }; } @@ -137,6 +149,34 @@ export class MutesPlugin extends ZeppelinPlugin { } } + @d.event("guildBanAdd") + async onGuildBanAdd(_, user: User) { + const mute = await this.mutes.findExistingMuteForUserId(user.id); + if (mute) { + this.mutes.clear(user.id); + } + } + + @d.command("clear_banned_mutes") + @d.permission("cleanup") + async clearBannedMutesCmd(msg: Message) { + const activeMutes = await this.mutes.getActiveMutes(); + const bans = await this.guild.getBans(); + const bannedIds = bans.map(b => b.id); + + await msg.channel.createMessage("Clearing mutes from banned users..."); + + let cleared = 0; + for (const mute of activeMutes) { + if (bannedIds.includes(mute.user_id)) { + await this.mutes.clear(mute.user_id); + cleared++; + } + } + + msg.channel.createMessage(successMessage(`Cleared mutes from banned users!`)); + } + protected async clearExpiredMutes() { const expiredMutes = await this.mutes.getExpiredMutes(); for (const mute of expiredMutes) {