"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.ellipseBase64InMessage = exports.message = void 0; const getPort = require("get-port"); const koishi_1 = require("koishi"); require("koishi-adapter-onebot"); const command_1 = require("./command"); const loggers_1 = require("./loggers"); const logger = loggers_1.getLogger('qqbot'); exports.message = koishi_1.segment; const ellipseBase64InMessage = (msg) => msg.replace(/(?<=\[CQ:.*base64:\/\/).*?(,|\])/, '...$1'); exports.ellipseBase64InMessage = ellipseBase64InMessage; class default_1 { constructor(opt) { 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.sendTo = (subscriber, msg) => (() => { switch (subscriber.chatType) { case 'group': return this.bot.sendMessage(subscriber.chatID.toString(), msg); case 'private': return this.bot.sendPrivateMessage(subscriber.chatID.toString(), msg); case 'temp': return this.bot.sendPrivateMessage(subscriber.chatID.qq.toString(), msg); } })() .then(response => { logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response:`); logger.info(response); }) .catch(reason => { logger.error(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`); throw Error(reason); }); this.initBot = () => __awaiter(this, void 0, void 0, function* () { const port = yield getPort(); this.app = new koishi_1.App({ type: 'onebot', port, server: `http://${this.botInfo.host}:${this.botInfo.port}`, selfId: this.botInfo.bot_id.toString(), token: this.botInfo.access_token, onebot: { quickOperation: 100, }, 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.send(msg); }); switch (cmdObj.cmd) { case 'twitter_view': case 'twitter_get': command_1.view(chat, cmdObj.args, reply); break; case 'twitter_query': case 'twitter_gettimeline': command_1.query(chat, cmdObj.args, reply); break; case 'twitter_sub': case 'twitter_subscribe': this.botInfo.sub(chat, cmdObj.args, reply); break; case 'twitter_unsub': case 'twitter_unsubscribe': this.botInfo.unsub(chat, cmdObj.args, reply); break; case 'ping': case 'twitter': this.botInfo.list(chat, cmdObj.args, reply); break; case 'help': if (cmdObj.args.length === 0) { reply(`推特搬运机器人: /twitter - 查询当前聊天中的推文订阅 /twitter_subscribe〈链接|用户名〉- 订阅 Twitter 推文搬运 /twitter_unsubscribe〈链接|用户名〉- 退订 Twitter 推文搬运 /twitter_view〈链接〉- 查看推文 /twitter_query〈链接|用户名〉[参数列表...] - 查询时间线(详见 /help twitter_query)\ ${chat.chatType === "temp" ? '\n(当前游客模式下无法使用订阅功能,请先添加本账号为好友。)' : ''}`); } else if (cmdObj.args[0] === 'twitter_query') { reply(`查询时间线中的推文: /twitter_query〈链接|用户名〉[〈参数 1〉=〈值 1〉〈参数 2〉=〈值 2〉...] 参数列表(方框内全部为可选,留空则为默认): count:查询数量上限(类型:非零整数,最大值正负 50)[默认值:10] since:查询起始点(类型:正整数或日期)[默认值:(空,无限过去)] until:查询结束点(类型:正整数或日期)[默认值:(空,当前时刻)] noreps 忽略回复推文(类型:on/off)[默认值:on(是)] norts:忽略原生转推(类型:on/off)[默认值:off(否)]`) .then(() => reply(`\ 起始点和结束点为正整数时取推特推文编号作为比较基准,否则会尝试作为日期读取。 推荐的日期格式:2012-12-22 12:22 UTC+2 (日期和时间均为可选,可分别添加) count 为正时,从新向旧查询;为负时,从旧向新查询 count 与 since/until 并用时,取二者中实际查询结果较少者 例子:/twitter_query RiccaTachibana count=5 since="2019-12-30\ UTC+9" until="2020-01-06 UTC+8" norts=on 从起始时间点(含)到结束时间点(不含)从新到旧获取最多 5 条推文,\ 其中不包含原生转推(实际上用户只发了 1 条)`)); } } })); }); this.connect = () => __awaiter(this, void 0, void 0, function* () { yield this.initBot(); yield this.app.start(); 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;