From 9b9b6a1518e6b853989c53d183c061dc7a592cbc Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sun, 12 Jan 2020 17:05:30 +0200 Subject: [PATCH] AutoReactions: send a bot alert if the bot is missing permissions to apply auto reactions (triggered by unknown messages, so usually 'read message history') --- backend/src/plugins/AutoReactionsPlugin.ts | 28 ++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/backend/src/plugins/AutoReactionsPlugin.ts b/backend/src/plugins/AutoReactionsPlugin.ts index 00e95fba..42d8ca33 100644 --- a/backend/src/plugins/AutoReactionsPlugin.ts +++ b/backend/src/plugins/AutoReactionsPlugin.ts @@ -1,11 +1,14 @@ -import { decorators as d, IBasePluginConfig, IPluginOptions } from "knub"; +import { decorators as d, IPluginOptions, logger } from "knub"; import { GuildSavedMessages } from "../data/GuildSavedMessages"; import { SavedMessage } from "../data/entities/SavedMessage"; import { GuildAutoReactions } from "../data/GuildAutoReactions"; import { Message } from "eris"; -import { customEmojiRegex, errorMessage, isEmoji, successMessage } from "../utils"; +import { customEmojiRegex, errorMessage, isEmoji } from "../utils"; import { CommandInfo, trimPluginDescription, ZeppelinPlugin } from "./ZeppelinPlugin"; +import DiscordRESTError from "eris/lib/errors/DiscordRESTError"; // tslint:disable-line import * as t from "io-ts"; +import { GuildLogs } from "../data/GuildLogs"; +import { LogType } from "../data/LogType"; const ConfigSchema = t.type({ can_manage: t.boolean, @@ -25,6 +28,7 @@ export class AutoReactionsPlugin extends ZeppelinPlugin { protected savedMessages: GuildSavedMessages; protected autoReactions: GuildAutoReactions; + protected logs: GuildLogs; private onMessageCreateFn; @@ -46,6 +50,7 @@ export class AutoReactionsPlugin extends ZeppelinPlugin { } onLoad() { + this.logs = new GuildLogs(this.guildId); this.savedMessages = GuildSavedMessages.getGuildInstance(this.guildId); this.autoReactions = GuildAutoReactions.getGuildInstance(this.guildId); @@ -119,8 +124,23 @@ export class AutoReactionsPlugin extends ZeppelinPlugin { const autoReaction = await this.autoReactions.getForChannel(msg.channel_id); if (!autoReaction) return; - const realMsg = await this.bot.getMessage(msg.channel_id, msg.id); - if (!realMsg) return; + let realMsg; + try { + realMsg = await this.bot.getMessage(msg.channel_id, msg.id); + } catch (e) { + if (e instanceof DiscordRESTError && e.code === 10008) { + // Unknown message, post warning in logs + logger.warn( + `Could not apply auto-reactions to ${msg.channel_id}/${msg.id} in guild ${this.guild.name} (${this.guildId}) (error code 10008)`, + ); + this.logs.log(LogType.BOT_ALERT, { + body: `Could not apply auto-reactions in <#${msg.channel_id}> for message \`${msg.id}\`. Make sure the bot has **Read Message History** permissions on the channel.`, + }); + return; + } else { + throw e; + } + } for (const reaction of autoReaction.reactions) { realMsg.addReaction(reaction);