浏览代码

add batch unsub

Mike L 3 年之前
父节点
当前提交
558c048de9
共有 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";
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.query = exports.view = exports.unsub = exports.list = exports.sub = exports.parseCmd = void 0;
+exports.query = exports.view = exports.unsubAll = exports.unsub = exports.list = exports.sub = exports.parseCmd = void 0;
 const fs = require("fs");
 const fs = require("fs");
 const path = require("path");
 const path = require("path");
 const datetime_1 = require("./datetime");
 const datetime_1 = require("./datetime");
@@ -117,6 +117,21 @@ https://twitter.com/TomoyoKurosawa/status/1294613494860361729`);
     });
     });
 }
 }
 exports.sub = sub;
 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(`已为此聊天退订所有推特链接。`);
+}
+exports.unsubAll = unsubAll;
 function unsub(chat, args, reply, lock, lockfile) {
 function unsub(chat, args, reply, lock, lockfile) {
     if (chat.chatType === "temp") {
     if (chat.chatType === "temp") {
         return reply('请先添加机器人为好友。');
         return reply('请先添加机器人为好友。');

+ 4 - 0
dist/koishi.js

@@ -193,6 +193,10 @@ class default_1 {
                     case 'twitter_unsubscribe':
                     case 'twitter_unsubscribe':
                         this.botInfo.unsub(chat, cmdObj.args, reply);
                         this.botInfo.unsub(chat, cmdObj.args, reply);
                         break;
                         break;
+                    case 'twitter_unsuball':
+                    case 'bye':
+                        this.botInfo.unsubAll(chat, cmdObj.args, reply);
+                        break;
                     case 'ping':
                     case 'ping':
                     case 'twitter':
                     case 'twitter':
                         this.botInfo.list(chat, cmdObj.args, reply);
                         this.botInfo.list(chat, cmdObj.args, reply);

+ 1 - 0
dist/main.js

@@ -117,6 +117,7 @@ const qq = new koishi_1.default({
     list: (c, a, cb) => command_1.list(c, a, cb, lock),
     list: (c, a, cb) => command_1.list(c, a, cb, lock),
     sub: (c, a, cb) => command_1.sub(c, a, cb, lock, config.lockfile),
     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),
     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({
 const worker = new twitter_1.default({
     consumerKey: config.twitter_consumer_key,
     consumerKey: config.twitter_consumer_key,

+ 17 - 1
src/command.ts

@@ -125,6 +125,22 @@ https://twitter.com/TomoyoKurosawa/status/1294613494860361729`);
   });
   });
 }
 }
 
 
+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(`已为此聊天退订所有推特链接。`);
+}
+
 function unsub(chat: IChat, args: string[], reply: (msg: string) => any,
 function unsub(chat: IChat, args: string[], reply: (msg: string) => any,
   lock: ILock, lockfile: string
   lock: ILock, lockfile: string
 ): void {
 ): void {
@@ -220,4 +236,4 @@ function query(chat: IChat, args: string[], reply: (msg: string) => any): void {
   }
   }
 }
 }
 
 
-export { parseCmd, sub, list, unsub, view, query };
+export { parseCmd, sub, list, unsub, unsubAll, view, query };

+ 5 - 0
src/koishi.ts

@@ -18,6 +18,7 @@ interface IQQProps {
   list(chat: IChat, args: string[], replyfn: (msg: string) => any): void;
   list(chat: IChat, args: string[], replyfn: (msg: string) => any): void;
   sub(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;
   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>) =>
 const cqUrlFix = (factory: segment.Factory<string | ArrayBuffer | Buffer>) =>
@@ -212,6 +213,10 @@ export default class {
         case 'twitter_unsubscribe':
         case 'twitter_unsubscribe':
           this.botInfo.unsub(chat, cmdObj.args, reply);
           this.botInfo.unsub(chat, cmdObj.args, reply);
           break;
           break;
+        case 'twitter_unsuball':
+        case 'bye':
+          this.botInfo.unsubAll(chat, cmdObj.args, reply);
+          break;
         case 'ping':
         case 'ping':
         case 'twitter':
         case 'twitter':
           this.botInfo.list(chat, cmdObj.args, reply);
           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 commandLineUsage from 'command-line-usage';
 
 
 import * as exampleConfig from '../config.example.json';
 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 { getLogger, setLogLevels } from './loggers';
 import QQBot from './koishi';
 import QQBot from './koishi';
 import Worker from './twitter';
 import Worker from './twitter';
@@ -129,6 +129,7 @@ const qq = new QQBot({
   list: (c, a, cb) => list(c, a, cb, lock),
   list: (c, a, cb) => list(c, a, cb, lock),
   sub: (c, a, cb) => sub(c, a, cb, lock, config.lockfile),
   sub: (c, a, cb) => sub(c, a, cb, lock, config.lockfile),
   unsub: (c, a, cb) => unsub(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({
 const worker = new Worker({