UNPKG

miraipie

Version:

the most powerful nodejs development kit for mirai-api-http

727 lines (726 loc) 23.8 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) to[j] = from[i]; return to; }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MiraiCode = exports.File = exports.Forward = exports.ForwardNode = exports.makeMusicShare = exports.MusicShare = exports.Dice = exports.Poke = exports.App = exports.Json = exports.Xml = exports.makeVoice = exports.Voice = exports.makeFlashImage = exports.FlashImage = exports.makeImage = exports.Image = exports.Plain = exports.Face = exports.AtAll = exports.At = exports.Quote = exports.MessageChain = void 0; var Mirai = __importStar(require("./mirai")); /** * 判断单一消息类型(typescript类型保护) * @param type 单一消息类型 * @return 是否为对应类型 */ function isType(type) { return this.type === type; } /** * mirai码字符转义 * @param raw 原始串 * @return 转移后串 */ function escape(raw) { return raw .replace(/([\[\]:,\\])/g, '\\$1') .replace(/\n/g, '\\n') .replace(/\r/g, '\\r'); } /** * 将单一消息转化为对应mirai码 * @example * Plain('Hello').toMiraiCode(); // "Hello" * AtAll().toMiraiCode(); // "[mirai:atall]" * Dice(6).toMiraiCode(); // "[mirai:dice:6]" */ function toMiraiCode() { if (this.isType('Source')) return "[mirai:source:" + this.id + "]"; else if (this.isType('Quote')) return "[mirai:quote:" + this.id + "]"; else if (this.isType('At')) return "[mirai:at:" + this.target + "]"; else if (this.isType('AtAll')) return "[mirai:atall]"; else if (this.isType('Face')) return "[mirai:face:" + this.faceId + "]"; else if (this.isType('Plain')) return "" + this.text; else if (this.isType('Image')) return "[mirai:image:" + this.imageId + "]"; else if (this.isType('FlashImage')) return "[mirai:flash:" + this.imageId + "]"; else if (this.isType('Voice')) return "[mirai:voice:" + this.voiceId + "]"; else if (this.isType('Xml')) return "[mirai:xml:" + escape(this.xml) + "]"; else if (this.isType('Json')) return "[mirai:json:" + escape(this.json) + "]"; else if (this.isType('App')) return "[mirai:app:" + escape(this.content) + "]"; else if (this.isType('Poke')) return "[mirai:poke:" + this.name + "]"; else if (this.isType('Dice')) return "[mirai:dice:" + this.value + "]"; else if (this.isType('MusicShare')) return "[mirai:musicshare:" + this.kind + "," + this.title + "," + this.summary + "," + this.jumpUrl + "," + this.pictureUrl + "," + this.musicUrl + "," + this.brief + "]"; else if (this.isType('Forward')) return "[mirai:forward:" + this.nodeList.length + "]"; else if (this.isType('File')) return "[mirai:file:" + this.id + "," + this.name + "," + this.size + "]"; else if (this.isType('MiraiCode')) return this.code; else return "[mirai:unknown]"; } /** * 将单一消息转化为对应显示串 * @example * Plain('Hello').toDisplayString(); // "Hello" * AtAll().toDisplayString(); // "@全体成员" * Dice(6).toDisplayString(); // "[骰子:6]" */ function toDisplayString() { if (this.isType('Quote')) return "[\u56DE\u590D]" + this.origin.toString(); else if (this.isType('At')) return "@" + this.target; else if (this.isType('AtAll')) return "@\u5168\u4F53\u6210\u5458"; else if (this.isType('Face')) return "[" + (this.name || Mirai.FaceType[this.faceId] || '表情') + "]"; else if (this.isType('Plain')) return "" + this.text; else if (this.isType('Image')) return "[\u56FE\u7247]"; else if (this.isType('FlashImage')) return "[\u95EA\u7167]"; else if (this.isType('Voice')) return "[\u8BED\u97F3\u6D88\u606F]"; else if (this.isType('Xml')) return "" + this.xml; else if (this.isType('Json')) return "" + this.json; else if (this.isType('App')) return "" + this.content; else if (this.isType('Poke')) return "[\u6233\u4E00\u6233]"; else if (this.isType('Dice')) return "[\u9AB0\u5B50:" + this.value + "]"; else if (this.isType('MusicShare')) return "[\u5206\u4EAB]" + this.title; else if (this.isType('Forward')) return "[\u8F6C\u53D1\u6D88\u606F]"; else if (this.isType('File')) return "[\u6587\u4EF6]" + this.name; else return "[\u4E0D\u652F\u6301\u7684\u6D88\u606F\u7C7B\u578B#" + this.type + "]"; } /** 消息链 */ var MessageChain = /** @class */ (function (_super) { __extends(MessageChain, _super); function MessageChain() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var _this = _super.apply(this, __spreadArray([], __read(args))) || this; Object.setPrototypeOf(_this, MessageChain.prototype); return _this; } Object.defineProperty(MessageChain.prototype, "firstClientMessage", { /** * 获取消息链中第一个有效消息, 如果没有将返回null * @return 第一个有效消息 * @example * const chain = MessageChain.from([AtAll(), Plain('Hello World!')]); * chain.firstClientMessage; // {type: 'AtAll'} */ get: function () { var e_1, _a; try { for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) { var message = _c.value; if (message.type !== 'Source') return message; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } return null; }, enumerable: false, configurable: true }); Object.defineProperty(MessageChain.prototype, "f", { /** * 获取消息链中第一个有效消息, 如果没有将返回null * @return 第一个有效消息 * @example * const chain = MessageChain.from([AtAll(), Plain('Hello World!')]); * chain.f; // {type: 'AtAll'} */ get: function () { return this.firstClientMessage; }, enumerable: false, configurable: true }); Object.defineProperty(MessageChain.prototype, "sourceId", { /** * 获取消息链中的消息id, 如果没有将返回null * @return 消息id * @example * const chain = fetchSomeMessages(); // MessageChain(2) [{type: 'Source', id: 123456, time: 123456}, {type: 'AtAll'}] * chain.sourceId; // 123456 */ get: function () { if (this.length > 0 && this[0].isType('Source')) return this[0].id; else return null; }, enumerable: false, configurable: true }); Object.defineProperty(MessageChain.prototype, "time", { /** * 获取消息链的消息发送时间, 如果没有将返回null * @return 消息发送时间 * @since 1.2.2 */ get: function () { if (this.length > 0 && this[0].isType('Source')) return this[0].time; else return null; }, enumerable: false, configurable: true }); /** * <strong>原地</strong>选择保留某类型的单一消息 * @param type 单一消息类型 * @return 消息链 * @example * const chain = MessageChain.from([AtAll(), Plain('Hello '), Plain('World!')]); * // MessageChain(1) [{type: 'AtAll'}] * chain.select('AtAll'); * // MessageChain(1) [{type: 'AtAll'}] * chain; */ MessageChain.prototype.select = function (type) { var _this = this; this.forEach(function (message, index) { if (message.type !== type) _this.splice(index, 1); }); return this; }; /** * 选择保留某类型的单一消息并生成<strong>新消息链</strong> * @param type 单一消息类型 * @return 消息链 * @example * const chain = MessageChain.from([AtAll(), Plain('Hello '), Plain('World!')]); * // MessageChain(1) [{type: 'AtAll'}] * chain.selected('AtAll'); * // MessageChain(3) [{type: 'AtAll'}, {type: 'Plain', text: 'Hello '}, {type: 'Plain', text: 'World!'}] * chain; */ MessageChain.prototype.selected = function (type) { return MessageChain.from(Array.from(this).filter(function (message) { return message.type === type; })); }; /** * <strong>原地</strong>删除某类型的单一消息 * @param type 单一消息类型 * @return 消息链 * @example * const chain = MessageChain.from([AtAll(), Plain('Hello '), Plain('World!')]); * // MessageChain(1) [{type: 'AtAll'}] * chain.drop('Plain'); * // MessageChain(1) [{type: 'AtAll'}] * chain; */ MessageChain.prototype.drop = function (type) { for (var i = this.length - 1; i >= 0; i--) { if (this[i].type === type) this.splice(i, 1); } return this; }; /** * 删除某类型的单一消息并生成<strong>新消息链</strong> * @param type 单一消息类型 * @return 消息链 * @example * const chain = MessageChain.from([AtAll(), Plain('Hello '), Plain('World!')]); * // MessageChain(1) [{type: 'AtAll'}] * chain.dropped('Plain'); * // MessageChain(3) [{type: 'AtAll'}, {type: 'Plain', text: 'Hello '}, {type: 'Plain', text: 'World!'}] * chain; */ MessageChain.prototype.dropped = function (type) { return MessageChain.from(Array.from(this).filter(function (message) { return message.type !== type; })); }; /** * 将消息链转化为mirai码表示形式(与mirai-core中的mirai码有差异, 不能混用) * @return mirai码表示形式 * @example * const chain = MessageChain.from([AtAll(), Plain('Hello '), Plain('World!')]); * // "[mirai:atall] Hello World!" * chain.toMiraiCode(); */ MessageChain.prototype.toMiraiCode = function () { return this.map(function (message) { return message.toMiraiCode(); }).join(''); }; /** * 将消息链转化为显示串 * @return 消息链的显示串 * @example * const chain = MessageChain.from([AtAll(), Plain('Hello '), Plain('World!')]); * // "@全体成员 Hello World!" * chain.toDisplayString(); */ MessageChain.prototype.toDisplayString = function () { return this.dropped('Source').map(function (message) { return message.toDisplayString(); }).join(''); }; /** * 使用单一消息数组构造消息链 * @param messageList 单一消息数组 * @return 消息链 * @example * // MessageChain(1) [{type: 'Plain', text: 'Hello World!'}] * MessageChain.from([Plain('Hello World!')]); * // MessageChain(2) [{type: 'AtAll'}, {type: 'Plain', text: 'Hello World!'}] * MessageChain.from([AtAll(), Plain('Hello World!')]); */ MessageChain.from = function (messageList) { var chain = new MessageChain(); chain.push.apply(chain, __spreadArray([], __read(messageList.map(function (message) { return (__assign(__assign({}, message), { isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode })); })))); return chain; }; return MessageChain; }(Array)); exports.MessageChain = MessageChain; /** * 构造引用回复消息 * @param id 引用消息id * @param groupId 群号 * @param senderId 发送人QQ号 * @param origin 接收者账号 * @param targetId 原始消息内容 */ function Quote(id, groupId, senderId, origin, targetId) { return { type: 'Quote', id: id, groupId: groupId, senderId: senderId, targetId: targetId, origin: origin, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Quote = Quote; /** * 构造&#64;消息 * @param target 群成员QQ号 * @param display At时显示的文字, 发送消息时无效, 自动使用群名片 */ function At(target, display) { if (display === void 0) { display = ''; } return { type: 'At', target: target, display: display, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.At = At; /** * 构造&#64;全体成员消息 */ function AtAll() { return { type: 'AtAll', isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.AtAll = AtAll; /** * 构造表情消息 * @param faceId 表情id * @param name 表情名称(使用时需将 faceId 设置为 undefined) */ function Face(faceId, name) { return { type: 'Face', faceId: faceId, name: name, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Face = Face; /** * 构造普通文本消息 * @param text 文本内容 */ function Plain(text) { return { type: 'Plain', text: text, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Plain = Plain; /** * 构造图片消息 * @param imageId 图片id * @param url 图片链接 * @param path 图片文件路径 * @param base64 图片Base64编码 */ function Image(imageId, url, path, base64) { return { type: 'Image', imageId: imageId, url: url, path: path, base64: base64, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Image = Image; /** * 通过对象构造图片消息 * @param options 图片选项 */ function makeImage(options) { return __assign(__assign({ type: 'Image', imageId: options.imageId, url: options.url }, options), { isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }); } exports.makeImage = makeImage; /** * 构造闪图消息 * @param imageId 图片id * @param url 图片链接 * @param path 图片文件路径 * @param base64 图片Base64编码 */ function FlashImage(imageId, url, path, base64) { return { type: 'FlashImage', imageId: imageId, url: url, path: path, base64: base64, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.FlashImage = FlashImage; /** * 通过对象构造闪图消息 * @param options 闪图选项 */ function makeFlashImage(options) { return __assign(__assign({ type: 'FlashImage', imageId: options.imageId, url: options.url }, options), { isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }); } exports.makeFlashImage = makeFlashImage; /** * 构造语音消息 * @param voiceId 语音id * @param url 语音链接 * @param path 语音文件路径 * @param base64 语音Base64编码 */ function Voice(voiceId, url, path, base64) { return { type: 'Voice', voiceId: voiceId, path: path, base64: base64, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Voice = Voice; /** * 通过对象构造语音消息 * @param options 语音选项 */ function makeVoice(options) { return __assign(__assign({ type: 'Voice', voiceId: options.voiceId }, options), { isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }); } exports.makeVoice = makeVoice; /** * 构造XML消息 * @param xml XML内容 */ function Xml(xml) { return { type: 'Xml', xml: xml, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Xml = Xml; /** * 构造JSON消息 * @param json JSON内容 */ function Json(json) { return { type: 'Json', json: json, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Json = Json; /** * 构造小程序消息(手动构造的一般无法发送) * @param content 小程序内容(Json格式) */ function App(content) { return { type: 'App', content: content, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.App = App; /** * 构造戳一戳消息 * @param name 戳一戳名称 */ function Poke(name) { return { type: 'Poke', name: name, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Poke = Poke; /** * 构造骰子消息 * @param value 骰子数值(1~6) */ function Dice(value) { return { type: 'Dice', value: value, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Dice = Dice; /** * 构造音乐分享消息 * @param kind 音乐分享类型 * @param title 标题 * @param summary 概括 * @param jumpUrl 跳转链接 * @param pictureUrl 封面图片链接 * @param musicUrl 音乐播放链接 * @param brief 简介 */ function MusicShare(kind, title, summary, jumpUrl, pictureUrl, musicUrl, brief) { return { type: 'MusicShare', kind: kind, title: title, summary: summary, jumpUrl: jumpUrl, pictureUrl: pictureUrl, musicUrl: musicUrl, brief: brief, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.MusicShare = MusicShare; /** * 通过对象构造音乐分享消息 * @param options 音乐分享选项 */ function makeMusicShare(options) { return { type: 'MusicShare', kind: options.kind, title: options.title, summary: options.summary, jumpUrl: options.jumpUrl, pictureUrl: options.pictureUrl, musicUrl: options.musicUrl, brief: options.brief, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.makeMusicShare = makeMusicShare; /** * 构造转发结点 * @param senderId 发送人QQ号 * @param time 发送时间戳 * @param senderName 发送人名称 * @param messageChain 消息链 * @param messageId 消息id */ function ForwardNode(senderId, time, senderName, messageChain, messageId) { return { senderId: senderId, time: time, senderName: senderName, messageChain: messageChain, messageId: messageId }; } exports.ForwardNode = ForwardNode; /** * 构造合并转发消息 * @param nodeList 结点列表 */ function Forward(nodeList) { return { type: 'Forward', nodeList: nodeList, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.Forward = Forward; /** * 构造文件消息 * @param id 文件id * @param name 文件名 * @param size 文件大小 */ function File(id, name, size) { return { type: 'File', id: id, name: name, size: size, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.File = File; /** * 构造mirai码消息 * @param code mirai码 */ function MiraiCode(code) { return { type: 'MiraiCode', code: code, isType: isType, toDisplayString: toDisplayString, toMiraiCode: toMiraiCode }; } exports.MiraiCode = MiraiCode;