onebots 0.4.21 → 0.4.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +163 -62
- package/lib/bin.js +1 -1
- package/lib/config.sample.yaml +1 -1
- package/lib/db.js +6 -6
- package/lib/index.d.ts +5 -5
- package/lib/onebot.d.ts +9 -6
- package/lib/onebot.js +63 -49
- package/lib/service/V11/action/common.d.ts +3 -2
- package/lib/service/V11/action/common.js +34 -38
- package/lib/service/V11/action/friend.d.ts +7 -7
- package/lib/service/V11/action/friend.js +6 -6
- package/lib/service/V11/action/group.d.ts +3 -3
- package/lib/service/V11/action/group.js +6 -6
- package/lib/service/V11/db_sqlite.js +7 -9
- package/lib/service/V11/index.d.ts +11 -12
- package/lib/service/V11/index.js +223 -172
- package/lib/service/V11/utils.d.ts +1 -4
- package/lib/service/V11/utils.js +11 -16
- package/lib/service/V12/index.d.ts +3 -5
- package/lib/service/V12/index.js +24 -26
- package/lib/service/shareMusicCustom.js +2 -2
- package/lib/service.d.ts +29 -0
- package/lib/service.js +85 -0
- package/lib/types.d.ts +2 -2
- package/lib/types.js +3 -3
- package/lib/utils.js +27 -15
- package/package.json +73 -70
package/lib/service/V11/index.js
CHANGED
|
@@ -1,39 +1,63 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
27
|
};
|
|
5
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
29
|
exports.V11 = void 0;
|
|
7
|
-
const icqq_1 = require("icqq");
|
|
8
|
-
const action_1 = require("./action");
|
|
9
30
|
const onebot_1 = require("../../onebot");
|
|
10
|
-
const
|
|
11
|
-
const
|
|
31
|
+
const app_1 = require("../../server/app");
|
|
32
|
+
const service_1 = require("../../service");
|
|
12
33
|
const utils_1 = require("../../utils");
|
|
13
|
-
const
|
|
14
|
-
const
|
|
34
|
+
const crypto = __importStar(require("crypto"));
|
|
35
|
+
const fs_1 = require("fs");
|
|
15
36
|
const http_1 = __importDefault(require("http"));
|
|
16
37
|
const https_1 = __importDefault(require("https"));
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const db_sqlite_1 = require("./db_sqlite");
|
|
38
|
+
const icqq_1 = require("icqq");
|
|
39
|
+
const icqq_cq_enable_1 = require("icqq-cq-enable");
|
|
20
40
|
const path_1 = require("path");
|
|
21
|
-
const
|
|
41
|
+
const url_1 = require("url");
|
|
42
|
+
const ws_1 = require("ws");
|
|
43
|
+
const action_1 = require("./action");
|
|
22
44
|
const db_entities_1 = require("./db_entities");
|
|
23
|
-
|
|
45
|
+
const db_sqlite_1 = require("./db_sqlite");
|
|
46
|
+
const sendMsgTypes = ["private", "group", "discuss"];
|
|
47
|
+
const sendMsgMethodRegex = new RegExp(`send_(${sendMsgTypes.join("|")})_msg`);
|
|
48
|
+
class V11 extends service_1.Service {
|
|
24
49
|
constructor(oneBot, client, config) {
|
|
25
|
-
super();
|
|
50
|
+
super(config);
|
|
26
51
|
this.oneBot = oneBot;
|
|
27
52
|
this.client = client;
|
|
28
|
-
this.
|
|
29
|
-
this.version = 'V11';
|
|
53
|
+
this.version = "V11";
|
|
30
54
|
this.timestamp = Date.now();
|
|
31
55
|
this._queue = [];
|
|
32
56
|
this.queue_running = false;
|
|
33
57
|
this.wsr = new Set();
|
|
34
58
|
this.action = new action_1.Action();
|
|
35
59
|
this.logger = this.oneBot.app.getLogger(this.oneBot.uin, this.version);
|
|
36
|
-
this.db = new db_sqlite_1.Database((0, path_1.join)(app_1.App.configDir,
|
|
60
|
+
this.db = new db_sqlite_1.Database((0, path_1.join)(app_1.App.configDir, "data", this.oneBot.uin + ".db"), this.logger);
|
|
37
61
|
}
|
|
38
62
|
start(path) {
|
|
39
63
|
this.path = `/${this.oneBot.uin}`;
|
|
@@ -43,33 +67,52 @@ class V11 extends events_1.EventEmitter {
|
|
|
43
67
|
this.startHttp();
|
|
44
68
|
if (this.config.use_ws)
|
|
45
69
|
this.startWs();
|
|
46
|
-
this.config.http_reverse.forEach(config => {
|
|
47
|
-
if (typeof config ===
|
|
70
|
+
this.config.http_reverse.forEach((config) => {
|
|
71
|
+
if (typeof config === "string") {
|
|
48
72
|
config = {
|
|
49
73
|
url: config,
|
|
50
74
|
access_token: this.config.access_token,
|
|
51
|
-
secret: this.config.secret
|
|
75
|
+
secret: this.config.secret,
|
|
52
76
|
};
|
|
53
77
|
}
|
|
54
78
|
else {
|
|
55
79
|
config = {
|
|
56
80
|
access_token: this.config.access_token,
|
|
57
81
|
secret: this.config.secret,
|
|
58
|
-
...config
|
|
82
|
+
...config,
|
|
59
83
|
};
|
|
60
84
|
}
|
|
61
85
|
this.startHttpReverse(config);
|
|
62
86
|
});
|
|
63
|
-
this.config.ws_reverse.forEach(config => {
|
|
87
|
+
this.config.ws_reverse.forEach((config) => {
|
|
64
88
|
this.startWsReverse(config);
|
|
65
89
|
});
|
|
90
|
+
this.on("dispatch", (serialized) => {
|
|
91
|
+
for (const ws of this.wss.clients) {
|
|
92
|
+
ws.send(serialized, (err) => {
|
|
93
|
+
if (err)
|
|
94
|
+
this.logger.error(`正向WS(${ws.url})上报事件失败: ` + err.message);
|
|
95
|
+
else
|
|
96
|
+
this.logger.debug(`正向WS(${ws.url})上报事件成功: ` + serialized);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
for (const ws of this.wsr) {
|
|
100
|
+
ws.send(serialized, (err) => {
|
|
101
|
+
if (err) {
|
|
102
|
+
this.logger.error(`反向WS(${ws.url})上报事件失败: ` + err.message);
|
|
103
|
+
}
|
|
104
|
+
else
|
|
105
|
+
this.logger.debug(`反向WS(${ws.url})上报事件成功: ` + serialized);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
});
|
|
66
109
|
if (this.config.heartbeat) {
|
|
67
110
|
this.heartbeat = setInterval(() => {
|
|
68
111
|
this.dispatch({
|
|
69
112
|
self_id: this.oneBot.uin,
|
|
70
113
|
status: {
|
|
71
114
|
online: this.client.status === icqq_1.OnlineStatus.Online,
|
|
72
|
-
good: this.oneBot.status === onebot_1.OneBotStatus.Good
|
|
115
|
+
good: this.oneBot.status === onebot_1.OneBotStatus.Good,
|
|
73
116
|
},
|
|
74
117
|
time: Math.floor(Date.now() / 1000),
|
|
75
118
|
post_type: "meta_event",
|
|
@@ -84,7 +127,7 @@ class V11 extends events_1.EventEmitter {
|
|
|
84
127
|
this.logger.mark(`开启http服务器成功,监听:http://127.0.0.1:${this.oneBot.app.config.port}${this.path}`);
|
|
85
128
|
}
|
|
86
129
|
startHttpReverse(config) {
|
|
87
|
-
this.on(
|
|
130
|
+
this.on("dispatch", (unserialized) => {
|
|
88
131
|
const serialized = JSON.stringify(unserialized);
|
|
89
132
|
const options = {
|
|
90
133
|
method: "POST",
|
|
@@ -98,16 +141,18 @@ class V11 extends events_1.EventEmitter {
|
|
|
98
141
|
};
|
|
99
142
|
if (this.config.secret) {
|
|
100
143
|
//@ts-ignore
|
|
101
|
-
options.headers["X-Signature"] =
|
|
144
|
+
options.headers["X-Signature"] =
|
|
145
|
+
"sha1=" + crypto.createHmac("sha1", String(this.config.secret)).update(serialized).digest("hex");
|
|
102
146
|
}
|
|
103
147
|
const protocol = config.url.startsWith("https") ? https_1.default : http_1.default;
|
|
104
148
|
try {
|
|
105
|
-
protocol
|
|
149
|
+
protocol
|
|
150
|
+
.request(config.url, options, (res) => {
|
|
106
151
|
if (res.statusCode !== 200)
|
|
107
152
|
return this.logger.warn(`POST(${config.url})上报事件收到非200响应:` + res.statusCode);
|
|
108
153
|
let data = "";
|
|
109
154
|
res.setEncoding("utf-8");
|
|
110
|
-
res.on("data", (chunk) => data += chunk);
|
|
155
|
+
res.on("data", (chunk) => (data += chunk));
|
|
111
156
|
res.on("end", () => {
|
|
112
157
|
this.logger.debug(`收到HTTP响应 ${res.statusCode} :` + data);
|
|
113
158
|
if (!data)
|
|
@@ -119,9 +164,11 @@ class V11 extends events_1.EventEmitter {
|
|
|
119
164
|
this.logger.error(`快速操作遇到错误:` + e.message);
|
|
120
165
|
}
|
|
121
166
|
});
|
|
122
|
-
})
|
|
167
|
+
})
|
|
168
|
+
.on("error", (err) => {
|
|
123
169
|
this.logger.error(`POST(${config.url})上报事件失败:` + err.message);
|
|
124
|
-
})
|
|
170
|
+
})
|
|
171
|
+
.end(serialized, () => {
|
|
125
172
|
this.logger.debug(`POST(${config.url})上报事件成功: ` + serialized);
|
|
126
173
|
});
|
|
127
174
|
}
|
|
@@ -146,38 +193,18 @@ class V11 extends events_1.EventEmitter {
|
|
|
146
193
|
});
|
|
147
194
|
if (this.config.access_token) {
|
|
148
195
|
const url = new url_1.URL(req.url, "http://127.0.0.1");
|
|
149
|
-
const token = url.searchParams.get(
|
|
196
|
+
const token = url.searchParams.get("access_token");
|
|
150
197
|
if (token)
|
|
151
198
|
req.headers["authorization"] = `Bearer ${token}`;
|
|
152
|
-
if (!req.headers["authorization"] ||
|
|
199
|
+
if (!req.headers["authorization"] ||
|
|
200
|
+
req.headers["authorization"] !== `Bearer ${this.config.access_token}`)
|
|
153
201
|
return ws.close(1002, "wrong access token");
|
|
154
202
|
}
|
|
155
203
|
this._webSocketHandler(ws);
|
|
156
204
|
});
|
|
157
|
-
this.on('dispatch', (serialized) => {
|
|
158
|
-
for (const ws of this.wss.clients) {
|
|
159
|
-
ws.send(serialized, (err) => {
|
|
160
|
-
if (err)
|
|
161
|
-
this.logger.error(`正向WS(${ws.url})上报事件失败: ` + err.message);
|
|
162
|
-
else
|
|
163
|
-
this.logger.debug(`正向WS(${ws.url})上报事件成功: ` + serialized);
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
205
|
}
|
|
168
206
|
startWsReverse(url) {
|
|
169
207
|
this._createWsr(url);
|
|
170
|
-
this.on('dispatch', (serialized) => {
|
|
171
|
-
for (const ws of this.wsr) {
|
|
172
|
-
ws.send(serialized, (err) => {
|
|
173
|
-
if (err) {
|
|
174
|
-
this.logger.error(`反向WS(${ws.url})上报事件失败: ` + err.message);
|
|
175
|
-
}
|
|
176
|
-
else
|
|
177
|
-
this.logger.debug(`反向WS(${ws.url})上报事件成功: ` + serialized);
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
208
|
}
|
|
182
209
|
async stop(force) {
|
|
183
210
|
if (this.client.status === icqq_1.OnlineStatus.Online) {
|
|
@@ -192,19 +219,20 @@ class V11 extends events_1.EventEmitter {
|
|
|
192
219
|
}
|
|
193
220
|
system_online(data) {
|
|
194
221
|
this.logger.info("【好友列表】");
|
|
195
|
-
this.client.fl.forEach(item => this.logger.info(`\t${item.nickname}(${item.user_id})`));
|
|
222
|
+
this.client.fl.forEach((item) => this.logger.info(`\t${item.nickname}(${item.user_id})`));
|
|
196
223
|
this.logger.info("【群列表】");
|
|
197
|
-
this.client.gl.forEach(item => this.logger.info(`\t${item.group_name}(${item.group_id})`));
|
|
198
|
-
this.logger.info(
|
|
224
|
+
this.client.gl.forEach((item) => this.logger.info(`\t${item.group_name}(${item.group_id})`));
|
|
225
|
+
this.logger.info("");
|
|
199
226
|
}
|
|
200
227
|
async dispatch(data) {
|
|
201
|
-
data.post_type = data.post_type ||
|
|
202
|
-
if (data.message && data.post_type ===
|
|
203
|
-
if (this.config.post_message_format ===
|
|
228
|
+
data.post_type = data.post_type || "system";
|
|
229
|
+
if (data.message && data.post_type === "message") {
|
|
230
|
+
if (this.config.post_message_format === "array") {
|
|
204
231
|
data.message = (0, icqq_cq_enable_1.toSegment)(data.message);
|
|
205
|
-
if (data.source) {
|
|
232
|
+
if (data.source) {
|
|
233
|
+
// reply
|
|
206
234
|
let msg0 = data.message[0];
|
|
207
|
-
msg0.data[
|
|
235
|
+
msg0.data["id"] = await this.getReplyMsgIdFromDB(data);
|
|
208
236
|
}
|
|
209
237
|
}
|
|
210
238
|
else {
|
|
@@ -221,7 +249,7 @@ class V11 extends events_1.EventEmitter {
|
|
|
221
249
|
if (data.message_id) {
|
|
222
250
|
data.message_id = await this.addMsgToDB(data);
|
|
223
251
|
}
|
|
224
|
-
if (data.post_type ==
|
|
252
|
+
if (data.post_type == "notice" && String(data.notice_type).endsWith("_recall")) {
|
|
225
253
|
this.db.markMsgAsRecalled(data.base64_id);
|
|
226
254
|
}
|
|
227
255
|
if (data.font) {
|
|
@@ -234,39 +262,44 @@ class V11 extends events_1.EventEmitter {
|
|
|
234
262
|
// if (!['user_id', 'group_id', 'discuss_id', 'member_id', 'channel_id', 'guild_id'].includes(key)) return value
|
|
235
263
|
// return value + ''
|
|
236
264
|
// })
|
|
237
|
-
this.emit(
|
|
265
|
+
this.emit("dispatch", this._formatEvent(data));
|
|
238
266
|
}
|
|
239
267
|
_formatEvent(data) {
|
|
240
|
-
if (data.post_type ===
|
|
268
|
+
if (data.post_type === "notice") {
|
|
241
269
|
// console.log(JSON.stringify(data))
|
|
242
270
|
const data1 = { ...data };
|
|
243
|
-
if (data.notice_type ===
|
|
271
|
+
if (data.notice_type === "group") {
|
|
244
272
|
delete data1.group;
|
|
245
273
|
delete data1.member;
|
|
246
274
|
switch (data.sub_type) {
|
|
247
|
-
case
|
|
248
|
-
data1.sub_type =
|
|
275
|
+
case "decrease":
|
|
276
|
+
data1.sub_type =
|
|
277
|
+
data.operator_id === data.user_id
|
|
278
|
+
? "leave"
|
|
279
|
+
: data.user_id === this.client.uin
|
|
280
|
+
? "kick_me"
|
|
281
|
+
: "kick";
|
|
249
282
|
data1.notice_type = `${data.notice_type}_${data.sub_type}`;
|
|
250
283
|
break;
|
|
251
|
-
case
|
|
284
|
+
case "increase":
|
|
252
285
|
data1.notice_type = `${data.notice_type}_${data.sub_type}`;
|
|
253
|
-
data1.sub_type =
|
|
286
|
+
data1.sub_type = "approve"; // todo 尚未实现
|
|
254
287
|
data1.operator_id = data1.user_id; // todo 尚未实现
|
|
255
288
|
break;
|
|
256
|
-
case
|
|
289
|
+
case "ban":
|
|
257
290
|
data1.notice_type = `${data.notice_type}_${data.sub_type}`;
|
|
258
|
-
data1.subtype = data.duration ?
|
|
291
|
+
data1.subtype = data.duration ? "ban" : "lift_ban";
|
|
259
292
|
break;
|
|
260
|
-
case
|
|
293
|
+
case "recall":
|
|
261
294
|
data1.notice_type = `${data.notice_type}_${data.sub_type}`;
|
|
262
295
|
delete data1.sub_type;
|
|
263
296
|
break;
|
|
264
|
-
case
|
|
297
|
+
case "admin":
|
|
265
298
|
data1.notice_type = `${data.notice_type}_${data.sub_type}`;
|
|
266
|
-
data1.sub_type = data.set ?
|
|
299
|
+
data1.sub_type = data.set ? "set" : "unset";
|
|
267
300
|
break;
|
|
268
|
-
case
|
|
269
|
-
data1.notice_type =
|
|
301
|
+
case "poke":
|
|
302
|
+
data1.notice_type = "notify";
|
|
270
303
|
data1.user_id = data.operator_id;
|
|
271
304
|
break;
|
|
272
305
|
default:
|
|
@@ -276,10 +309,10 @@ class V11 extends events_1.EventEmitter {
|
|
|
276
309
|
else {
|
|
277
310
|
delete data1.friend;
|
|
278
311
|
switch (data.sub_type) {
|
|
279
|
-
case
|
|
312
|
+
case "increase":
|
|
280
313
|
data1.notice_type = `friend_add`;
|
|
281
314
|
break;
|
|
282
|
-
case
|
|
315
|
+
case "recall":
|
|
283
316
|
data1.notice_type = `friend_recall`;
|
|
284
317
|
break;
|
|
285
318
|
default:
|
|
@@ -293,7 +326,8 @@ class V11 extends events_1.EventEmitter {
|
|
|
293
326
|
}
|
|
294
327
|
}
|
|
295
328
|
async addMsgToDB(data) {
|
|
296
|
-
if (!data.sender || !(
|
|
329
|
+
if (!data.sender || !("user_id" in data.sender)) {
|
|
330
|
+
// eg. notice
|
|
297
331
|
return;
|
|
298
332
|
}
|
|
299
333
|
let msg = new db_entities_1.MsgEntry();
|
|
@@ -301,13 +335,13 @@ class V11 extends events_1.EventEmitter {
|
|
|
301
335
|
msg.seq = data.seq;
|
|
302
336
|
msg.user_id = data.sender.user_id;
|
|
303
337
|
msg.nickname = data.sender.nickname;
|
|
304
|
-
if (data.message_type ===
|
|
338
|
+
if (data.message_type === "group") {
|
|
305
339
|
msg.group_id = data.group_id;
|
|
306
|
-
msg.group_name = data["group_name"] ||
|
|
340
|
+
msg.group_name = data["group_name"] || ""; // 可能不存在(gocq默认不发)
|
|
307
341
|
}
|
|
308
342
|
else {
|
|
309
343
|
msg.group_id = 0;
|
|
310
|
-
msg.group_name =
|
|
344
|
+
msg.group_name = "";
|
|
311
345
|
}
|
|
312
346
|
msg.content = data.cqCode;
|
|
313
347
|
return await this.db.addOrUpdateMsg(msg);
|
|
@@ -325,23 +359,26 @@ class V11 extends events_1.EventEmitter {
|
|
|
325
359
|
msg.base64_id = base64_id;
|
|
326
360
|
msg.seq = seq;
|
|
327
361
|
msg.user_id = user_id;
|
|
328
|
-
msg.nickname =
|
|
362
|
+
msg.nickname = "";
|
|
329
363
|
msg.group_id = group_id;
|
|
330
|
-
msg.
|
|
364
|
+
msg.group_name = "";
|
|
365
|
+
msg.content = "";
|
|
331
366
|
return await this.db.addOrUpdateMsg(msg);
|
|
332
367
|
}
|
|
333
368
|
async getReplyMsgIdFromDB(data) {
|
|
334
|
-
let group_id =
|
|
369
|
+
let group_id = data.message_type === "group" ? data.group_id : 0;
|
|
335
370
|
let msg = await this.db.getMsgByParams(data.source.user_id, group_id, data.source.seq);
|
|
336
371
|
return msg ? msg.id : 0;
|
|
337
372
|
}
|
|
338
373
|
async _httpRequestHandler(ctx) {
|
|
339
|
-
if (ctx.method ===
|
|
340
|
-
return ctx
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
374
|
+
if (ctx.method === "OPTIONS") {
|
|
375
|
+
return ctx
|
|
376
|
+
.writeHead(200, {
|
|
377
|
+
"Access-Control-Allow-Origin": "*",
|
|
378
|
+
"Access-Control-Allow-Methods": "POST, GET, OPTIONS",
|
|
379
|
+
"Access-Control-Allow-Headers": "Content-Type, authorization",
|
|
380
|
+
})
|
|
381
|
+
.end();
|
|
345
382
|
}
|
|
346
383
|
const url = new url_1.URL(ctx.url, `http://127.0.0.1`);
|
|
347
384
|
if (this.config.access_token) {
|
|
@@ -360,7 +397,7 @@ class V11 extends events_1.EventEmitter {
|
|
|
360
397
|
ctx.res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
361
398
|
if (this.config.enable_cors)
|
|
362
399
|
ctx.res.setHeader("Access-Control-Allow-Origin", "*");
|
|
363
|
-
const action = url.pathname.replace(`${this.path}`,
|
|
400
|
+
const action = url.pathname.replace(`${this.path}`, "").slice(1);
|
|
364
401
|
if (ctx.method === "GET") {
|
|
365
402
|
try {
|
|
366
403
|
const ret = await this.apply({ action, params: ctx.query });
|
|
@@ -372,7 +409,10 @@ class V11 extends events_1.EventEmitter {
|
|
|
372
409
|
}
|
|
373
410
|
else if (ctx.method === "POST") {
|
|
374
411
|
try {
|
|
375
|
-
const params = {
|
|
412
|
+
const params = {
|
|
413
|
+
...(ctx.request.query || {}),
|
|
414
|
+
...(ctx.request.body || {}),
|
|
415
|
+
};
|
|
376
416
|
const ret = await this.apply({ action, params });
|
|
377
417
|
ctx.res.writeHead(200).end(ret);
|
|
378
418
|
}
|
|
@@ -403,7 +443,7 @@ class V11 extends events_1.EventEmitter {
|
|
|
403
443
|
status: "async",
|
|
404
444
|
data: null,
|
|
405
445
|
error: null,
|
|
406
|
-
echo: data.echo
|
|
446
|
+
echo: data.echo,
|
|
407
447
|
});
|
|
408
448
|
}
|
|
409
449
|
else {
|
|
@@ -413,7 +453,7 @@ class V11 extends events_1.EventEmitter {
|
|
|
413
453
|
}
|
|
414
454
|
catch (e) {
|
|
415
455
|
let code, message;
|
|
416
|
-
if (e instanceof
|
|
456
|
+
if (e instanceof onebot_1.NotFoundError) {
|
|
417
457
|
code = 1404;
|
|
418
458
|
message = "不支持的api";
|
|
419
459
|
}
|
|
@@ -426,11 +466,12 @@ class V11 extends events_1.EventEmitter {
|
|
|
426
466
|
status: "failed",
|
|
427
467
|
data: null,
|
|
428
468
|
error: {
|
|
429
|
-
code,
|
|
469
|
+
code,
|
|
470
|
+
message,
|
|
430
471
|
},
|
|
431
472
|
echo: data?.echo,
|
|
432
473
|
msg: e.message,
|
|
433
|
-
action: data.action
|
|
474
|
+
action: data.action,
|
|
434
475
|
}));
|
|
435
476
|
}
|
|
436
477
|
});
|
|
@@ -501,9 +542,6 @@ class V11 extends events_1.EventEmitter {
|
|
|
501
542
|
*/
|
|
502
543
|
async apply(req) {
|
|
503
544
|
let { action, params, echo } = req;
|
|
504
|
-
if (typeof params.message_id == 'number' || /^\d+$/.test(params.message_id)) {
|
|
505
|
-
params.message_id = (await this.db.getMsgById(params.message_id)).id; // 调用api时把本地的数字id转为base64发给icqq
|
|
506
|
-
}
|
|
507
545
|
action = (0, utils_1.toLine)(action);
|
|
508
546
|
let is_async = action.includes("_async");
|
|
509
547
|
if (is_async)
|
|
@@ -511,10 +549,9 @@ class V11 extends events_1.EventEmitter {
|
|
|
511
549
|
let is_queue = action.includes("_rate_limited");
|
|
512
550
|
if (is_queue)
|
|
513
551
|
action = action.replace("_rate_limited", "");
|
|
514
|
-
if (action ===
|
|
515
|
-
if (
|
|
552
|
+
if (action === "send_msg") {
|
|
553
|
+
if (sendMsgTypes.includes(params.message_type))
|
|
516
554
|
action = "send_" + params.message_type + "_msg";
|
|
517
|
-
}
|
|
518
555
|
else if (params.user_id)
|
|
519
556
|
action = "send_private_msg";
|
|
520
557
|
else if (params.group_id)
|
|
@@ -522,81 +559,95 @@ class V11 extends events_1.EventEmitter {
|
|
|
522
559
|
else if (params.discuss_id)
|
|
523
560
|
action = "send_discuss_msg";
|
|
524
561
|
else
|
|
525
|
-
throw new Error(
|
|
562
|
+
throw new Error("required message_type or input (user_id/group_id)");
|
|
563
|
+
}
|
|
564
|
+
else if (action === "send_like") {
|
|
565
|
+
action = "send_user_like";
|
|
526
566
|
}
|
|
527
|
-
if (action === 'send_like')
|
|
528
|
-
action = 'send_user_like';
|
|
529
567
|
const method = (0, utils_1.toHump)(action);
|
|
530
|
-
if (Reflect.has(this.action, method))
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
if (onebot_2.BOOLS.includes(k))
|
|
540
|
-
params[k] = (0, utils_1.toBool)(params[k]);
|
|
541
|
-
if (k === 'message') {
|
|
542
|
-
if (typeof params[k] === 'string') {
|
|
543
|
-
if (/[CQ:music,type=.+,id=.+]/.test(params[k])) {
|
|
544
|
-
params[k] = params[k].replace(',type=', ',platform=');
|
|
545
|
-
}
|
|
546
|
-
params[k] = (0, icqq_cq_enable_1.fromCqcode)(params[k]);
|
|
547
|
-
}
|
|
548
|
-
else {
|
|
549
|
-
if (params[k][0].type == 'music' && params[k][0]?.data?.type) {
|
|
550
|
-
params[k][0].data.platform = params[k][0].data.type;
|
|
551
|
-
delete params[k][0].data.type;
|
|
552
|
-
}
|
|
553
|
-
params[k] = (0, icqq_cq_enable_1.fromSegment)(params[k]);
|
|
554
|
-
}
|
|
555
|
-
params['message_id'] = params[k].find(e => e.type === 'reply')?.message_id;
|
|
556
|
-
}
|
|
557
|
-
args.push(params[k]);
|
|
558
|
-
}
|
|
568
|
+
if (!Reflect.has(this.action, method))
|
|
569
|
+
throw new onebot_1.NotFoundError();
|
|
570
|
+
const processOBMessage = (message) => {
|
|
571
|
+
if (typeof message === "string")
|
|
572
|
+
message = (0, icqq_cq_enable_1.fromCqcode)(message);
|
|
573
|
+
const [firstSeg] = message;
|
|
574
|
+
if (firstSeg.type == "music" && firstSeg?.data?.type) {
|
|
575
|
+
firstSeg.data.platform = firstSeg.data.type;
|
|
576
|
+
delete firstSeg.data.type;
|
|
559
577
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
578
|
+
return (0, icqq_cq_enable_1.fromSegment)(message);
|
|
579
|
+
};
|
|
580
|
+
for (const key of Object.keys(params)) {
|
|
581
|
+
if (onebot_1.BOOLS.includes(key))
|
|
582
|
+
params[key] = (0, utils_1.toBool)(params[key]);
|
|
583
|
+
else if (key === "message") {
|
|
584
|
+
const message = processOBMessage(params[key]);
|
|
585
|
+
const replyFilter = (e) => e.type === "reply";
|
|
586
|
+
const replyElem = message.find(replyFilter);
|
|
587
|
+
const messageId = replyElem?.id ?? replyElem?.message_id;
|
|
588
|
+
if (messageId)
|
|
589
|
+
params.message_id = messageId;
|
|
590
|
+
params.message = message.filter((e) => !replyFilter(e));
|
|
565
591
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
592
|
+
}
|
|
593
|
+
if (action === "get_msg")
|
|
594
|
+
params.onebot_id = params.message_id;
|
|
595
|
+
if (typeof params.message_id === "number" || /^\d+$/.test(params.message_id)) {
|
|
596
|
+
params.message_id = (await this.db.getMsgById(params.message_id)).base64_id; // 调用api时把本地的数字id转为base64发给icqq
|
|
597
|
+
}
|
|
598
|
+
// this.logger.info(`处理过后的请求 params: `, params)
|
|
599
|
+
const orgArgNameList = String(Reflect.get(this.action, method))
|
|
600
|
+
.match(/\(.*\)/)?.[0]
|
|
601
|
+
.replace("(", "")
|
|
602
|
+
.replace(")", "")
|
|
603
|
+
.split(",")
|
|
604
|
+
.filter(Boolean)
|
|
605
|
+
.map((v) => v.replace(/=.+/, "").trim());
|
|
606
|
+
const args = orgArgNameList.map((k) => params[k]);
|
|
607
|
+
// this.logger.info(`Action 原参数名列表: `, orgArgNameList)
|
|
608
|
+
// this.logger.info(`处理后的 Action 参数列表: `, args)
|
|
609
|
+
let ret, result;
|
|
610
|
+
if (is_queue) {
|
|
611
|
+
this._queue.push({ method, args });
|
|
612
|
+
this._runQueue();
|
|
613
|
+
result = V11.ok(null, 0, true);
|
|
614
|
+
}
|
|
615
|
+
else {
|
|
616
|
+
try {
|
|
617
|
+
ret = await this.action[method].apply(this, args);
|
|
618
|
+
}
|
|
619
|
+
catch (e) {
|
|
620
|
+
this.logger.error(e);
|
|
621
|
+
const err = V11.error(e.message);
|
|
622
|
+
if (echo)
|
|
623
|
+
err.echo = echo;
|
|
624
|
+
return JSON.stringify(err);
|
|
625
|
+
}
|
|
626
|
+
if (ret instanceof Promise) {
|
|
627
|
+
if (is_async) {
|
|
628
|
+
result = V11.ok(null, 0, true);
|
|
580
629
|
}
|
|
581
630
|
else {
|
|
582
631
|
result = V11.ok(await ret, 0, false);
|
|
583
632
|
}
|
|
584
633
|
}
|
|
585
|
-
|
|
586
|
-
result
|
|
587
|
-
if (result.data?.message)
|
|
588
|
-
result.data.message = (0, icqq_cq_enable_1.toSegment)(result.data.message);
|
|
589
|
-
// send_msg_xxx 时提前把数据写入数据库(也有可能来的比message慢,后来的话会被数据库忽略)
|
|
590
|
-
if (result.status === 'ok' && params.user_id && result.data?.message_id && result.data?.seq) {
|
|
591
|
-
result.data.message_id = await this.addMsgToDBFromSendMsgResult(params.user_id, params.group_id || 0, result.data.seq, result.data.message_id);
|
|
592
|
-
}
|
|
593
|
-
if (echo) {
|
|
594
|
-
result.echo = echo;
|
|
634
|
+
else {
|
|
635
|
+
result = V11.ok(await ret, 0, false);
|
|
595
636
|
}
|
|
596
|
-
return JSON.stringify(result);
|
|
597
637
|
}
|
|
598
|
-
|
|
599
|
-
|
|
638
|
+
if (result.data instanceof Map)
|
|
639
|
+
result.data = [...result.data.values()];
|
|
640
|
+
if (result.data?.message)
|
|
641
|
+
result.data.message = (0, icqq_cq_enable_1.toSegment)(result.data.message);
|
|
642
|
+
// send_msg_xxx 时提前把数据写入数据库(也有可能来的比message慢,后来的话会被数据库忽略)
|
|
643
|
+
if (result.status === "ok" && action.match(sendMsgMethodRegex) && result.data?.message_id && result.data?.seq) {
|
|
644
|
+
result.data.message_id = await this.addMsgToDBFromSendMsgResult(this.client.uin, // msg send resp uin is always bot uin
|
|
645
|
+
params.group_id || 0, result.data.seq, result.data.message_id);
|
|
646
|
+
}
|
|
647
|
+
if (echo) {
|
|
648
|
+
result.echo = echo;
|
|
649
|
+
}
|
|
650
|
+
return JSON.stringify(result);
|
|
600
651
|
}
|
|
601
652
|
/**
|
|
602
653
|
* 限速队列调用
|
|
@@ -621,35 +672,35 @@ exports.V11 = V11;
|
|
|
621
672
|
function ok(data, retcode = 0, pending) {
|
|
622
673
|
return {
|
|
623
674
|
retcode,
|
|
624
|
-
status: pending ?
|
|
675
|
+
status: pending ? "async" : "ok",
|
|
625
676
|
data,
|
|
626
|
-
error: null
|
|
677
|
+
error: null,
|
|
627
678
|
};
|
|
628
679
|
}
|
|
629
680
|
V11.ok = ok;
|
|
630
681
|
function error(error, retcode = 1) {
|
|
631
682
|
return {
|
|
632
683
|
retcode,
|
|
633
|
-
status:
|
|
684
|
+
status: "error",
|
|
634
685
|
data: null,
|
|
635
|
-
error
|
|
686
|
+
error,
|
|
636
687
|
};
|
|
637
688
|
}
|
|
638
689
|
V11.error = error;
|
|
639
690
|
V11.defaultConfig = {
|
|
640
691
|
heartbeat: 3,
|
|
641
|
-
access_token:
|
|
692
|
+
access_token: "",
|
|
642
693
|
post_timeout: 15,
|
|
643
|
-
secret:
|
|
694
|
+
secret: "",
|
|
644
695
|
rate_limit_interval: 4,
|
|
645
|
-
post_message_format:
|
|
696
|
+
post_message_format: "string",
|
|
646
697
|
reconnect_interval: 3,
|
|
647
698
|
use_http: true,
|
|
648
699
|
enable_cors: true,
|
|
649
700
|
enable_reissue: false,
|
|
650
701
|
use_ws: true,
|
|
651
702
|
http_reverse: [],
|
|
652
|
-
ws_reverse: []
|
|
703
|
+
ws_reverse: [],
|
|
653
704
|
};
|
|
654
705
|
function genMetaEvent(uin, type) {
|
|
655
706
|
return {
|