Browse Source

:bug: bugfix

Signed-off-by: LI JIAHAO <lijiahao99131@gmail.com>
LI JIAHAO 6 years ago
parent
commit
6393008b41
7 changed files with 55 additions and 115 deletions
  1. 6 0
      dist/cqhttp.js
  2. 0 85
      dist/qq.js
  3. 19 15
      dist/twitter.js
  4. 1 0
      dist/webshot.js
  5. 8 0
      src/cqhttp.ts
  6. 19 14
      src/twitter.ts
  7. 2 1
      src/webshot.ts

+ 6 - 0
dist/cqhttp.js

@@ -62,6 +62,12 @@ class default_1 {
 /twitter_unsubscribe [链接] - 退订 Twitter 搬运`;
                 }
             });
+            this.bot.on('api.send.pre', (type, apiRequest) => {
+                logger.info(`sending request ${type}: ${JSON.stringify(apiRequest)}`);
+            });
+            this.bot.on('api.send.post', (type) => {
+                logger.info(`sent request ${type}`);
+            });
         };
         this.connect = () => {
             this.initWebsocket();

+ 0 - 85
dist/qq.js

@@ -1,85 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-const CQWebsocket = require("cq-websocket");
-const log4js = require("log4js");
-const helper_1 = require("./helper");
-const logger = log4js.getLogger('cq-websocket');
-logger.level = 'info';
-class default_1 {
-    constructor(opt) {
-        this.retryInterval = 1000;
-        this.initWebsocket = () => {
-            this.bot = new CQWebsocket({
-                access_token: this.botInfo.access_token,
-                enableAPI: true,
-                enableEvent: true,
-                host: this.botInfo.host,
-                port: this.botInfo.port,
-            });
-            this.bot.on('socket.connect', () => {
-                logger.info('websocket connected');
-                this.retryInterval = 1000;
-            });
-            this.bot.on('socket.close', () => {
-                logger.error('websocket closed');
-                this.reconnect();
-            });
-            this.bot.on('socket.error', () => {
-                logger.error('websocket connect error');
-                this.reconnect();
-            });
-            this.bot.on('message', (e, context) => {
-                e.cancel();
-                const chat = {
-                    chatType: context.message_type,
-                    chatID: 0,
-                };
-                switch (context.message_type) {
-                    case "private" /* Private */:
-                        chat.chatID = context.user_id;
-                        break;
-                    case "group" /* Group */:
-                        chat.chatID = context.group_id;
-                        break;
-                    case "discuss" /* Discuss */:
-                        chat.chatID = context.discuss_id;
-                }
-                const cmdObj = helper_1.default(context.raw_message);
-                switch (cmdObj.cmd) {
-                    case 'twitter_sub':
-                    case 'twitter_subscribe':
-                        return this.botInfo.sub(chat, cmdObj.args);
-                    case 'twitter_unsub':
-                    case 'twitter_unsubscribe':
-                        return this.botInfo.unsub(chat, cmdObj.args);
-                    case 'ping':
-                    case 'twitter':
-                        return this.botInfo.list(chat, cmdObj.args);
-                    case 'help':
-                        return `推特搬运机器人:
-/twitter - 查询当前聊天中的订阅
-/twitter_subscribe [链接] - 订阅 Twitter 搬运
-/twitter_unsubscribe [链接] - 退订 Twitter 搬运`;
-                }
-            });
-        };
-        this.connect = () => {
-            this.initWebsocket();
-            logger.warn('connecting to websocket...');
-            this.bot.connect();
-        };
-        this.reconnect = () => {
-            this.retryInterval *= 2;
-            if (this.retryInterval > 300000)
-                this.retryInterval = 300000;
-            logger.info(`retrying in ${this.retryInterval / 1000}s...`);
-            setTimeout(() => {
-                logger.warn('reconnecting to websocket...');
-                this.connect();
-            }, this.retryInterval);
-        };
-        logger.info(`init cqwebsocket for ${opt.host}:${opt.port}, with access_token ${opt.access_token}`);
-        this.botInfo = opt;
-    }
-}
-exports.default = default_1;

+ 19 - 15
dist/twitter.js

@@ -25,6 +25,7 @@ class default_1 {
                 !lock.threads[lock.feed[lock.workon]].subscribers ||
                 lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
                 logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
+                delete lock.threads[lock.feed[lock.workon]];
                 lock.feed.splice(lock.workon, 1);
                 fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
                 this.work();
@@ -63,22 +64,25 @@ class default_1 {
             promise.then((tweets) => {
                 if (tweets.length === 0)
                     return;
-                if (lock.threads[lock.feed[lock.workon]].offset !== -1) {
-                    if (lock.threads[lock.feed[lock.workon]].offset === 0)
-                        tweets.splice(1);
-                    webshot_1.default(tweets, msg => {
-                        lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
-                            this.bot.bot('send_msg', {
-                                message_type: subscriber.chatType,
-                                user_id: subscriber.chatID,
-                                group_id: subscriber.chatID,
-                                discuss_id: subscriber.chatID,
-                                message: msg,
-                            });
-                        });
-                    }, this.webshotDelay);
+                if (lock.threads[lock.feed[lock.workon]].offset === -1) {
+                    lock.threads[lock.feed[lock.workon]].offset = +tweets[0].id_str;
+                    return;
                 }
-                lock.threads[lock.feed[lock.workon]].offset = tweets[0].id;
+                if (lock.threads[lock.feed[lock.workon]].offset === 0)
+                    tweets.splice(1);
+                return webshot_1.default(tweets, msg => {
+                    lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
+                        logger.info(`pushing data of thread ${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: msg,
+                        });
+                    });
+                }, this.webshotDelay)
+                    .then(() => lock.threads[lock.feed[lock.workon]].offset = +tweets[0].id_str);
             })
                 .then(() => {
                 lock.workon++;

+ 1 - 0
dist/webshot.js

@@ -99,5 +99,6 @@ function default_1(tweets, callback, webshotDelay) {
         // TODO: Translate
         promise.then(() => callback(cqstr));
     });
+    return promise;
 }
 exports.default = default_1;

+ 8 - 0
src/cqhttp.ts

@@ -79,6 +79,14 @@ export default class {
 /twitter_unsubscribe [链接] - 退订 Twitter 搬运`;
       }
     });
+
+    this.bot.on('api.send.pre', (type, apiRequest) => {
+      logger.info(`sending request ${type}: ${JSON.stringify(apiRequest)}`);
+    });
+
+    this.bot.on('api.send.post', (type) => {
+      logger.info(`sent request ${type}`);
+    });
 }
 
   public connect = () => {

+ 19 - 14
src/twitter.ts

@@ -58,6 +58,7 @@ export default class {
       !lock.threads[lock.feed[lock.workon]].subscribers ||
       lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
       logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
+      delete lock.threads[lock.feed[lock.workon]];
       lock.feed.splice(lock.workon, 1);
       fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
       this.work();
@@ -94,21 +95,25 @@ export default class {
 
     promise.then((tweets: any) => {
       if (tweets.length === 0) return;
-      if (lock.threads[lock.feed[lock.workon]].offset !== -1) {
-        if (lock.threads[lock.feed[lock.workon]].offset === 0) tweets.splice(1);
-        webshot(tweets, msg => {
-          lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
-            this.bot.bot('send_msg', {
-              message_type: subscriber.chatType,
-              user_id: subscriber.chatID,
-              group_id: subscriber.chatID,
-              discuss_id: subscriber.chatID,
-              message: msg,
-            });
-          });
-        }, this.webshotDelay);
+      if (lock.threads[lock.feed[lock.workon]].offset === -1) {
+        lock.threads[lock.feed[lock.workon]].offset = +tweets[0].id_str;
+        return;
       }
-      lock.threads[lock.feed[lock.workon]].offset = tweets[0].id;
+      if (lock.threads[lock.feed[lock.workon]].offset === 0) tweets.splice(1);
+      return webshot(tweets, msg => {
+        lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
+          logger.info(`pushing data of thread ${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: msg,
+          });
+        });
+      }, this.webshotDelay)
+        .then(() => lock.threads[lock.feed[lock.workon]].offset = +tweets[0].id_str);
+
     })
       .then(() => {
         lock.workon++;

+ 2 - 1
src/webshot.ts

@@ -73,7 +73,7 @@ function fetchImage(url: string): Promise<string> {
   });
 }
 
-export default function (tweets, callback, webshotDelay: number) {
+export default function (tweets, callback, webshotDelay: number): Promise<void> {
   let promise = new Promise<void>(resolve => {
     resolve();
   });
@@ -95,4 +95,5 @@ export default function (tweets, callback, webshotDelay: number) {
     // TODO: Translate
     promise.then(() => callback(cqstr));
   });
+  return promise;
 }