Преглед на файлове

Add error handling to react

ghorsington преди 3 години
родител
ревизия
5e4b19d831
променени са 2 файла, в които са добавени 25 реда и са изтрити 7 реда
  1. 6 1
      bot/src/plugins/random_react.ts
  2. 19 6
      bot/src/plugins/react.ts

+ 6 - 1
bot/src/plugins/random_react.ts

@@ -3,6 +3,8 @@ import { getRepository } from "typeorm";
 import { RandomMessageReaction } from "@shared/db/entity/RandomMesssageReaction";
 import { client } from "src/client";
 import { Event, BotEventData, Plugin } from "src/model/plugin";
+import { tryDo } from "@shared/common/async_utils";
+import { logger } from "src/logging";
 
 const timeout = (ms: number) => new Promise(r => setTimeout(r, ms));
 
@@ -27,7 +29,10 @@ export class RandomReact {
 
         if(Math.random() < reactInfo.reactProbability) {
             await timeout(Math.random() * reactInfo.maxWaitMs);
-            await msg.react(emote);
+            const result = await tryDo(msg.react(emote));
+            if (!result.ok) {
+                logger.error("Failed to react to user %s#%s (%s): %s", msg.author.username, msg.author.discriminator, msg.author.id, result.error);
+            }
         }
     }
 }

+ 19 - 6
bot/src/plugins/react.ts

@@ -6,6 +6,7 @@ import { Message, User, PartialUser, MessageReaction as DiscordMessageReaction }
 import { logger } from "src/logging";
 import { Command, ICommandData, Event, BotEventData, Plugin } from "src/model/plugin";
 import { MessageReaction } from "@shared/db/entity/MessageReaction";
+import { tryDo } from "@shared/common/async_utils";
 
 const pattern = /^react to\s+"([^"]+)"\s+with\s+<:[^:]+:([^>]+)>$/i;
 
@@ -33,8 +34,12 @@ export class ReactCommands {
     @Event("messageReactionAdd")
     async randomReact(data: BotEventData, reaction: DiscordMessageReaction, user: User | PartialUser): Promise<void> {
         if (Math.random() <= this.REACT_PROBABILITY && !user.bot) {
-            logger.verbose(`Reacting to message ${reaction.message.id} because user ${user.tag} reacted to it`);
-            reaction.message.react(reaction.emoji);
+            const msg = reaction.message;
+            logger.verbose(`Reacting to message ${msg.id} because user ${user.tag} reacted to it`);
+            const result = await tryDo(msg.react(reaction.emoji));
+            if (!result.ok) {
+                logger.error("Failed to react to user %s#%s (%s): %s", msg.author.username, msg.author.discriminator, msg.author.id, result.error);
+            }
         }
     }
 
@@ -126,8 +131,12 @@ export class ReactCommands {
 
         if (message) {
             const emoji = client.bot.emojis.resolve(message.reactionEmoteId);
-            if (emoji)
-                msg.react(emoji);
+            if (emoji) {
+                const result = await tryDo(msg.react(emoji));
+                if (!result.ok) {
+                    logger.error("Failed to react to user %s#%s (%s): %s", msg.author.username, msg.author.discriminator, msg.author.id, result.error);
+                }
+            }
             data.actionsDone = true;
             return;
         }
@@ -161,8 +170,12 @@ export class ReactCommands {
 
         for (const emote of randomEmotes) {
             const emoji = client.bot.emojis.resolve(emote.reactionId);
-            if(emoji)
-                await msg.react(emoji);
+            if(emoji) {
+                const result = await tryDo(msg.react(emoji));
+                if (!result.ok) {
+                    logger.error("Failed to react to user %s#%s (%s): %s", msg.author.username, msg.author.discriminator, msg.author.id, result.error);
+                }
+            }
         }
 
         data.actionsDone = true;