From 186ac659246db1467699991ab30d7bf416de095e Mon Sep 17 00:00:00 2001
From: Dragory <2606411+Dragory@users.noreply.github.com>
Date: Sun, 4 Apr 2021 22:30:21 +0300
Subject: [PATCH] Handle errors when sending message in automod 'alert' action

---
 backend/src/plugins/Automod/actions/alert.ts | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/backend/src/plugins/Automod/actions/alert.ts b/backend/src/plugins/Automod/actions/alert.ts
index f4a098c9..b22f8e3c 100644
--- a/backend/src/plugins/Automod/actions/alert.ts
+++ b/backend/src/plugins/Automod/actions/alert.ts
@@ -9,6 +9,7 @@ import {
   resolveMember,
   stripObjectToScalars,
   tNullable,
+  verboseChannelMention,
 } from "../../../utils";
 import { resolveActionContactMethods } from "../functions/resolveActionContactMethods";
 import { ModActionsPlugin } from "../../ModActions/ModActionsPlugin";
@@ -68,7 +69,23 @@ export const AlertAction = automodAction({
         throw err;
       }
 
-      await createChunkedMessage(channel, rendered);
+      try {
+        await createChunkedMessage(channel, rendered);
+      } catch (err) {
+        if (err.code === 50001) {
+          logs.log(LogType.BOT_ALERT, {
+            body: `Missing access to send alert to channel ${verboseChannelMention(
+              channel,
+            )} in automod rule **${ruleName}**`,
+          });
+        } else {
+          logs.log(LogType.BOT_ALERT, {
+            body: `Error ${err.code || "UNKNOWN"} when sending alert to channel ${verboseChannelMention(
+              channel,
+            )} in automod rule **${ruleName}**`,
+          });
+        }
+      }
     } else {
       logs.log(LogType.BOT_ALERT, {
         body: `Invalid channel id \`${actionConfig.channel}\` for alert action in automod rule **${ruleName}**`,