소스 검색

:white_check_mark: tell user if 404

Signed-off-by: LI JIAHAO <lijiahao99131@gmail.com>
LI JIAHAO 6 년 전
부모
커밋
0dd3a65854
4개의 변경된 파일72개의 추가작업 그리고 45개의 파일을 삭제
  1. 1 1
      README.md
  2. 34 20
      dist/twitter.js
  3. 5 5
      src/command.ts
  4. 32 19
      src/twitter.ts

+ 1 - 1
README.md

@@ -17,7 +17,7 @@ npm i -g cqhttp-twitter-bot
 它们是什么?  
 观察它们的文档:[https://cqhttp.cc/](https://cqhttp.cc/) [https://cqp.cc/t/15124](https://cqp.cc/t/15124)
 
-因为需要发图所以必须用 **CQ Pro** 才行。CQ Pro 是收费的,一个月 10 块左右。  
+因为需要发图所以必须用 **酷Q Pro** 才行。[酷Q Pro](https://cqp.cc/t/14901) 是收费的,一个月 10 块左右。  
 为什么要发图?  
 因为 twitter 有很多种,转推、回复、带图片的、带视频的。如果直接发文字的话体验很不好,估计也没人会需要吧。
 

+ 34 - 20
dist/twitter.js

@@ -34,43 +34,57 @@ class default_1 {
             logger.debug(`pulling feed ${lock.feed[lock.workon]}`);
             const promise = new Promise(resolve => {
                 let match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
+                let config;
+                let endpoint;
                 if (match) {
-                    const config = {
+                    config = {
                         owner_screen_name: match[1],
                         slug: match[2],
                     };
-                    const offset = lock.threads[lock.feed[lock.workon]].offset;
-                    if (offset > 0)
-                        config.since_id = offset;
-                    this.client.get('lists/statuses', config, (error, tweets, response) => {
-                        if (error) {
-                            logger.error(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
-                        }
-                        resolve(tweets);
-                    });
+                    endpoint = 'lists/statuses';
                 }
                 else {
                     match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
                     if (match) {
-                        const config = {
+                        config = {
                             screen_name: match[1],
                             exclude_replies: false,
                         };
-                        const offset = lock.threads[lock.feed[lock.workon]].offset;
-                        if (offset > 0)
-                            config.since_id = offset;
-                        this.client.get('statuses/user_timeline', config, (error, tweets, response) => {
-                            if (error) {
-                                logger.error(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
+                        endpoint = 'statuses/user_timeline';
+                    }
+                }
+                if (endpoint) {
+                    const offset = lock.threads[lock.feed[lock.workon]].offset;
+                    if (offset > 0)
+                        config.since_id = offset;
+                    this.client.get(endpoint, config, (error, tweets, response) => {
+                        if (error) {
+                            if (error instanceof Array && error.length > 0 && error[0].code === 34) {
+                                logger.warn(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
+                                lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
+                                    logger.info(`sending notfound message of ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
+                                    this.bot.bot('send_msg', {
+                                        message_type: subscriber.chatType,
+                                        user_id: subscriber.chatID,
+                                        group_id: subscriber.chatID,
+                                        discuss_id: subscriber.chatID,
+                                        message: `链接 ${lock.feed[lock.workon]} 指向的用户或列表不存在,请退订。`,
+                                    });
+                                });
+                            }
+                            else {
+                                logger.error(`unhandled error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
                             }
+                            resolve();
+                        }
+                        else
                             resolve(tweets);
-                        });
-                    }
+                    });
                 }
             });
             promise.then((tweets) => {
                 logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${lock.feed[lock.workon]}`);
-                if (tweets && tweets.length === 0) {
+                if (!tweets || tweets.length === 0) {
                     lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
                     return;
                 }

+ 5 - 5
src/command.ts

@@ -2,7 +2,7 @@ import * as fs from 'fs';
 import * as log4js from 'log4js';
 import * as path from 'path';
 
-import {relativeDate} from './datetime';
+import { relativeDate } from './datetime';
 
 const logger = log4js.getLogger('command');
 logger.level = (global as any).loglevel;
@@ -14,7 +14,7 @@ function parseLink(link: string): { link: string, match: string[] } | undefined
     return {
       link,
       match: [match[1], match[2]],
-    }
+    };
   }
   match = link.match(/twitter.com\/([^\/?#]+)/);
   if (match) {
@@ -22,7 +22,7 @@ function parseLink(link: string): { link: string, match: string[] } | undefined
     return {
       link,
       match: [match[1]],
-    }
+    };
   }
   match = link.match(/^([^\/?#]+)\/([^\/?#]+)$/);
   if (match) {
@@ -30,7 +30,7 @@ function parseLink(link: string): { link: string, match: string[] } | undefined
     return {
       link,
       match: [match[1], match[2]],
-    }
+    };
   }
   match = link.match(/^([^\/?#]+)$/);
   if (match) {
@@ -38,7 +38,7 @@ function parseLink(link: string): { link: string, match: string[] } | undefined
     return {
       link,
       match: [match[1]],
-    }
+    };
   }
   return undefined;
 }

+ 32 - 19
src/twitter.ts

@@ -69,41 +69,54 @@ export default class {
 
     const promise = new Promise(resolve => {
       let match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
+      let config: any;
+      let endpoint: string;
       if (match) {
-        const config: any = {
+        config = {
           owner_screen_name: match[1],
           slug: match[2],
         };
-        const offset = lock.threads[lock.feed[lock.workon]].offset;
-        if (offset > 0) config.since_id = offset;
-        this.client.get('lists/statuses', config, (error, tweets, response) => {
-          if (error) {
-            logger.error(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
-          }
-          resolve(tweets);
-        });
+        endpoint = 'lists/statuses';
       } else {
         match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
         if (match) {
-          const config: any = {
+          config = {
             screen_name: match[1],
             exclude_replies: false,
           };
-          const offset = lock.threads[lock.feed[lock.workon]].offset;
-          if (offset > 0) config.since_id = offset;
-          this.client.get('statuses/user_timeline', config, (error, tweets, response) => {
-            if (error) {
-              logger.error(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
-            }
-            resolve(tweets);
-          });
+          endpoint = 'statuses/user_timeline';
         }
       }
+
+      if (endpoint) {
+        const offset = lock.threads[lock.feed[lock.workon]].offset;
+        if (offset > 0) config.since_id = offset;
+        this.client.get(endpoint, config, (error, tweets, response) => {
+          if (error) {
+            if (error instanceof Array && error.length > 0 && error[0].code === 34) {
+              logger.warn(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
+              lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
+                logger.info(`sending notfound message of ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
+                this.bot.bot('send_msg', {
+                  message_type: subscriber.chatType,
+                  user_id: subscriber.chatID,
+                  group_id: subscriber.chatID,
+                  discuss_id: subscriber.chatID,
+                  message: `链接 ${lock.feed[lock.workon]} 指向的用户或列表不存在,请退订。`,
+                });
+              });
+            } else {
+              logger.error(`unhandled error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
+            }
+            resolve();
+          } else resolve(tweets);
+        });
+      }
     });
 
     promise.then((tweets: any) => {
       logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${lock.feed[lock.workon]}`);
-      if (tweets && tweets.length === 0) {
+      if (!tweets || tweets.length === 0) {
         lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
         return;
       }