Browse Source

Merge branch 'koishi-guild' into mediaonly-koishi-guild

Mike L 2 years ago
parent
commit
382c30c3f7
2 changed files with 53 additions and 12 deletions
  1. 21 4
      dist/koishi.js
  2. 32 8
      src/koishi.ts

+ 21 - 4
dist/koishi.js

@@ -15,6 +15,22 @@ const plugin_adapter_onebot_1 = require("@koishijs/plugin-adapter-onebot");
 const command_1 = require("./command");
 const loggers_1 = require("./loggers");
 const logger = (0, loggers_1.getLogger)('qqbot');
+const batchExec = (executer, ...[chat, args, reply]) => {
+    let combinedMsg = '';
+    let promise = Promise.resolve();
+    args.forEach(arg => {
+        promise = promise.then(() => new Promise(resolve => {
+            executer(chat, [arg], (msg) => {
+                combinedMsg += msg + '\n';
+                resolve();
+            });
+        }));
+    });
+    promise.then(() => {
+        if (combinedMsg)
+            reply(combinedMsg.slice(0, -1));
+    });
+};
 exports.Message = {
     Image: koishi_1.segment.image,
     Video: koishi_1.segment.video,
@@ -203,10 +219,10 @@ class default_1 {
                         (0, command_1.query)(chat, cmdObj.args, reply);
                         break;
                     case 'twipic_sub':
-                        this.botInfo.sub(chat, cmdObj.args, reply);
+                        batchExec(this.botInfo.sub, chat, cmdObj.args, reply);
                         break;
                     case 'twipic_unsub':
-                        this.botInfo.unsub(chat, cmdObj.args, reply);
+                        batchExec(this.botInfo.unsub, chat, cmdObj.args, reply);
                         break;
                     case 'twipic_unsuball':
                         this.botInfo.unsubAll(chat, cmdObj.args, reply);
@@ -218,8 +234,9 @@ class default_1 {
                         if (cmdObj.args[0] === 'twipic') {
                             reply(`推特媒体推文搬运机器人:
 /twipic_listsub - 查询当前聊天中的媒体推文订阅
-/twipic_sub〈链接|用户名〉- 订阅 Twitter 媒体推文搬运
-/twipic_unsub〈链接|用户名〉- 退订 Twitter 媒体推文搬运
+/twipic_sub〈链接|用户名〉[〈链接|用户名〉...] - 订阅一个或多个媒体推文搬运
+/twipic_unsub〈链接|用户名〉[〈链接|用户名〉...] - 退订一个或多个媒体推文搬运
+/twipic_unsuball - 退订当前聊天中全部的媒体推文搬运
 /twipic_view〈链接|表达式〉[{force|refresh}={on|off}] - 查看推文(可选强制重新载入)
 /twipic_resendlast〈用户名〉- 强制重发该用户最后一条媒体推文
 /twipic_query〈链接|用户名〉[参数列表...] - 查询时间线(详见 /help twipic_query)\

+ 32 - 8
src/koishi.ts

@@ -6,15 +6,38 @@ import { getLogger } from './loggers';
 
 const logger = getLogger('qqbot');
 
+type ExecuterParams = [
+  chat: IChat, args: string[], replyfn: (msg: string) => any
+];
+
 interface IQQProps {
   access_token: string;
   host: string;
   port: number;
   bot_id: number;
-  list(chat: IChat, args: string[], replyfn: (msg: string) => any): void;
-  sub(chat: IChat, args: string[], replyfn: (msg: string) => any): void;
-  unsub(chat: IChat, args: string[], replyfn: (msg: string) => any): void;
-  unsubAll(chat: IChat, args: string[], replyfn: (msg: string) => any): void;
+  list(..._: ExecuterParams): void;
+  sub(..._: ExecuterParams): void;
+  unsub(..._: ExecuterParams): void;
+  unsubAll(..._: ExecuterParams): void;
+}
+
+const batchExec = (
+  executer: (..._: ExecuterParams) => void,
+  ...[chat, args, reply]: ExecuterParams
+) => {
+  let combinedMsg = '';
+  let promise = Promise.resolve();
+  args.forEach(arg => {
+    promise = promise.then(() => new Promise(resolve => {
+      executer(chat, [arg], (msg: string) => {
+        combinedMsg += msg + '\n';
+        resolve();
+      });
+    }));
+  });
+  promise.then(() => {
+    if (combinedMsg) reply(combinedMsg.slice(0, -1));
+  });
 }
 
 export const Message = {
@@ -228,10 +251,10 @@ export default class {
           query(chat, cmdObj.args, reply);
           break;
         case 'twipic_sub':
-          this.botInfo.sub(chat, cmdObj.args, reply);
+          batchExec(this.botInfo.sub, chat, cmdObj.args, reply);
           break;
         case 'twipic_unsub':
-          this.botInfo.unsub(chat, cmdObj.args, reply);
+          batchExec(this.botInfo.unsub, chat, cmdObj.args, reply);
           break;
         case 'twipic_unsuball':
           this.botInfo.unsubAll(chat, cmdObj.args, reply);
@@ -243,8 +266,9 @@ export default class {
           if (cmdObj.args[0] === 'twipic') {
             reply(`推特媒体推文搬运机器人:
 /twipic_listsub - 查询当前聊天中的媒体推文订阅
-/twipic_sub〈链接|用户名〉- 订阅 Twitter 媒体推文搬运
-/twipic_unsub〈链接|用户名〉- 退订 Twitter 媒体推文搬运
+/twipic_sub〈链接|用户名〉[〈链接|用户名〉...] - 订阅一个或多个媒体推文搬运
+/twipic_unsub〈链接|用户名〉[〈链接|用户名〉...] - 退订一个或多个媒体推文搬运
+/twipic_unsuball - 退订当前聊天中全部的媒体推文搬运
 /twipic_view〈链接|表达式〉[{force|refresh}={on|off}] - 查看推文(可选强制重新载入)
 /twipic_resendlast〈用户名〉- 强制重发该用户最后一条媒体推文
 /twipic_query〈链接|用户名〉[参数列表...] - 查询时间线(详见 /help twipic_query)\