Browse Source

Add error checking for news

ghorsington 5 years ago
parent
commit
4b9659f59f
1 changed files with 56 additions and 52 deletions
  1. 56 52
      bot/src/commands/forums_news_checker.ts

+ 56 - 52
bot/src/commands/forums_news_checker.ts

@@ -45,62 +45,66 @@ export class ForumsNewsChecker {
     }
 
     checkFeeds = async () => {
-        console.log(`Checking feeds on ${new Date().toISOString()}`);
-        let forumsNewsRepo = getRepository(PostedForumNewsItem);
-        let postVerifyMessageRepo = getRepository(PostVerifyMessage);
-
-        let forumThreads = await forumClient.getForumThreads(NEWS_FORUM_ID);
-
-        for (let thread of [...forumThreads.threads, ...forumThreads.sticky]) {
-            let firstPost = await forumClient.getPost(thread.first_post_id);
-
-            let contents = this.bbCodeToMarkdown(firstPost.message);
-            let itemObj = forumsNewsRepo.create({
-                id: thread.thread_id.toString(),
-                hash: sha1(firstPost.message),
-                verifyMessage: postVerifyMessageRepo.create({
-                    author: thread.username,
-                    link: `${FORUMS_DOMAIN}/index.php?threads/${thread.thread_id}/`,
-                    title: thread.title,
-                    text: `${contents.substr(0, Math.min(contents.length, PREVIEW_CHAR_LIMIT))}...`,
-                    isNew: true
-                })
-            });
-
-            let postItem = await forumsNewsRepo.findOne({
-                where: { id: itemObj.id },
-                relations: ["verifyMessage"]
-            });
-
-            if (postItem) {
-
-                if (process.env.IGNORE_CHANGED_NEWS === "TRUE") {
-                    await forumsNewsRepo.update({
-                        id: postItem.id
-                    }, {
-                            hash: itemObj.hash
-                        });
-                    continue;
+        try {
+            console.log(`Checking feeds on ${new Date().toISOString()}`);
+            let forumsNewsRepo = getRepository(PostedForumNewsItem);
+            let postVerifyMessageRepo = getRepository(PostVerifyMessage);
+
+            let forumThreads = await forumClient.getForumThreads(NEWS_FORUM_ID);
+
+            for (let thread of [...forumThreads.threads, ...forumThreads.sticky]) {
+                let firstPost = await forumClient.getPost(thread.first_post_id);
+
+                let contents = this.bbCodeToMarkdown(firstPost.message);
+                let itemObj = forumsNewsRepo.create({
+                    id: thread.thread_id.toString(),
+                    hash: sha1(firstPost.message),
+                    verifyMessage: postVerifyMessageRepo.create({
+                        author: thread.username,
+                        link: `${FORUMS_DOMAIN}/index.php?threads/${thread.thread_id}/`,
+                        title: thread.title,
+                        text: `${contents.substr(0, Math.min(contents.length, PREVIEW_CHAR_LIMIT))}...`,
+                        isNew: true
+                    })
+                });
+
+                let postItem = await forumsNewsRepo.findOne({
+                    where: { id: itemObj.id },
+                    relations: ["verifyMessage"]
+                });
+
+                if (postItem) {
+
+                    if (process.env.IGNORE_CHANGED_NEWS === "TRUE") {
+                        await forumsNewsRepo.update({
+                            id: postItem.id
+                        }, {
+                                hash: itemObj.hash
+                            });
+                        continue;
+                    }
+
+                    // Add message ID to mark for edit
+                    if (postItem.hash != itemObj.hash) {
+                        let newHash = itemObj.hash;
+                        if (!postItem.verifyMessage)
+                            postItem.verifyMessage = itemObj.verifyMessage;
+
+                        itemObj = postItem;
+                        itemObj.verifyMessage.isNew = false;
+                        itemObj.hash = newHash;
+                    }
+                    else
+                        continue;
                 }
 
-                // Add message ID to mark for edit
-                if (postItem.hash != itemObj.hash) {
-                    let newHash = itemObj.hash;
-                    if (!postItem.verifyMessage)
-                        postItem.verifyMessage = itemObj.verifyMessage;
-
-                    itemObj = postItem;
-                    itemObj.verifyMessage.isNew = false;
-                    itemObj.hash = newHash;
-                }
+                if (!this.shouldVerify() || firstPost.user_id == this.botUserId)
+                    await this.sendNews(itemObj);
                 else
-                    continue;
+                    await this.addVerifyMessage(itemObj);
             }
-
-            if (!this.shouldVerify() || firstPost.user_id == this.botUserId)
-                await this.sendNews(itemObj);
-            else
-                await this.addVerifyMessage(itemObj);
+        } catch (err) {
+            console.log(`Failed to check forums because ${err}`);
         }
     }