From 166c011997200761244a896f92f046bd38118b29 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Wed, 15 Apr 2020 09:15:00 +0300 Subject: [PATCH] Fix error when missing permissions to post in case log channel --- backend/src/plugins/Cases.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/backend/src/plugins/Cases.ts b/backend/src/plugins/Cases.ts index 44c17402..b77a8a5d 100644 --- a/backend/src/plugins/Cases.ts +++ b/backend/src/plugins/Cases.ts @@ -6,12 +6,13 @@ import moment from "moment-timezone"; import { CaseTypeColors } from "../data/CaseTypeColors"; import { PluginInfo, trimPluginDescription, ZeppelinPlugin } from "./ZeppelinPlugin"; import { GuildArchives } from "../data/GuildArchives"; -import { IPluginOptions } from "knub"; +import { IPluginOptions, logger } from "knub"; import { GuildLogs } from "../data/GuildLogs"; import { LogType } from "../data/LogType"; import * as t from "io-ts"; import { tNullable } from "../utils"; import { ERRORS } from "../RecoverablePluginError"; +import DiscordRESTError from "eris/lib/errors/DiscordRESTError"; // tslint:disable-line const ConfigSchema = t.type({ log_automatic_actions: t.boolean, @@ -264,14 +265,31 @@ export class CasesPlugin extends ZeppelinPlugin { * A helper for posting to the case log channel. * Returns silently if the case log channel isn't specified or is invalid. */ - public postToCaseLogChannel(content: MessageContent, file: MessageFile = null): Promise { + public async postToCaseLogChannel(content: MessageContent, file: MessageFile = null): Promise { const caseLogChannelId = this.getConfig().case_log_channel; if (!caseLogChannelId) return; const caseLogChannel = this.guild.channels.get(caseLogChannelId); if (!caseLogChannel || !(caseLogChannel instanceof TextChannel)) return; - return caseLogChannel.createMessage(content, file); + let result; + try { + result = await caseLogChannel.createMessage(content, file); + } catch (e) { + if (e instanceof DiscordRESTError && e.code === 50013) { + logger.warn( + `Missing permissions to post mod cases in <#${caseLogChannel.id}> in guild ${this.guild.name} (${this.guild.id})`, + ); + this.logs.log(LogType.BOT_ALERT, { + body: `Missing permissions to post mod cases in <#${caseLogChannel.id}>`, + }); + return; + } + + throw e; + } + + return result; } /**