Browse Source

Fix reaction collectors

ghorsington 4 năm trước cách đây
mục cha
commit
29ed57017f
2 tập tin đã thay đổi với 67 bổ sung64 xóa
  1. 30 29
      bot/src/commands/forums_news_checker.ts
  2. 37 35
      bot/src/commands/news_aggregator.ts

+ 30 - 29
bot/src/commands/forums_news_checker.ts

@@ -140,7 +140,7 @@ export class ForumsNewsChecker {
             }
 
             const collector = m.createReactionCollector(this.isVerifyReaction, { maxEmojis: 1 });
-            collector.on("collect", this.collectReaction);
+            collector.on("collect", this.collectorFor(collector));
             this.reactionCollectors[m.id] = collector;
             this.verifyMessageIdToPost[m.id] = msg.id;
         }
@@ -175,7 +175,7 @@ export class ForumsNewsChecker {
         await newMessage.react("❌");
 
         const collector = newMessage.createReactionCollector(this.isVerifyReaction, { maxEmojis: 1 });
-        collector.on("collect", this.collectReaction);
+        collector.on("collect", this.collectorFor(collector));
         this.reactionCollectors[newMessage.id] = collector;
         this.verifyMessageIdToPost[newMessage.id] = item.id;
 
@@ -183,33 +183,34 @@ export class ForumsNewsChecker {
         await forumsNewsRepo.save(item);
     }
 
-    collectReaction = async (reaction: MessageReaction, collector: Collector<string, MessageReaction>): Promise<void> => {
-        const verifyMessageRepo = getRepository(PostVerifyMessage);
-        const postRepo = getRepository(PostedForumNewsItem);
-
-        const m = reaction.message;
-        collector.stop();
-        delete this.reactionCollectors[m.id];
-        const postId = this.verifyMessageIdToPost[m.id];
-
-        const post = await postRepo.findOne({
-            where: { id: postId },
-            relations: ["verifyMessage"]
-        });
-
-        if (!post) 
-            throw new Error("Post not found!");
-
-        await postRepo.update({ id: post.id }, { verifyMessage: undefined });
-        if (post.verifyMessage)
-            await verifyMessageRepo.delete({ id: post.verifyMessage.id });
-        await reaction.message.delete();
-
-        if (reaction.emoji.name == "✅")
-            this.sendNews(post);
-
-        delete this.verifyMessageIdToPost[m.id];
-    }
+    collectorFor = (collector: ReactionCollector) =>
+        async (reaction: MessageReaction): Promise<void> => {
+            const verifyMessageRepo = getRepository(PostVerifyMessage);
+            const postRepo = getRepository(PostedForumNewsItem);
+    
+            const m = reaction.message;
+            collector.stop();
+            delete this.reactionCollectors[m.id];
+            const postId = this.verifyMessageIdToPost[m.id];
+    
+            const post = await postRepo.findOne({
+                where: { id: postId },
+                relations: ["verifyMessage"]
+            });
+    
+            if (!post) 
+                throw new Error("Post not found!");
+    
+            await postRepo.update({ id: post.id }, { verifyMessage: undefined });
+            if (post.verifyMessage)
+                await verifyMessageRepo.delete({ id: post.verifyMessage.id });
+            await reaction.message.delete();
+    
+            if (reaction.emoji.name == "✅")
+                this.sendNews(post);
+    
+            delete this.verifyMessageIdToPost[m.id];
+        };
 
     async sendNews(item: PostedForumNewsItem): Promise<void> {
         const channelRepo = getRepository(KnownChannel);

+ 37 - 35
bot/src/commands/news_aggregator.ts

@@ -163,7 +163,7 @@ export class NewsAggregator {
         await msg.react("❌");
 
         const collector = msg.createReactionCollector(this.isVerifyReaction, { maxEmojis: 1 });
-        collector.on("collect", this.collectReaction);
+        collector.on("collect", this.collectorFor(collector));
         this.reactionCollectors[msg.id] = collector;
         this.verifyMessageIdToPost[msg.id] = newsItem;
 
@@ -174,44 +174,45 @@ export class NewsAggregator {
         return (reaction.emoji.name == "✅" || reaction.emoji.name == "❌") && !user.bot && user.id != client.botUser.id;
     }
 
-    collectReaction = async (reaction: MessageReaction, collector: Collector<string, MessageReaction>): Promise<void> => {
-        const repo = getRepository(AggroNewsItem);
+    collectorFor = (collector: ReactionCollector) =>
+        async (reaction: MessageReaction): Promise<void> => {
+            const repo = getRepository(AggroNewsItem);
 
-        const m = reaction.message;
-        collector.stop();
-        delete this.reactionCollectors[m.id];
-        const post = this.verifyMessageIdToPost[m.id];
+            const m = reaction.message;
+            collector.stop();
+            delete this.reactionCollectors[m.id];
+            const post = this.verifyMessageIdToPost[m.id];
 
-        if(!post.forumsEditPostId) {
-            throw new Error("No forum edit post found!");
-        }
+            if(!post.forumsEditPostId) {
+                throw new Error("No forum edit post found!");
+            }
 
-        if (reaction.emoji.name == "✅") {
-            const res = await client.forum.getThread(post.forumsEditPostId);
-            const forumPost = await client.forum.getPost(res.thread.first_post_id);
-
-            if (!post.forumsNewsPostId) {
-                const newThread = await client.forum.createThread(FORUMS_NEWS_ID, res.thread.title, forumPost.message);
-                post.forumsNewsPostId = newThread.thread.thread_id;
-            } else {
-                const curThread = await client.forum.editThread(post.forumsNewsPostId, {
-                    title: res.thread.title
-                });
-
-                await client.forum.editPost(curThread.thread.first_post_id, {
-                    message: forumPost.message
-                });
+            if (reaction.emoji.name == "✅") {
+                const res = await client.forum.getThread(post.forumsEditPostId);
+                const forumPost = await client.forum.getPost(res.thread.first_post_id);
+
+                if (!post.forumsNewsPostId) {
+                    const newThread = await client.forum.createThread(FORUMS_NEWS_ID, res.thread.title, forumPost.message);
+                    post.forumsNewsPostId = newThread.thread.thread_id;
+                } else {
+                    const curThread = await client.forum.editThread(post.forumsNewsPostId, {
+                        title: res.thread.title
+                    });
+
+                    await client.forum.editPost(curThread.thread.first_post_id, {
+                        message: forumPost.message
+                    });
+                }
             }
-        }
 
-        await client.forum.deleteThread(post.forumsEditPostId);
-        await repo.update({ newsId: post.newsId, feedName: post.feedName }, { 
-            editMessageId: undefined, 
-            forumsEditPostId: undefined, 
-            forumsNewsPostId: post.forumsNewsPostId });
-        await reaction.message.delete();
-        delete this.verifyMessageIdToPost[m.id];
-    };
+            await client.forum.deleteThread(post.forumsEditPostId);
+            await repo.update({ newsId: post.newsId, feedName: post.feedName }, { 
+                editMessageId: undefined, 
+                forumsEditPostId: undefined, 
+                forumsNewsPostId: post.forumsNewsPostId });
+            await reaction.message.delete();
+            delete this.verifyMessageIdToPost[m.id];
+        };
 
     async deleteCacheMessage(messageId: string): Promise<void> {
         if(!this.aggregateChannelID)
@@ -249,6 +250,7 @@ export class NewsAggregator {
             if (ext != ".js")
                 continue;
 
+            // eslint-disable-next-line @typescript-eslint/no-var-requires
             const obj = require(path.resolve(aggregatorsPath, file)).default as IAggregator;
 
             if (obj)
@@ -283,7 +285,7 @@ export class NewsAggregator {
             }
 
             const collector = m.createReactionCollector(this.isVerifyReaction, { maxEmojis: 1 });
-            collector.on("collect", this.collectReaction);
+            collector.on("collect", this.collectorFor(collector));
             this.reactionCollectors[m.id] = collector;
             this.verifyMessageIdToPost[m.id] = msg;
         }