From 9aeae4f89eaba5e5587ccb64da07193faf7046fa Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Tue, 3 Mar 2020 11:19:36 +0200 Subject: [PATCH] Improve error resilience with auto-reactions --- backend/src/plugins/AutoReactionsPlugin.ts | 47 ++++++++++++++++++---- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/backend/src/plugins/AutoReactionsPlugin.ts b/backend/src/plugins/AutoReactionsPlugin.ts index 42d8ca33..cd4baf65 100644 --- a/backend/src/plugins/AutoReactionsPlugin.ts +++ b/backend/src/plugins/AutoReactionsPlugin.ts @@ -128,14 +128,25 @@ export class AutoReactionsPlugin extends ZeppelinPlugin { 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 + if (e instanceof DiscordRESTError) { logger.warn( - `Could not apply auto-reactions to ${msg.channel_id}/${msg.id} in guild ${this.guild.name} (${this.guildId}) (error code 10008)`, + `Could not load auto-reaction message ${msg.channel_id}/${msg.id} in guild ${this.guild.name} (${this.guildId}) (error code ${e.code})`, ); - 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.`, - }); + + if (e.code === 50001) { + // Missing access + this.logs.log(LogType.BOT_ALERT, { + body: `Could not load auto-reaction message \`${msg.id}\` in <#${msg.channel_id}>. Make sure the bot has **Read Message History** permissions on the channel.`, + }); + } else if (e.code === 10008) { + this.logs.log(LogType.BOT_ALERT, { + body: `Could not load auto-reaction message \`${msg.id}\` in <#${msg.channel_id}>. Make sure nothing is deleting the message immediately.`, + }); + } else { + this.logs.log(LogType.BOT_ALERT, { + body: `Could not load auto-reaction message \`${msg.id}\` in <#${msg.channel_id}>. Error code ${e.code}.`, + }); + } return; } else { throw e; @@ -143,7 +154,29 @@ export class AutoReactionsPlugin extends ZeppelinPlugin { } for (const reaction of autoReaction.reactions) { - realMsg.addReaction(reaction); + try { + await realMsg.addReaction(reaction); + } catch (e) { + if (e instanceof DiscordRESTError) { + logger.warn( + `Could not apply auto-reaction to ${msg.channel_id}/${msg.id} in guild ${this.guild.name} (${this.guildId}) (error code ${e.code})`, + ); + + if (e.code === 10008) { + this.logs.log(LogType.BOT_ALERT, { + body: `Could not apply auto-reactions in <#${msg.channel_id}> for message \`${msg.id}\`. Make sure nothing is deleting the message before the reactions are applied.`, + }); + } else { + this.logs.log(LogType.BOT_ALERT, { + body: `Could not apply auto-reactions in <#${msg.channel_id}> for message \`${msg.id}\`. Error code ${e.code}.`, + }); + } + + return; + } else { + throw e; + } + } } } }