Browse Source

readd reconnect, undo separate image, fix temp suffix

Mike L 4 years ago
parent
commit
3adc474289
4 changed files with 29 additions and 7 deletions
  1. 13 2
      dist/koishi.js
  2. 1 1
      dist/webshot.js
  3. 14 3
      src/koishi.ts
  4. 1 1
      src/webshot.ts

+ 13 - 2
dist/koishi.js

@@ -24,7 +24,7 @@ exports.Message = {
     ellipseBase64: (msg) => msg.replace(/(?<=\[CQ:.*base64:\/\/).*?(,|\])/g, '...$1'),
     separateAttachment: (msg) => {
         const attachments = [];
-        const message = msg.replace(/\[CQ:(image|video|record),.*?\]/g, code => {
+        const message = msg.replace(/\[CQ:(video|record),.*?\]/g, code => {
             attachments.push(code);
             return '';
         });
@@ -174,9 +174,20 @@ count 与 since/until 并用时,取二者中实际查询结果较少者
                 }
             }), true);
         };
+        this.listen = (logMsg = 'connecting to bot provider...') => __awaiter(this, void 0, void 0, function* () {
+            logger.warn(logMsg);
+            try {
+                yield this.app.start();
+            }
+            catch (err) {
+                logger.error(`error connecting to bot provider at ${this.app.options.server}, will retry in 2.5s...`);
+                yield koishi_1.sleep(2500);
+                yield this.listen('retry connecting...');
+            }
+        });
         this.connect = () => __awaiter(this, void 0, void 0, function* () {
             this.initBot();
-            yield this.app.start();
+            yield this.listen();
             this.bot = this.app.getBot('onebot');
         });
         logger.warn(`Initialized koishi on ${opt.host}:${opt.port} with access_token ${opt.access_token}`);

+ 1 - 1
dist/webshot.js

@@ -250,7 +250,7 @@ class Webshot extends CallableInstance {
         }).then(data => {
             var _a;
             return (ext => {
-                const mediaTempFilePath = temp.path({ suffix: ext });
+                const mediaTempFilePath = temp.path({ suffix: `.${ext}` });
                 data.pipe(fs_1.createWriteStream(mediaTempFilePath));
                 const path = `file://${mediaTempFilePath}`;
                 switch (ext) {

+ 14 - 3
src/koishi.ts

@@ -1,4 +1,4 @@
-import { App, Bot, segment, Session } from 'koishi';
+import { App, Bot, segment, Session, sleep } from 'koishi';
 import 'koishi-adapter-onebot';
 
 import { parseCmd, query, view } from './command';
@@ -28,7 +28,7 @@ export const Message = {
   ellipseBase64: (msg: string) => msg.replace(/(?<=\[CQ:.*base64:\/\/).*?(,|\])/g, '...$1'),
   separateAttachment: (msg: string) => {
     const attachments: string[] = [];
-    const message = msg.replace(/\[CQ:(image|video|record),.*?\]/g, code => {
+    const message = msg.replace(/\[CQ:(video|record),.*?\]/g, code => {
       attachments.push(code);
       return '';
     });
@@ -193,9 +193,20 @@ count 与 since/until 并用时,取二者中实际查询结果较少者
     }, true);
   };
 
+  private listen = async (logMsg = 'connecting to bot provider...'): Promise<void> => {
+    logger.warn(logMsg);
+    try {
+      await this.app.start();
+    } catch (err) {
+      logger.error(`error connecting to bot provider at ${this.app.options.server}, will retry in 2.5s...`);
+      await sleep(2500);
+      await this.listen('retry connecting...');
+    }
+  };
+
   public connect = async () => {
     this.initBot();
-    await this.app.start();
+    await this.listen();
     this.bot = this.app.getBot('onebot');
   };
 

+ 1 - 1
src/webshot.ts

@@ -278,7 +278,7 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
     });
   }).then(data =>
     (ext => {
-      const mediaTempFilePath = temp.path({suffix: ext});
+      const mediaTempFilePath = temp.path({suffix: `.${ext}`});
       data.pipe(createWriteStream(mediaTempFilePath));
       const path = `file://${mediaTempFilePath}`;
       switch (ext) {