miraipie
Version:
the most powerful nodejs development kit for mirai-api-http
688 lines (687 loc) • 31.5 kB
JavaScript
"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 ws_1 = __importDefault(require("ws"));
var adapter_1 = require("../adapter");
var mirai_1 = require("../mirai");
var utils_1 = require("../utils");
function generateSyncId() {
var syncId;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
syncId = 1;
_a.label = 1;
case 1:
if (!true) return [3 /*break*/, 3];
syncId = (syncId + 1) % 100;
return [4 /*yield*/, syncId];
case 2:
_a.sent();
return [3 /*break*/, 1];
case 3: return [2 /*return*/];
}
});
}
/**
* mirai-api-http 提供的 websocket adapter<br/>
* @see <a href="https://github.com/project-mirai/mirai-api-http/blob/master/docs/adapter/WebsocketAdapter.md">文档<a/>
*/
var WebsocketApiAdapter = adapter_1.makeApiAdapter({
id: 'ws',
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; },
},
timeout: {
type: Number,
description: '发送请求的超时时间(毫秒)',
default: function () { return 10000; },
},
ssl: {
type: Boolean,
description: '是否使用SSL',
default: function () { return false; },
},
},
data: {
/** 响应队列 */
queue: new Map(),
/** syncId 生成器 */
syncIdGenerator: generateSyncId(),
/**
* websocket 连接
* @type WebSocket
*/
ws: null,
},
methods: {
request: function (command, content, subCommand) {
if (content === void 0) { content = {}; }
if (subCommand === void 0) { subCommand = null; }
return __awaiter(this, void 0, void 0, function () {
var syncId, data, counter, resp;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
syncId = this.syncIdGenerator.next().value;
// 判断是否正在监听
if (!this.listening) {
this.logger.error("\u8FDB\u884C '" + command + "' \u64CD\u4F5C\u524D\u5FC5\u987B\u542F\u52A8\u76D1\u542C");
return [2 /*return*/];
}
data = { syncId: syncId, command: command, subCommand: subCommand, content: content };
this.ws.send(JSON.stringify(data), function (err) {
if (err)
_this.logger.error("\u53D1\u9001\u8BF7\u6C42\u9519\u8BEF, \u8BF7\u6C42\u547D\u4EE4\u5B57: " + command + ", \u8BF7\u6C42\u539F\u59CB\u6570\u636E:", data, err.message);
});
counter = 0;
_a.label = 1;
case 1:
if (!(counter * 200 < this.configs.timeout)) return [3 /*break*/, 4];
if (this.queue.has(syncId))
return [3 /*break*/, 4];
return [4 /*yield*/, utils_1.sleep(200)];
case 2:
_a.sent();
_a.label = 3;
case 3:
counter++;
return [3 /*break*/, 1];
case 4:
resp = this.queue.get(syncId);
if (resp) {
this.queue.delete(syncId);
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\u6C42\u547D\u4EE4\u5B57: " + command + ", \u9519\u8BEF\u539F\u56E0: " + resp.msg + ", \u8BF7\u6C42\u539F\u59CB\u6570\u636E:", data);
}
return [2 /*return*/, resp];
}
else {
this.logger.error('发送的请求已超时, 请求原始数据:', data);
}
return [2 /*return*/];
}
});
});
},
},
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) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('about')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getMessageFromId: function (messageId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('messageFromId', { target: messageId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getFriendList: function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('friendList')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getGroupList: function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('groupList')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getMemberList: function (groupId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('memberList', { target: groupId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getBotProfile: function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('botProfile')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getFriendProfile: function (friendId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('friendProfile', { target: friendId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getMemberProfile: function (memberId, groupId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('memberProfile', { target: groupId, memberId: memberId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getUserProfile: function (userId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.request('userProfile', { target: userId })];
});
});
},
sendFriendMessage: function (friendId, messageChain, quoteMessageId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('sendFriendMessage', { qq: friendId, messageChain: messageChain, quote: quoteMessageId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
sendGroupMessage: function (groupId, messageChain, quoteMessageId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('sendGroupMessage', { group: groupId, messageChain: messageChain, quote: quoteMessageId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
sendTempMessage: function (memberId, groupId, messageChain, quoteMessageId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('sendTempMessage', {
qq: memberId,
group: groupId,
messageChain: messageChain,
quote: quoteMessageId,
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
sendNudge: function (targetId, subjectId, kind) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('sendNudge', { target: targetId, subject: subjectId, kind: kind })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
recall: function (messageId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('recall', { target: messageId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
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) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('file_list', {
id: directoryId,
path: directoryPath,
group: groupId,
qq: friendId,
offset: offset,
size: size,
withDownloadInfo: withDownloadInfo,
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
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) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('file_info', {
id: fileId,
path: path,
group: groupId,
qq: friendId,
withDownloadInfo: withDownloadInfo,
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
createFileDirectory: function (parentDirectoryId, parentDirectoryPath, directoryName, groupId, friendId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('file_mkdir', {
id: parentDirectoryId,
path: parentDirectoryPath,
group: groupId,
qq: friendId,
directoryName: directoryName,
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
deleteFile: function (id, path, groupId, friendId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('file_delete', { id: id, path: path, group: groupId, qq: friendId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
moveFile: function (id, path, groupId, friendId, moveToDirectoryId, moveToDirectoryPath) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('file_move', {
id: id,
path: path,
group: groupId,
qq: friendId,
moveTo: moveToDirectoryId,
moveToPath: moveToDirectoryPath,
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
renameFile: function (id, path, groupId, friendId, name) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('file_rename', { id: id, path: path, group: groupId, qq: friendId, renameTo: name })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
deleteFriend: function (friendId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('deleteFriend', { target: friendId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
muteMember: function (memberId, groupId, time) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('mute', { target: groupId, memberId: memberId, time: time })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
unmuteMember: function (memberId, groupId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('unmute', { target: groupId, memberId: memberId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
kickMember: function (memberId, groupId, message) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('kick', { target: groupId, memberId: memberId, msg: message })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
quitGroup: function (groupId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('quit', { target: groupId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
muteAll: function (groupId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('muteAll', { target: groupId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
unmuteAll: function (groupId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('unmuteAll', { target: groupId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
setEssence: function (messageId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('setEssence', { target: messageId })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getGroupConfig: function (groupId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('groupConfig', { target: groupId }, 'get')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
setGroupConfig: function (groupId, config) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('groupConfig', { target: groupId, config: config }, 'update')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getMemberInfo: function (memberId, groupId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('memberInfo', { target: groupId, memberId: memberId }, 'get')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
setMemberInfo: function (memberId, groupId, info) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('memberInfo', { target: groupId, memberId: memberId, info: info }, 'update')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
setMemberAdmin: function (memberId, groupId, admin) {
if (admin === void 0) { admin = true; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('memberAdmin', { target: groupId, memberId: memberId, assign: admin })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
getGroupAnnouncements: function (groupId, offset, size) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.request('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.request('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.request('anno_delete', { id: groupId, fid: announcementId })];
});
});
},
executeCommand: function (command) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.request('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.request('cmd_register', { name: name, alias: alias, usage: usage, description: description })];
});
});
},
handleNewFriendRequest: function (eventId, fromId, groupId, operate, message) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('resp_newFriendRequestEvent', { eventId: eventId, fromId: fromId, groupId: groupId, operate: operate, message: message })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
handleMemberJoinRequest: function (eventId, fromId, groupId, operate, message) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('resp_memberJoinRequestEvent', { eventId: eventId, fromId: fromId, groupId: groupId, operate: operate, message: message })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
handleBotInvitedJoinGroupRequest: function (eventId, fromId, groupId, operate, message) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request('resp_botInvitedJoinGroupRequestEvent', { eventId: eventId, fromId: fromId, groupId: groupId, operate: operate, message: message })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
},
uploadImage: function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.logger.error('暂不支持的操作: uploadImage');
return [2 /*return*/, null];
});
});
},
uploadVoice: function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.logger.error('暂不支持的操作: uploadVoice');
return [2 /*return*/, null];
});
});
},
uploadGroupFile: function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.logger.error('暂不支持的操作: uploadGroupFile');
return [2 /*return*/, null];
});
});
},
});
module.exports = WebsocketApiAdapter;