UNPKG

miraipie

Version:

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

631 lines (630 loc) 29.2 kB
"use strict"; 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 __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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var axios_1 = __importDefault(require("axios")); var form_data_1 = __importDefault(require("form-data")); var ws_1 = __importDefault(require("ws")); var adapter_1 = require("../adapter"); var mirai_1 = require("../mirai"); /** * 使用 http 发送操作请求, ws 监听事件 的混用客户端 adapter<br/> */ var MixedApiAdapter = adapter_1.makeApiAdapter({ id: 'mixed', supportVersion: mirai_1.MIRAI_API_HTTP_VERSION, configMeta: { qq: { type: Number, description: 'mirai-api-http服务的QQ号', }, verifyKey: { type: String, required: true, description: 'mirai-api-http配置中的verifyKey', }, host: { type: String, required: true, description: 'mirai-api-http服务的主机地址', default: function () { return '127.0.0.1'; }, }, port: { type: Number, required: true, description: 'mirai-api-http服务的端口号', default: function () { return 23333; }, }, ssl: { type: Boolean, description: '是否使用SSL', default: function () { return false; }, }, }, data: { session: null, ws: null, }, methods: { request: function (uri, data, method, requireSession, multipart) { if (requireSession === void 0) { requireSession = true; } if (multipart === void 0) { multipart = false; } return __awaiter(this, void 0, void 0, function () { var config, resp, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: config = { method: method, url: "http" + (this.configs.ssl ? 's' : '') + "://" + this.configs.host + ":" + this.configs.port + "/" + uri, headers: {}, }; // 判断请求类型 if (method === 'POST') config.data = data; else config.params = data; if (multipart) config.headers = data.getHeaders(); if (!requireSession) return [3 /*break*/, 4]; if (!!this.session) return [3 /*break*/, 3]; return [4 /*yield*/, this.verify()]; case 1: _a.sent(); return [4 /*yield*/, this.bind()]; case 2: _a.sent(); if (!this.session) this.logger.error('session未绑定或已失效'); _a.label = 3; case 3: config.headers.sessionKey = this.session; _a.label = 4; case 4: _a.trys.push([4, 6, , 7]); return [4 /*yield*/, axios_1.default.request(config)]; case 5: resp = (_a.sent()).data; if ('code' in resp && resp['code'] !== mirai_1.ResponseCode.Success) { this.logger.warn("\u53D1\u9001\u7684\u8BF7\u6C42\u672A\u80FD\u8FBE\u5230\u9884\u671F\u7684\u6548\u679C, \u8BF7\u6C42uri: " + uri + ", \u9519\u8BEF\u539F\u56E0: " + resp.msg + ", \u8BF7\u6C42\u539F\u59CB\u6570\u636E:", data); } return [2 /*return*/, resp]; case 6: err_1 = _a.sent(); this.logger.error('发送请求错误, 请求原始数据:', data, err_1.message); return [3 /*break*/, 7]; case 7: return [2 /*return*/]; } }); }); }, get: function (uri, params, requireSession, multipart) { if (requireSession === void 0) { requireSession = true; } if (multipart === void 0) { multipart = false; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.request(uri, params, 'GET', requireSession, multipart)]; }); }); }, post: function (uri, data, requireSession, multipart) { if (requireSession === void 0) { requireSession = true; } if (multipart === void 0) { multipart = false; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.request(uri, data, 'POST', requireSession, multipart)]; }); }); }, }, verify: function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { _this.post('verify', { verifyKey: _this.configs.verifyKey }, false).then(function (res) { if (res.code === mirai_1.ResponseCode.WrongVerifyKey) _this.logger.error("\u8BA4\u8BC1\u5931\u8D25, \u9519\u8BEF\u7684verifyKey"); else _this.session = res.session; resolve(res); }).catch(function (err) { reject(err); }); })]; }); }); }, bind: function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('bind', { sessionKey: this.session, qq: this.configs.qq }, false)]; }); }); }, release: function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { _this.post('release', { sessionKey: _this.session, qq: _this.configs.qq }, false).then(function (res) { _this.session = null; resolve(res); }).catch(function (err) { reject(err); }); })]; }); }); }, listen: function () { return __awaiter(this, void 0, void 0, function () { var id; var _this = this; return __generator(this, function (_a) { if (!this.listening) { id = this.configs.qq; this.ws = new ws_1.default("ws" + (this.configs.ssl ? 's' : '') + "://" + this.configs.host + ":" + this.configs.port + "/all?verifyKey=" + this.configs.verifyKey + "&qq=" + id); this.ws.on('error', function (err) { _this.logger.error("\u76D1\u542C\u5668\u542F\u52A8\u51FA\u9519:", err.message); }); this.ws.on('message', function (buffer) { var message = JSON.parse(buffer.toString()); if ('syncId' in message) { if (message.syncId === '-1') { var data = message.data; if (data.type.endsWith('Message')) _this.emit('message', data); else if (data.type.endsWith('SyncMessage')) _this.emit('sync', data); else if (data.type.endsWith('Event')) _this.emit('event', data); } else if (message.syncId === '') { var data = message.data; if (data.code === mirai_1.ResponseCode.Success) { _this.listening = true; _this.emit('listen'); _this.logger.info('已启动监听'); } else { _this.logger.error("\u76D1\u542C\u5668\u542F\u52A8\u5931\u8D25, \u9519\u8BEF\u539F\u56E0: " + data.msg); _this.ws.close(); } } else { _this.queue.set(parseInt(message.syncId), message.data); } } else { _this.logger.warn('未能解析的数据:', message); } }); } return [2 /*return*/]; }); }); }, stop: function () { if (this.ws && this.ws.readyState === this.ws.OPEN) { this.ws.close(); this.listening = false; this.logger.info('已停止监听'); this.emit('stop'); } }, getAbout: function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('about', null, false)]; }); }); }, getMessageFromId: function (messageId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('messageFromId', { id: messageId })]; }); }); }, getBotProfile: function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('botProfile')]; }); }); }, getFriendList: function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('friendList')]; }); }); }, getGroupList: function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('groupList')]; }); }); }, getMemberList: function (groupId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('memberList', { target: groupId })]; }); }); }, getFriendProfile: function (friendId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('friendProfile', { target: friendId })]; }); }); }, getMemberProfile: function (memberId, groupId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('memberProfile', { target: groupId, memberId: memberId })]; }); }); }, getUserProfile: function (userId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('userProfile', { target: userId })]; }); }); }, sendFriendMessage: function (friendId, messageChain, quoteMessageId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('sendFriendMessage', { target: friendId, messageChain: messageChain, quote: quoteMessageId, })]; }); }); }, sendGroupMessage: function (groupId, messageChain, quoteMessageId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('sendGroupMessage', { target: groupId, messageChain: messageChain, quote: quoteMessageId, })]; }); }); }, sendTempMessage: function (memberId, groupId, messageChain, quoteMessageId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('sendTempMessage', { qq: memberId, group: groupId, messageChain: messageChain, quote: quoteMessageId, })]; }); }); }, sendNudge: function (targetId, subjectId, kind) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('sendNudge', { target: targetId, subject: subjectId, kind: kind })]; }); }); }, recall: function (messageId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('recall', { target: messageId })]; }); }); }, getFileList: function (directoryId, directoryPath, groupId, friendId, offset, size, withDownloadInfo) { if (offset === void 0) { offset = 1; } if (size === void 0) { size = 100; } if (withDownloadInfo === void 0) { withDownloadInfo = true; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('file/list', { id: directoryId, path: directoryPath, group: groupId, qq: friendId, offset: offset, size: size, withDownloadInfo: withDownloadInfo, })]; }); }); }, getFileInfo: function (fileId, path, groupId, friendId, withDownloadInfo) { if (withDownloadInfo === void 0) { withDownloadInfo = true; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('file/info', { id: fileId, path: path, group: groupId, qq: friendId, withDownloadInfo: withDownloadInfo })]; }); }); }, createFileDirectory: function (parentDirectoryId, parentDirectoryPath, directoryName, groupId, friendId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('file/mkdir', { id: parentDirectoryId, path: parentDirectoryPath, group: groupId, qq: friendId, directoryName: directoryName, })]; }); }); }, deleteFile: function (id, path, groupId, friendId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('file/delete', { id: id, path: path, group: groupId, qq: friendId })]; }); }); }, moveFile: function (id, path, groupId, friendId, moveToDirectoryId, moveToDirectoryPath) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('file/move', { id: id, path: path, group: groupId, qq: friendId, moveTo: moveToDirectoryId, moveToPath: moveToDirectoryPath, })]; }); }); }, renameFile: function (id, path, groupId, friendId, name) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('file/rename', { id: id, path: path, group: groupId, qq: friendId, renameTo: name })]; }); }); }, deleteFriend: function (friendId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('deleteFriend', { target: friendId })]; }); }); }, muteMember: function (memberId, groupId, time) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('mute', { target: groupId, memberId: memberId, time: time })]; }); }); }, unmuteMember: function (memberId, groupId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('unmute', { target: groupId, memberId: memberId })]; }); }); }, kickMember: function (memberId, groupId, message) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('kick', { target: groupId, memberId: memberId, msg: message })]; }); }); }, quitGroup: function (groupId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('quit', { target: groupId })]; }); }); }, muteAll: function (groupId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('muteAll', { target: groupId })]; }); }); }, unmuteAll: function (groupId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('unmuteAll', { target: groupId })]; }); }); }, setEssence: function (messageId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('setEssence', { target: messageId })]; }); }); }, getGroupConfig: function (groupId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('groupConfig', { target: groupId })]; }); }); }, setGroupConfig: function (groupId, config) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('groupConfig', { target: groupId, config: config })]; }); }); }, getMemberInfo: function (memberId, groupId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('memberInfo', { target: groupId, memberId: memberId })]; }); }); }, setMemberInfo: function (memberId, groupId, info) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('memberInfo', { target: groupId, memberId: memberId, info: info })]; }); }); }, setMemberAdmin: function (memberId, groupId, admin) { if (admin === void 0) { admin = true; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('memberAdmin', { target: groupId, memberId: memberId, assign: admin })]; }); }); }, executeCommand: function (command) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('cmd/execute', { command: command })]; }); }); }, registerCommand: function (name, alias, usage, description) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('cmd/register', { name: name, alias: alias, usage: usage, description: description })]; }); }); }, getGroupAnnouncements: function (groupId, offset, size) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.get('anno/list', { id: groupId, offset: offset, size: size })]; }); }); }, postGroupAnnouncement: function (groupId, announcement) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('anno/publish', __assign({ target: groupId }, announcement))]; }); }); }, deleteGroupAnnouncement: function (groupId, announcementId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('anno/delete', { id: groupId, fid: announcementId })]; }); }); }, handleNewFriendRequest: function (eventId, fromId, groupId, operate, message) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('resp/newFriendRequestEvent', { eventId: eventId, fromId: fromId, groupId: groupId, operate: operate, message: message })]; }); }); }, handleMemberJoinRequest: function (eventId, fromId, groupId, operate, message) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('resp/memberJoinRequestEvent', { eventId: eventId, fromId: fromId, groupId: groupId, operate: operate, message: message })]; }); }); }, handleBotInvitedJoinGroupRequest: function (eventId, fromId, groupId, operate, message) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.post('resp/botInvitedJoinGroupRequestEvent', { eventId: eventId, fromId: fromId, groupId: groupId, operate: operate, message: message, })]; }); }); }, uploadImage: function (uploadType, imageData) { return __awaiter(this, void 0, void 0, function () { var data; return __generator(this, function (_a) { data = new form_data_1.default(); data.append('type', uploadType); data.append('img', imageData); return [2 /*return*/, this.post('uploadImage', data, true, true)]; }); }); }, uploadVoice: function (uploadType, voiceData) { return __awaiter(this, void 0, void 0, function () { var data; return __generator(this, function (_a) { data = new form_data_1.default(); data.append('type', uploadType); data.append('voice', voiceData); return [2 /*return*/, this.post('uploadVoice', data, true, true)]; }); }); }, uploadGroupFile: function (uploadType, path, fileData) { return __awaiter(this, void 0, void 0, function () { var data; return __generator(this, function (_a) { data = new form_data_1.default(); data.append('type', uploadType); data.append('path', path); data.append('file', fileData); return [2 /*return*/, this.post('uploadFile', data, true, true)]; }); }); }, }); module.exports = MixedApiAdapter;