miraipie
Version:
the most powerful nodejs development kit for mirai-api-http
662 lines (661 loc) • 30.3 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 __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.");
};
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 adapter_1 = require("../adapter");
var mirai_1 = require("../mirai");
var utils_1 = require("../utils");
/**
* mirai-api-http 提供的 http adapter<br/>
* @see <a href="https://github.com/project-mirai/mirai-api-http/blob/master/docs/adapter/HttpAdapter.md">文档<a/>
*/
var HttpApiAdapter = adapter_1.makeApiAdapter({
id: 'http',
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; },
},
interval: {
type: Number,
description: 'http轮询周期(毫秒)',
default: function () { return 500; },
},
ssl: {
type: Boolean,
description: '是否使用SSL',
default: function () { return false; },
},
},
data: {
session: 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 first, resp, _a, _b, i, err_2;
var e_1, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (!!this.listening) return [3 /*break*/, 10];
first = true;
_d.label = 1;
case 1:
if (!(this.listening || first)) return [3 /*break*/, 10];
_d.label = 2;
case 2:
_d.trys.push([2, 7, 8, 9]);
return [4 /*yield*/, this.get('fetchMessage', { count: 30 })];
case 3:
resp = _d.sent();
if (!(resp.code === mirai_1.ResponseCode.Success)) return [3 /*break*/, 5];
if (first) {
this.listening = true;
this.logger.info('已启动监听');
this.emit('listen');
}
try {
for (_a = (e_1 = void 0, __values(resp.data)), _b = _a.next(); !_b.done; _b = _a.next()) {
i = _b.value;
if (i.type.endsWith('Message'))
this.emit('message', i);
else if (i.type.endsWith('SyncMessage'))
this.emit('sync', i);
else
this.emit('event', i);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
return [4 /*yield*/, utils_1.sleep(this.configs.interval)];
case 4:
_d.sent();
return [3 /*break*/, 6];
case 5:
this.logger.error("\u76D1\u542C\u5668\u542F\u52A8\u5931\u8D25, \u9519\u8BEF\u539F\u56E0: " + resp.msg);
_d.label = 6;
case 6: return [3 /*break*/, 9];
case 7:
err_2 = _d.sent();
this.logger.error('监听时发生错误:', err_2.message);
this.listening = false;
return [3 /*break*/, 9];
case 8:
first = false;
return [7 /*endfinally*/];
case 9: return [3 /*break*/, 1];
case 10: return [2 /*return*/];
}
});
});
},
stop: function () {
if (this.listening) {
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 })];
});
});
},
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,
})];
});
});
},
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 })];
});
});
},
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 = HttpApiAdapter;