| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 | 
							- "use strict";
 
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
 
-     function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
 
-     return new (P || (P = Promise))(function (resolve, reject) {
 
-         function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
 
-         function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
 
-         function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
 
-         step((generator = generator.apply(thisArg, _arguments || [])).next());
 
-     });
 
- };
 
- Object.defineProperty(exports, "__esModule", { value: true });
 
- exports.Message = void 0;
 
- const koishi_1 = require("koishi");
 
- require("koishi-adapter-onebot");
 
- const command_1 = require("./command");
 
- const loggers_1 = require("./loggers");
 
- const utils_1 = require("./utils");
 
- const logger = loggers_1.getLogger('qqbot');
 
- const cqUrlFix = (factory) => (...args) => factory(...args).replace(/(?<=\[CQ:.*)url=(?=(base64|file|https?):\/\/)/, 'file=');
 
- exports.Message = {
 
-     Image: cqUrlFix(koishi_1.segment.image),
 
-     Video: cqUrlFix(koishi_1.segment.video),
 
-     Voice: cqUrlFix(koishi_1.segment.audio),
 
-     ellipseBase64: (msg) => msg.replace(/(?<=\[CQ:.*base64:\/\/).*?(,|\])/g, '...$1'),
 
-     separateAttachment: (msg) => {
 
-         const attachments = [];
 
-         const message = msg.replace(/\[CQ:(video|record),.*?\]/g, code => {
 
-             attachments.push(code);
 
-             return '';
 
-         });
 
-         return { message, attachments };
 
-     },
 
- };
 
- class default_1 {
 
-     constructor(opt) {
 
-         this.messageQueues = {};
 
-         this.next = (type, id) => {
 
-             const queue = this.messageQueues[`${type}:${id}`];
 
-             if (queue && queue.length) {
 
-                 queue[0]().then(() => {
 
-                     queue.shift();
 
-                     if (!queue.length)
 
-                         delete this.messageQueues[`${type}:${id}`];
 
-                     else
 
-                         this.next(type, id);
 
-                 });
 
-             }
 
-         };
 
-         this.enqueue = (type, id, resolver) => {
 
-             var _a, _b;
 
-             let wasEmpty = false;
 
-             const queue = (_a = this.messageQueues)[_b = `${type}:${id}`] || (_a[_b] = (() => { wasEmpty = true; return []; })());
 
-             queue.push(() => koishi_1.sleep(200).then(resolver));
 
-             logger.debug(`no. of message currently queued for ${type}:${id}: ${queue.length}`);
 
-             if (wasEmpty)
 
-                 this.next(type, id);
 
-         };
 
-         this.getChat = (session) => __awaiter(this, void 0, void 0, function* () {
 
-             switch (session.subtype) {
 
-                 case 'private':
 
-                     if (session.groupId) {
 
-                         const friendList = yield session.bot.getFriendList();
 
-                         if (!friendList.some(friendItem => friendItem.userId === session.userId)) {
 
-                             return {
 
-                                 chatID: {
 
-                                     qq: Number(session.userId),
 
-                                     group: Number(session.groupId),
 
-                                 },
 
-                                 chatType: "temp",
 
-                             };
 
-                         }
 
-                     }
 
-                     return {
 
-                         chatID: Number(session.userId),
 
-                         chatType: "private",
 
-                     };
 
-                 case 'group':
 
-                     return {
 
-                         chatID: Number(session.groupId),
 
-                         chatType: "group",
 
-                     };
 
-             }
 
-         });
 
-         this.sendToGroup = (groupID, message) => new Promise(resolve => {
 
-             this.enqueue('group', groupID, () => this.bot.sendMessage(groupID, message).then(resolve));
 
-         });
 
-         this.sendToUser = (userID, message) => new Promise(resolve => {
 
-             this.enqueue('private', userID, () => this.bot.sendPrivateMessage(userID, message).then(resolve));
 
-         });
 
-         this.sendTo = (subscriber, messageChain) => utils_1.chainPromises((splitted => [splitted.message, ...splitted.attachments])(exports.Message.separateAttachment(messageChain)).map(msg => {
 
-             switch (subscriber.chatType) {
 
-                 case 'group':
 
-                     return this.sendToGroup(subscriber.chatID.toString(), msg);
 
-                 case 'private':
 
-                     return this.sendToUser(subscriber.chatID.toString(), msg);
 
-                 case 'temp':
 
-                     return this.sendToUser(subscriber.chatID.qq.toString(), msg);
 
-             }
 
-         }))
 
-             .then(response => {
 
-             if (response === undefined)
 
-                 return;
 
-             logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response: ${response}`);
 
-         })
 
-             .catch(reason => {
 
-             reason = exports.Message.ellipseBase64(reason);
 
-             logger.error(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`);
 
-             throw Error(reason);
 
-         });
 
-         this.initBot = () => {
 
-             this.app = new koishi_1.App({
 
-                 type: 'onebot',
 
-                 server: `ws://${this.botInfo.host}:${this.botInfo.port}`,
 
-                 selfId: this.botInfo.bot_id.toString(),
 
-                 token: this.botInfo.access_token,
 
-                 axiosConfig: {
 
-                     maxContentLength: Infinity,
 
-                 },
 
-                 processMessage: msg => msg.trim(),
 
-             });
 
-             this.app.on('friend-request', (session) => __awaiter(this, void 0, void 0, function* () {
 
-                 const userString = `${session.username}(${session.userId})`;
 
-                 const groupString = `${session.groupName}(${session.groupId})`;
 
-                 logger.debug(`detected new friend request event: ${userString}`);
 
-                 return session.bot.getGroupList().then(groupList => {
 
-                     if (groupList.some(groupItem => groupItem.groupId === session.groupId)) {
 
-                         session.bot.handleFriendRequest(session.messageId, true);
 
-                         return logger.info(`accepted friend request from ${userString} (from group ${groupString})`);
 
-                     }
 
-                     logger.warn(`received friend request from ${userString} (from group ${groupString})`);
 
-                     logger.warn('please manually accept this friend request');
 
-                 });
 
-             }));
 
-             this.app.on('group-request', (session) => __awaiter(this, void 0, void 0, function* () {
 
-                 const userString = `${session.username}(${session.userId})`;
 
-                 const groupString = `${session.groupName}(${session.groupId})`;
 
-                 logger.debug(`detected group invitation event: ${groupString}}`);
 
-                 return session.bot.getFriendList().then(friendList => {
 
-                     if (friendList.some(friendItem => friendItem.userId = session.userId)) {
 
-                         session.bot.handleGroupRequest(session.messageId, true);
 
-                         return logger.info(`accepted group invitation from ${userString} (friend)`);
 
-                     }
 
-                     logger.warn(`received group invitation from ${userString} (stranger)`);
 
-                     logger.warn('please manually accept this group invitation');
 
-                 });
 
-             }));
 
-             this.app.middleware((session) => __awaiter(this, void 0, void 0, function* () {
 
-                 const chat = yield this.getChat(session);
 
-                 const cmdObj = command_1.parseCmd(session.content);
 
-                 const reply = (msg) => __awaiter(this, void 0, void 0, function* () { return session.sendQueued(msg); });
 
-                 switch (cmdObj.cmd) {
 
-                     case 'instagram_view':
 
-                     case 'instagram_get':
 
-                         command_1.view(chat, cmdObj.args, reply);
 
-                         break;
 
-                     case 'instagram_sub':
 
-                     case 'instagram_subscribe':
 
-                         this.botInfo.sub(chat, cmdObj.args, reply);
 
-                         break;
 
-                     case 'instagram_unsub':
 
-                     case 'instagram_unsubscribe':
 
-                         this.botInfo.unsub(chat, cmdObj.args, reply);
 
-                         break;
 
-                     case 'ping':
 
-                     case 'instagram':
 
-                         this.botInfo.list(chat, cmdObj.args, reply);
 
-                         break;
 
-                     case 'help':
 
-                         if (cmdObj.args.length === 0) {
 
-                             reply(`Instagram 搬运机器人:
 
- /instagram - 查询当前聊天中的 Instagram 动态订阅
 
- /instagram_subscribe〈链接|用户名〉- 订阅 Instagram 媒体搬运
 
- /instagram_unsubscribe〈链接|用户名〉- 退订 Instagram 媒体搬运
 
- /instagram_view〈链接〉- 查看媒体
 
- ${chat.chatType === "temp" ?
 
-                                 '\n(当前游客模式下无法使用订阅功能,请先添加本账号为好友。)' : ''}`);
 
-                         }
 
-                 }
 
-             }), 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.listen();
 
-             this.bot = this.app.getBot('onebot');
 
-         });
 
-         logger.warn(`Initialized koishi on ${opt.host}:${opt.port} with access_token ${opt.access_token}`);
 
-         this.botInfo = opt;
 
-     }
 
- }
 
- exports.default = default_1;
 
 
  |