소스 검색

add batch unsub

# Conflicts:
#	dist/command.js
#	src/command.ts
Mike L 3 년 전
부모
커밋
e90a243514
6개의 변경된 파일45개의 추가작업 그리고 3개의 파일을 삭제
  1. 16 1
      dist/command.js
  2. 4 0
      dist/koishi.js
  3. 1 0
      dist/main.js
  4. 17 1
      src/command.ts
  5. 5 0
      src/koishi.ts
  6. 2 1
      src/main.ts

+ 16 - 1
dist/command.js

@@ -1,6 +1,6 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.view = exports.unsub = exports.list = exports.sub = exports.parseCmd = void 0;
+exports.view = exports.unsubAll = exports.unsub = exports.list = exports.sub = exports.parseCmd = void 0;
 const fs = require("fs");
 const path = require("path");
 const datetime_1 = require("./datetime");
@@ -108,6 +108,21 @@ https://www.instagram.com/p/B6GHRSmgV-7/`);
     }
 }
 exports.sub = sub;
+function unsubAll(chat, args, reply, lock, lockfile) {
+    if (chat.chatType === "temp") {
+        return reply('请先添加机器人为好友。');
+    }
+    Object.entries(lock.threads).forEach(([link, { subscribers }]) => {
+        const index = subscribers.indexOf(chat);
+        if (index === -1)
+            return;
+        subscribers.splice(index, 1);
+        fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
+        logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
+    });
+    return reply(`已为此聊天退订所有 Instagram 链接。`);
+}
+exports.unsubAll = unsubAll;
 function unsub(chat, args, reply, lock, lockfile) {
     var _a;
     if (chat.chatType === "temp") {

+ 4 - 0
dist/koishi.js

@@ -189,6 +189,10 @@ class default_1 {
                     case 'instagram_unsubscribe':
                         this.botInfo.unsub(chat, cmdObj.args, reply);
                         break;
+                    case 'instagram_unsuball':
+                    case 'bye':
+                        this.botInfo.unsubAll(chat, cmdObj.args, reply);
+                        break;
                     case 'ping':
                     case 'instagram':
                         this.botInfo.list(chat, cmdObj.args, reply);

+ 1 - 0
dist/main.js

@@ -126,6 +126,7 @@ const qq = new koishi_1.default({
     list: (c, a, cb) => command_1.list(c, a, cb, lock),
     sub: (c, a, cb) => command_1.sub(c, a, cb, lock, config.lockfile),
     unsub: (c, a, cb) => command_1.unsub(c, a, cb, lock, config.lockfile),
+    unsubAll: (c, a, cb) => command_1.unsubAll(c, a, cb, lock, config.lockfile),
 });
 const worker = new twitter_1.default({
     sessionLockfile: config.ig_session_lockfile,

+ 17 - 1
src/command.ts

@@ -113,6 +113,22 @@ https://www.instagram.com/p/B6GHRSmgV-7/`);
   }
 }
 
+function unsubAll(chat: IChat, args: string[], reply: (msg: string) => any,
+  lock: ILock, lockfile: string
+): void {
+  if (chat.chatType === ChatType.Temp) {
+    return reply('请先添加机器人为好友。');
+  }
+  Object.entries(lock.threads).forEach(([link, {subscribers}]) => {
+    const index = subscribers.indexOf(chat);
+    if (index === -1) return;
+    subscribers.splice(index, 1);
+    fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
+    logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
+  });
+  return reply(`已为此聊天退订所有 Instagram 链接。`);
+}
+
 function unsub(chat: IChat, args: string[], reply: (msg: string) => any,
   lock: ILock, lockfile: string
 ): void {
@@ -164,4 +180,4 @@ function view(chat: IChat, args: string[], reply: (msg: string) => any): void {
   }
 }
 
-export { parseCmd, sub, list, unsub, view };
+export { parseCmd, sub, list, unsub, unsubAll, view };

+ 5 - 0
src/koishi.ts

@@ -18,6 +18,7 @@ interface IQQProps {
   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;
 }
 
 const cqUrlFix = (factory: segment.Factory<string | ArrayBuffer | Buffer>) =>
@@ -208,6 +209,10 @@ export default class {
         case 'instagram_unsubscribe':
           this.botInfo.unsub(chat, cmdObj.args, reply);
           break;
+        case 'instagram_unsuball':
+        case 'bye':
+          this.botInfo.unsubAll(chat, cmdObj.args, reply);
+          break;
         case 'ping':
         case 'instagram':
           this.botInfo.list(chat, cmdObj.args, reply);

+ 2 - 1
src/main.ts

@@ -6,7 +6,7 @@ import * as path from 'path';
 import * as commandLineUsage from 'command-line-usage';
 
 import * as exampleConfig from '../config.example.json';
-import { list, sub, unsub } from './command';
+import { list, sub, unsub, unsubAll } from './command';
 import { getLogger, setLogLevels } from './loggers';
 import QQBot from './koishi';
 import Worker from './twitter';
@@ -141,6 +141,7 @@ const qq = new QQBot({
   list: (c, a, cb) => list(c, a, cb, lock),
   sub: (c, a, cb) => sub(c, a, cb, lock, config.lockfile),
   unsub: (c, a, cb) => unsub(c, a, cb, lock, config.lockfile),
+  unsubAll: (c, a, cb) => unsubAll(c, a, cb, lock, config.lockfile),
 });
 
 const worker = new Worker({