From dea3c2516ce04d548ccf5f9220f774c1d8fee374 Mon Sep 17 00:00:00 2001 From: Nils <7890309+DarkView@users.noreply.github.com> Date: Thu, 28 Jan 2021 00:25:11 +0100 Subject: [PATCH] Add more verbose mute error messages as BOT_ALERTs and console errors (#146) --- backend/src/RecoverablePluginError.ts | 4 +++ .../src/plugins/Mutes/functions/muteUser.ts | 34 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/backend/src/RecoverablePluginError.ts b/backend/src/RecoverablePluginError.ts index 0a63ed78..3cb1c7ac 100644 --- a/backend/src/RecoverablePluginError.ts +++ b/backend/src/RecoverablePluginError.ts @@ -7,6 +7,8 @@ export enum ERRORS { NO_USER_NOTIFICATION_CHANNEL, INVALID_USER_NOTIFICATION_CHANNEL, INVALID_USER, + INVALID_MUTE_ROLE_ID, + MUTE_ROLE_ABOVE_ZEP, } export const RECOVERABLE_PLUGIN_ERROR_MESSAGES = { @@ -16,6 +18,8 @@ export const RECOVERABLE_PLUGIN_ERROR_MESSAGES = { [ERRORS.NO_USER_NOTIFICATION_CHANNEL]: "No user notify channel specified", [ERRORS.INVALID_USER_NOTIFICATION_CHANNEL]: "Invalid user notify channel specified", [ERRORS.INVALID_USER]: "Invalid user", + [ERRORS.INVALID_MUTE_ROLE_ID]: "Specified mute role is not invalid", + [ERRORS.MUTE_ROLE_ABOVE_ZEP]: "Specified mute role is above Zeppelin in the role hierarchy", }; export class RecoverablePluginError extends Error { diff --git a/backend/src/plugins/Mutes/functions/muteUser.ts b/backend/src/plugins/Mutes/functions/muteUser.ts index 6f33faaa..1ccbcc35 100644 --- a/backend/src/plugins/Mutes/functions/muteUser.ts +++ b/backend/src/plugins/Mutes/functions/muteUser.ts @@ -17,6 +17,8 @@ import { CasesPlugin } from "../../Cases/CasesPlugin"; import { CaseTypes } from "../../../data/CaseTypes"; import { LogType } from "../../../data/LogType"; import { Case } from "../../../data/entities/Case"; +import { sendErrorMessage } from "src/pluginUtils"; +import { LogsPlugin } from "src/plugins/Logs/LogsPlugin"; export async function muteUser( pluginData: GuildPluginData, @@ -51,9 +53,39 @@ export async function muteUser( const config = pluginData.config.getMatchingConfig({ member, userId }); if (member) { + const logs = pluginData.getPlugin(LogsPlugin); // Apply mute role if it's missing if (!member.roles.includes(muteRole)) { - await member.addRole(muteRole); + try { + await member.addRole(muteRole); + } catch (e) { + const actualMuteRole = pluginData.guild.roles.find(x => x.id === muteRole); + if (!actualMuteRole) { + lock.unlock(); + logs.log(LogType.BOT_ALERT, { + body: `Cannot mute users, specified mute role Id is invalid`, + }); + throw new RecoverablePluginError(ERRORS.INVALID_MUTE_ROLE_ID); + } + + const zep = await resolveMember(pluginData.client, pluginData.guild, pluginData.client.user.id); + const zepRoles = pluginData.guild.roles.filter(x => zep!.roles.includes(x.id)); + // If we have roles and one of them is above the muted role, throw generic error + if (zepRoles.length >= 0 && zepRoles.some(zepRole => zepRole.position > actualMuteRole.position)) { + lock.unlock(); + logs.log(LogType.BOT_ALERT, { + body: `Cannot mute user ${member.id}: ${e}`, + }); + throw e; + } else { + // Otherwise, throw error that mute role is above zeps roles + lock.unlock(); + logs.log(LogType.BOT_ALERT, { + body: `Cannot mute users, specified mute role is above Zeppelin in the role hierarchy`, + }); + throw new RecoverablePluginError(ERRORS.MUTE_ROLE_ABOVE_ZEP); + } + } } // If enabled, move the user to the mute voice channel (e.g. afk - just to apply the voice perms from the mute role)