onebots 0.4.47 → 0.4.48
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/dist/assets/{index-yG7HG4Oa.css → index-2tNhvoZy.css} +1 -1
- package/dist/assets/index-_Kz1yWe8.js +19 -0
- package/dist/index.html +2 -2
- package/lib/adapter.js +6 -8
- package/lib/adapters/dingtalk/index.d.ts +4 -4
- package/lib/adapters/dingtalk/index.js +56 -49
- package/lib/adapters/dingtalk/utils.js +5 -20
- package/lib/adapters/icqq/index.d.ts +4 -3
- package/lib/adapters/icqq/index.js +128 -101
- package/lib/adapters/icqq/utils.js +2 -2
- package/lib/adapters/qq/index.d.ts +4 -4
- package/lib/adapters/qq/index.js +63 -52
- package/lib/adapters/qq/utils.js +5 -20
- package/lib/adapters/wechat/index.d.ts +3 -3
- package/lib/adapters/wechat/index.js +63 -52
- package/lib/adapters/wechat/utils.js +5 -20
- package/lib/bin.js +6 -6
- package/lib/db.js +10 -10
- package/lib/onebot.d.ts +12 -12
- package/lib/onebot.js +25 -16
- package/lib/server/app.d.ts +2 -2
- package/lib/server/app.js +71 -70
- package/lib/server/router.d.ts +1 -1
- package/lib/server/router.js +5 -5
- package/lib/service/V11/action/common.d.ts +1 -0
- package/lib/service/V11/action/common.js +24 -24
- package/lib/service/V11/action/friend.js +16 -8
- package/lib/service/V11/action/group.js +52 -22
- package/lib/service/V11/index.js +86 -50
- package/lib/service/V12/action/common.d.ts +3 -3
- package/lib/service/V12/action/common.js +33 -31
- package/lib/service/V12/action/friend.js +8 -4
- package/lib/service/V12/action/group.js +50 -20
- package/lib/service/V12/action/guild.js +110 -35
- package/lib/service/V12/index.d.ts +18 -18
- package/lib/service/V12/index.js +189 -147
- package/lib/service/shareMusicCustom.js +14 -9
- package/lib/service.js +29 -27
- package/lib/utils.d.ts +1 -1
- package/lib/utils.js +44 -32
- package/package.json +1 -1
- package/dist/assets/index-YN4djyjB.js +0 -19
package/lib/service/V12/index.js
CHANGED
|
@@ -22,21 +22,21 @@ class V12 extends service_1.Service {
|
|
|
22
22
|
super(oneBot.adapter, config);
|
|
23
23
|
this.oneBot = oneBot;
|
|
24
24
|
this.config = config;
|
|
25
|
-
this.version =
|
|
25
|
+
this.version = "V12";
|
|
26
26
|
this.timestamp = Date.now();
|
|
27
27
|
this.wsr = new Set();
|
|
28
|
-
this.db = new db_1.JsonDB((0, path_1.join)(app_1.App.configDir,
|
|
28
|
+
this.db = new db_1.JsonDB((0, path_1.join)(app_1.App.configDir, "data", `${this.oneBot.uin}_v12.jsondb`));
|
|
29
29
|
this.action = new action_1.Action();
|
|
30
30
|
this.logger = this.oneBot.adapter.getLogger(this.oneBot.uin, this.version);
|
|
31
31
|
}
|
|
32
32
|
addHistory(payload) {
|
|
33
|
-
return this.db.push(
|
|
33
|
+
return this.db.push("eventBuffer", payload);
|
|
34
34
|
}
|
|
35
35
|
shiftHistory() {
|
|
36
|
-
return this.db.shift(
|
|
36
|
+
return this.db.shift("eventBuffer");
|
|
37
37
|
}
|
|
38
38
|
get history() {
|
|
39
|
-
return this.db.get(
|
|
39
|
+
return this.db.get("eventBuffer", []);
|
|
40
40
|
}
|
|
41
41
|
getFile(file_id) {
|
|
42
42
|
return this.db.get(`files.${file_id}`);
|
|
@@ -50,36 +50,36 @@ class V12 extends service_1.Service {
|
|
|
50
50
|
return file_id;
|
|
51
51
|
}
|
|
52
52
|
get files() {
|
|
53
|
-
const files = this.db.get(
|
|
54
|
-
return Object.keys(files).map(
|
|
53
|
+
const files = this.db.get("files", {});
|
|
54
|
+
return Object.keys(files).map(file_id => {
|
|
55
55
|
return {
|
|
56
56
|
file_id,
|
|
57
|
-
...files[file_id]
|
|
57
|
+
...files[file_id],
|
|
58
58
|
};
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
set history(value) {
|
|
62
|
-
this.db.set(
|
|
62
|
+
this.db.set("eventBuffer", value);
|
|
63
63
|
}
|
|
64
64
|
start() {
|
|
65
65
|
if (this.config.use_http) {
|
|
66
|
-
const config = typeof this.config.use_http ===
|
|
66
|
+
const config = typeof this.config.use_http === "boolean" ? {} : this.config.use_http || {};
|
|
67
67
|
this.startHttp({
|
|
68
68
|
access_token: this.config.access_token,
|
|
69
69
|
event_enabled: true,
|
|
70
70
|
event_buffer_size: 10,
|
|
71
|
-
...config
|
|
71
|
+
...config,
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
if (this.config.use_ws) {
|
|
75
|
-
const config = typeof this.config.use_ws ===
|
|
75
|
+
const config = typeof this.config.use_ws === "boolean" ? {} : this.config.use_ws || {};
|
|
76
76
|
this.startWs({
|
|
77
77
|
access_token: this.config.access_token,
|
|
78
|
-
...config
|
|
78
|
+
...config,
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
this.config.webhook.forEach(config => {
|
|
82
|
-
if (typeof config ===
|
|
82
|
+
if (typeof config === "string") {
|
|
83
83
|
config = {
|
|
84
84
|
url: config,
|
|
85
85
|
access_token: this.config.access_token,
|
|
@@ -88,13 +88,13 @@ class V12 extends service_1.Service {
|
|
|
88
88
|
else {
|
|
89
89
|
config = {
|
|
90
90
|
access_token: this.config.access_token,
|
|
91
|
-
...config
|
|
91
|
+
...config,
|
|
92
92
|
};
|
|
93
93
|
}
|
|
94
94
|
this.startWebhook(config);
|
|
95
95
|
});
|
|
96
96
|
this.config.ws_reverse.forEach(config => {
|
|
97
|
-
if (typeof config ===
|
|
97
|
+
if (typeof config === "string") {
|
|
98
98
|
config = {
|
|
99
99
|
url: config,
|
|
100
100
|
access_token: this.config.access_token,
|
|
@@ -103,15 +103,15 @@ class V12 extends service_1.Service {
|
|
|
103
103
|
else {
|
|
104
104
|
config = {
|
|
105
105
|
access_token: this.config.access_token,
|
|
106
|
-
...config
|
|
106
|
+
...config,
|
|
107
107
|
};
|
|
108
108
|
}
|
|
109
109
|
this.startWsReverse(config);
|
|
110
110
|
});
|
|
111
|
-
this.on(
|
|
111
|
+
this.on("dispatch", unserialized => {
|
|
112
112
|
const serialized = JSON.stringify(unserialized);
|
|
113
113
|
for (const ws of this.wss?.clients) {
|
|
114
|
-
ws.send(serialized,
|
|
114
|
+
ws.send(serialized, err => {
|
|
115
115
|
if (err)
|
|
116
116
|
this.logger.error(`正向WS(${ws.url})上报事件失败: ` + err.message);
|
|
117
117
|
else
|
|
@@ -119,7 +119,7 @@ class V12 extends service_1.Service {
|
|
|
119
119
|
});
|
|
120
120
|
}
|
|
121
121
|
for (const ws of this.wsr) {
|
|
122
|
-
ws.send(serialized,
|
|
122
|
+
ws.send(serialized, err => {
|
|
123
123
|
if (err)
|
|
124
124
|
this.logger.error(`反向WS(${ws.url})上报事件失败: ` + err.message);
|
|
125
125
|
else
|
|
@@ -129,36 +129,37 @@ class V12 extends service_1.Service {
|
|
|
129
129
|
});
|
|
130
130
|
if (this.config.heartbeat) {
|
|
131
131
|
this.heartbeat = setInterval(() => {
|
|
132
|
-
this.dispatch(V12.formatPayload(this.oneBot.uin,
|
|
132
|
+
this.dispatch(V12.formatPayload(this.oneBot.uin, "heartbeat", {
|
|
133
133
|
detail_type: "heartbeat",
|
|
134
134
|
interval: new Date().getTime() + this.config.heartbeat * 1000,
|
|
135
|
-
status: this.action.getStatus.apply(this)
|
|
135
|
+
status: this.action.getStatus.apply(this),
|
|
136
136
|
}));
|
|
137
137
|
}, this.config.heartbeat * 1000);
|
|
138
138
|
}
|
|
139
|
-
this.adapter.on(
|
|
140
|
-
const payload = this.adapter.formatEventPayload(uin,
|
|
139
|
+
this.adapter.on("message.receive", (uin, event) => {
|
|
140
|
+
const payload = this.adapter.formatEventPayload(uin, "V12", "message", event);
|
|
141
141
|
this.dispatch(payload);
|
|
142
142
|
});
|
|
143
|
-
this.adapter.on(
|
|
144
|
-
const payload = this.adapter.formatEventPayload(uin,
|
|
143
|
+
this.adapter.on("notice.receive", (uin, event) => {
|
|
144
|
+
const payload = this.adapter.formatEventPayload(uin, "V12", "notice", event);
|
|
145
145
|
this.dispatch(payload);
|
|
146
146
|
});
|
|
147
|
-
this.adapter.on(
|
|
148
|
-
const payload = this.adapter.formatEventPayload(uin,
|
|
147
|
+
this.adapter.on("request.receive", (uin, event) => {
|
|
148
|
+
const payload = this.adapter.formatEventPayload(uin, "V12", "request", event);
|
|
149
149
|
this.dispatch(payload);
|
|
150
150
|
});
|
|
151
151
|
}
|
|
152
152
|
startHttp(config) {
|
|
153
|
-
this.oneBot.app.router.all(this.path,
|
|
154
|
-
this.oneBot.app.router.all(new RegExp(`^${this.path}/(.*)$`),
|
|
153
|
+
this.oneBot.app.router.all(this.path, ctx => this.httpRequestHandler(ctx, config));
|
|
154
|
+
this.oneBot.app.router.all(new RegExp(`^${this.path}/(.*)$`), ctx => this._httpRequestHandler(ctx, config));
|
|
155
155
|
this.logger.mark(`开启http服务器成功,监听:http://127.0.0.1:${this.oneBot.app.config.port}${this.path}`);
|
|
156
|
-
this.on(
|
|
157
|
-
if (![
|
|
156
|
+
this.on("dispatch", (payload) => {
|
|
157
|
+
if (!["message", "notice", "request", "meta"].includes(payload.type))
|
|
158
158
|
return;
|
|
159
159
|
if (config.event_enabled) {
|
|
160
160
|
this.addHistory(payload);
|
|
161
|
-
if (config.event_buffer_size !== 0 &&
|
|
161
|
+
if (config.event_buffer_size !== 0 &&
|
|
162
|
+
this.history.length > config.event_buffer_size)
|
|
162
163
|
this.shiftHistory();
|
|
163
164
|
}
|
|
164
165
|
});
|
|
@@ -175,24 +176,26 @@ class V12 extends service_1.Service {
|
|
|
175
176
|
},
|
|
176
177
|
};
|
|
177
178
|
if (config.access_token) {
|
|
178
|
-
options.headers[
|
|
179
|
+
options.headers["Authorization"] = `Bearer ${config.access_token}`;
|
|
179
180
|
}
|
|
180
181
|
const protocol = config.url.startsWith("https") ? https_1.default : http_1.default;
|
|
181
|
-
this.on(
|
|
182
|
+
this.on("dispatch", (unserialized) => {
|
|
182
183
|
try {
|
|
183
184
|
const serialized = JSON.stringify(unserialized);
|
|
184
|
-
protocol
|
|
185
|
+
protocol
|
|
186
|
+
.request(config.url, {
|
|
185
187
|
...options,
|
|
186
188
|
headers: {
|
|
187
189
|
...options.headers,
|
|
188
190
|
"Content-Length": Buffer.byteLength(serialized),
|
|
189
|
-
}
|
|
190
|
-
},
|
|
191
|
+
},
|
|
192
|
+
}, res => {
|
|
191
193
|
if (res.statusCode !== 200)
|
|
192
|
-
return this.logger.warn(`Webhook(${config.url})上报事件收到非200响应:` +
|
|
194
|
+
return this.logger.warn(`Webhook(${config.url})上报事件收到非200响应:` +
|
|
195
|
+
res.statusCode);
|
|
193
196
|
let data = "";
|
|
194
197
|
res.setEncoding("utf-8");
|
|
195
|
-
res.on("data",
|
|
198
|
+
res.on("data", chunk => (data += chunk));
|
|
196
199
|
res.on("end", () => {
|
|
197
200
|
this.logger.debug(`收到Webhook响应 ${res.statusCode} :` + data);
|
|
198
201
|
if (!data)
|
|
@@ -204,9 +207,11 @@ class V12 extends service_1.Service {
|
|
|
204
207
|
this.logger.error(`快速操作遇到错误:` + e.message);
|
|
205
208
|
}
|
|
206
209
|
});
|
|
207
|
-
})
|
|
210
|
+
})
|
|
211
|
+
.on("error", err => {
|
|
208
212
|
this.logger.error(`Webhook(${config.url})上报事件失败:` + err.message);
|
|
209
|
-
})
|
|
213
|
+
})
|
|
214
|
+
.end(serialized, () => {
|
|
210
215
|
this.logger.debug(`Webhook(${config.url})上报事件成功: ` + serialized);
|
|
211
216
|
});
|
|
212
217
|
}
|
|
@@ -215,28 +220,30 @@ class V12 extends service_1.Service {
|
|
|
215
220
|
}
|
|
216
221
|
});
|
|
217
222
|
if (config.get_latest_actions) {
|
|
218
|
-
const interval =
|
|
219
|
-
config.get_latest_actions.interval * 1000
|
|
220
|
-
1000 * 60;
|
|
223
|
+
const interval = config.get_latest_actions && typeof config.get_latest_actions === "object"
|
|
224
|
+
? config.get_latest_actions.interval * 1000
|
|
225
|
+
: 1000 * 60;
|
|
221
226
|
setInterval(() => {
|
|
222
227
|
try {
|
|
223
|
-
const actionPath = typeof config.get_latest_actions ===
|
|
224
|
-
config.get_latest_actions
|
|
225
|
-
typeof config.get_latest_actions ===
|
|
226
|
-
|
|
227
|
-
config.get_latest_actions.path ||
|
|
228
|
-
protocol
|
|
228
|
+
const actionPath = typeof config.get_latest_actions === "string"
|
|
229
|
+
? config.get_latest_actions
|
|
230
|
+
: typeof config.get_latest_actions === "boolean"
|
|
231
|
+
? "get_latest_actions"
|
|
232
|
+
: config.get_latest_actions.path || "/get_latest_actions";
|
|
233
|
+
protocol
|
|
234
|
+
.request(`${config.url}${actionPath}`, {
|
|
229
235
|
...options,
|
|
230
|
-
method:
|
|
236
|
+
method: "GET",
|
|
231
237
|
headers: {
|
|
232
238
|
...options.headers,
|
|
233
|
-
}
|
|
234
|
-
},
|
|
239
|
+
},
|
|
240
|
+
}, res => {
|
|
235
241
|
if (res.statusCode !== 200)
|
|
236
|
-
return this.logger.warn(`Webhook(${config.url})获取动作队列收到非200响应:` +
|
|
242
|
+
return this.logger.warn(`Webhook(${config.url})获取动作队列收到非200响应:` +
|
|
243
|
+
res.statusCode);
|
|
237
244
|
let data = "";
|
|
238
245
|
res.setEncoding("utf-8");
|
|
239
|
-
res.on("data",
|
|
246
|
+
res.on("data", chunk => (data += chunk));
|
|
240
247
|
res.on("end", () => {
|
|
241
248
|
this.logger.info(`获取动作队列响应 ${res.statusCode} :` + data);
|
|
242
249
|
if (!data)
|
|
@@ -248,9 +255,11 @@ class V12 extends service_1.Service {
|
|
|
248
255
|
this.logger.error(`执行动作报错:` + e.message);
|
|
249
256
|
}
|
|
250
257
|
});
|
|
251
|
-
})
|
|
258
|
+
})
|
|
259
|
+
.on("error", err => {
|
|
252
260
|
this.logger.error(`Webhook(${config.url})获取动作队列失败:` + err.message);
|
|
253
|
-
})
|
|
261
|
+
})
|
|
262
|
+
.end();
|
|
254
263
|
}
|
|
255
264
|
catch (e) {
|
|
256
265
|
this.logger.error(`Webhook(${config.url})获取动作队列失败:` + e.message);
|
|
@@ -266,12 +275,12 @@ class V12 extends service_1.Service {
|
|
|
266
275
|
startWs(config) {
|
|
267
276
|
this.wss = this.oneBot.app.router.ws(this.path, this.oneBot.app.httpServer);
|
|
268
277
|
this.logger.mark(`开启ws服务器成功,监听:ws://127.0.0.1:${this.oneBot.app.config.port}${this.path}`);
|
|
269
|
-
this.wss.on("error",
|
|
278
|
+
this.wss.on("error", err => {
|
|
270
279
|
this.logger.error(err.message);
|
|
271
280
|
});
|
|
272
281
|
this.wss.on("connection", (ws, req) => {
|
|
273
282
|
this.logger.info(`ws客户端(${req.url})已连接`);
|
|
274
|
-
ws.on("error",
|
|
283
|
+
ws.on("error", err => {
|
|
275
284
|
this.logger.error(`ws客户端(${req.url})报错:${err.message}`);
|
|
276
285
|
});
|
|
277
286
|
ws.on("close", (code, reason) => {
|
|
@@ -279,10 +288,11 @@ class V12 extends service_1.Service {
|
|
|
279
288
|
});
|
|
280
289
|
if (config.access_token) {
|
|
281
290
|
const url = new url_1.URL(req.url, "http://127.0.0.1");
|
|
282
|
-
const token = url.searchParams.get(
|
|
291
|
+
const token = url.searchParams.get("access_token");
|
|
283
292
|
if (token)
|
|
284
293
|
req.headers["authorization"] = `Bearer ${token}`;
|
|
285
|
-
if (!req.headers["authorization"] ||
|
|
294
|
+
if (!req.headers["authorization"] ||
|
|
295
|
+
req.headers["authorization"] !== `Bearer ${config.access_token}`)
|
|
286
296
|
return ws.close(401, "wrong access token");
|
|
287
297
|
}
|
|
288
298
|
this._webSocketHandler(ws);
|
|
@@ -298,87 +308,95 @@ class V12 extends service_1.Service {
|
|
|
298
308
|
}
|
|
299
309
|
}
|
|
300
310
|
format(event, ...args) {
|
|
301
|
-
const data =
|
|
311
|
+
const data = typeof args[0] === "object" ? args.shift() || {} : {};
|
|
302
312
|
data.type = data.post_type;
|
|
303
313
|
if (!data.type) {
|
|
304
|
-
data.type =
|
|
305
|
-
data.detail_type =
|
|
314
|
+
data.type = "meta";
|
|
315
|
+
data.detail_type = "online";
|
|
306
316
|
if (data.image) {
|
|
307
|
-
data.type =
|
|
308
|
-
data.detail_type =
|
|
317
|
+
data.type = "login";
|
|
318
|
+
data.detail_type = "qrcode";
|
|
309
319
|
}
|
|
310
320
|
else if (data.url) {
|
|
311
|
-
data.type =
|
|
312
|
-
data.detail_type =
|
|
321
|
+
data.type = "login";
|
|
322
|
+
data.detail_type = "slider";
|
|
313
323
|
if (data.phone) {
|
|
314
|
-
data.detail_type =
|
|
324
|
+
data.detail_type = "device";
|
|
315
325
|
}
|
|
316
326
|
}
|
|
317
327
|
else if (data.message) {
|
|
318
|
-
data.type =
|
|
319
|
-
data.detial_type =
|
|
328
|
+
data.type = "login";
|
|
329
|
+
data.detial_type = "error";
|
|
320
330
|
}
|
|
321
331
|
}
|
|
322
|
-
if (data.type ===
|
|
332
|
+
if (data.type === "notice") {
|
|
323
333
|
switch (data.detail_type) {
|
|
324
|
-
case
|
|
325
|
-
if ([
|
|
326
|
-
data.detail_type =
|
|
327
|
-
else if (data.sub_type ===
|
|
328
|
-
data.detail_type =
|
|
334
|
+
case "friend":
|
|
335
|
+
if (["increase", "decrease"].includes(data.sub_type))
|
|
336
|
+
data.detail_type = "friend_" + data.sub_type;
|
|
337
|
+
else if (data.sub_type === "recall")
|
|
338
|
+
data.detail_type = "private_message_delete";
|
|
329
339
|
break;
|
|
330
|
-
case
|
|
331
|
-
if ([
|
|
332
|
-
data.detail_type =
|
|
333
|
-
else if (data.sub_type ===
|
|
334
|
-
data.detail_type =
|
|
340
|
+
case "group":
|
|
341
|
+
if (["increase", "decrease"].includes(data.sub_type))
|
|
342
|
+
data.detail_type = "group_member_" + data.sub_type;
|
|
343
|
+
else if (data.sub_type === "recall")
|
|
344
|
+
data.detail_type = "group_message_delete";
|
|
335
345
|
}
|
|
336
346
|
}
|
|
337
|
-
if (data.type ===
|
|
338
|
-
data.type =
|
|
347
|
+
if (data.type === "system")
|
|
348
|
+
data.type = "meta";
|
|
339
349
|
data.alt_message = data.raw_message;
|
|
340
350
|
data.self = this.action.getSelfInfo.apply(this);
|
|
341
351
|
if (!data.detail_type)
|
|
342
|
-
data.detail_type =
|
|
343
|
-
|
|
352
|
+
data.detail_type =
|
|
353
|
+
data.message_type || data.notice_type || data.request_type || data.system_type;
|
|
354
|
+
data.message =
|
|
355
|
+
data.type === "message" ? this.adapter.toSegment("V12", data.message) : data.message;
|
|
344
356
|
if (data.source)
|
|
345
357
|
data.source = {
|
|
346
358
|
...data.source,
|
|
347
|
-
message_id: data.detail_type ===
|
|
348
|
-
(0, message_1.genDmMessageId)(data.source.user_id, data.source.seq, data.source.rand, data.source.time)
|
|
349
|
-
(0, message_1.genGroupMessageId)(data.group_id, data.source.user_id, data.source.seq, data.source.rand, data.source.time),
|
|
359
|
+
message_id: data.detail_type === "private"
|
|
360
|
+
? (0, message_1.genDmMessageId)(data.source.user_id, data.source.seq, data.source.rand, data.source.time)
|
|
361
|
+
: (0, message_1.genGroupMessageId)(data.group_id, data.source.user_id, data.source.seq, data.source.rand, data.source.time),
|
|
350
362
|
user_id: data.source.user_id,
|
|
351
363
|
message: data.source.message,
|
|
352
364
|
};
|
|
353
365
|
return V12.formatPayload(this.oneBot.uin, event, data);
|
|
354
366
|
}
|
|
355
|
-
system_online(data) {
|
|
356
|
-
}
|
|
367
|
+
system_online(data) { }
|
|
357
368
|
async dispatch(data) {
|
|
358
369
|
const payload = {
|
|
359
370
|
id: (0, utils_2.uuid)(),
|
|
360
|
-
impl:
|
|
371
|
+
impl: "onebots",
|
|
361
372
|
version: 12,
|
|
362
373
|
platform: this.oneBot.platform,
|
|
363
374
|
self: {
|
|
364
375
|
platform: this.oneBot.platform,
|
|
365
|
-
user_id: `${this.oneBot.uin}
|
|
376
|
+
user_id: `${this.oneBot.uin}`,
|
|
366
377
|
},
|
|
367
378
|
};
|
|
368
379
|
Object.assign(payload, (0, utils_2.transformObj)(data, (key, value) => {
|
|
369
|
-
if (![
|
|
380
|
+
if (![
|
|
381
|
+
"user_id",
|
|
382
|
+
"group_id",
|
|
383
|
+
"discuss_id",
|
|
384
|
+
"member_id",
|
|
385
|
+
"channel_id",
|
|
386
|
+
"guild_id",
|
|
387
|
+
].includes(key))
|
|
370
388
|
return value;
|
|
371
|
-
return value +
|
|
389
|
+
return value + "";
|
|
372
390
|
}), {
|
|
373
391
|
self_id: `${this.oneBot.uin}`,
|
|
374
392
|
self: {
|
|
375
393
|
platform: this.oneBot.platform,
|
|
376
|
-
user_id: `${this.oneBot.uin}
|
|
394
|
+
user_id: `${this.oneBot.uin}`,
|
|
377
395
|
},
|
|
378
396
|
});
|
|
379
397
|
if (!this.filterFn(payload))
|
|
380
398
|
return;
|
|
381
|
-
this.emit(
|
|
399
|
+
this.emit("dispatch", payload);
|
|
382
400
|
}
|
|
383
401
|
async apply(req) {
|
|
384
402
|
let { action = "", params = {}, echo } = req;
|
|
@@ -386,8 +404,8 @@ class V12 extends service_1.Service {
|
|
|
386
404
|
let is_async = action.includes("_async");
|
|
387
405
|
if (is_async)
|
|
388
406
|
action = action.replace("_async", "");
|
|
389
|
-
if (action ===
|
|
390
|
-
if (["private", "group", "discuss",
|
|
407
|
+
if (action === "send_message") {
|
|
408
|
+
if (["private", "group", "discuss", "direct", "guild"].includes(params.detail_type)) {
|
|
391
409
|
action = "send_" + params.detail_type + "_msg";
|
|
392
410
|
}
|
|
393
411
|
else if (params.user_id)
|
|
@@ -399,35 +417,37 @@ class V12 extends service_1.Service {
|
|
|
399
417
|
else if (params.channel_id)
|
|
400
418
|
action = "send_guild_msg";
|
|
401
419
|
else if (params.guild_id)
|
|
402
|
-
action =
|
|
420
|
+
action = "send_direct_msg";
|
|
403
421
|
else
|
|
404
|
-
throw new Error(
|
|
422
|
+
throw new Error("required detail_type or input (user_id/group_id/guild_id/channel_id)");
|
|
405
423
|
}
|
|
406
424
|
const method = (0, utils_2.toHump)(action);
|
|
407
425
|
if (Reflect.has(this.action, method)) {
|
|
408
|
-
const ARGS = String(Reflect.get(this.action, method))
|
|
426
|
+
const ARGS = String(Reflect.get(this.action, method))
|
|
427
|
+
.match(/\(.*\)/)?.[0]
|
|
409
428
|
.replace("(", "")
|
|
410
429
|
.replace(")", "")
|
|
411
430
|
.split(",")
|
|
412
|
-
.filter(Boolean)
|
|
431
|
+
.filter(Boolean)
|
|
432
|
+
.map(v => v.replace(/=.+/, "").trim());
|
|
413
433
|
const args = [];
|
|
414
434
|
for (let k of ARGS) {
|
|
415
435
|
if (Reflect.has(params, k)) {
|
|
416
436
|
if (onebot_1.BOOLS.includes(k))
|
|
417
437
|
params[k] = (0, utils_2.toBool)(params[k]);
|
|
418
|
-
if (k ===
|
|
419
|
-
if (typeof params[k] ===
|
|
438
|
+
if (k === "message") {
|
|
439
|
+
if (typeof params[k] === "string") {
|
|
420
440
|
if (/[CQ:music,type=.+,id=.+]/.test(params[k])) {
|
|
421
|
-
params[k] = params[k].replace(
|
|
441
|
+
params[k] = params[k].replace(",type=", ",platform=");
|
|
422
442
|
}
|
|
423
|
-
params[k] = this.adapter.fromCqcode(
|
|
443
|
+
params[k] = this.adapter.fromCqcode("V12", params[k]);
|
|
424
444
|
}
|
|
425
445
|
else {
|
|
426
|
-
if (params[k][0].type ==
|
|
446
|
+
if (params[k][0].type == "music" && params[k][0]?.data?.type) {
|
|
427
447
|
params[k][0].data.platform = params[k][0].data.type;
|
|
428
448
|
delete params[k][0].data.type;
|
|
429
449
|
}
|
|
430
|
-
params[k] = this.adapter.fromSegment(
|
|
450
|
+
params[k] = this.adapter.fromSegment("V12", params[k]);
|
|
431
451
|
}
|
|
432
452
|
}
|
|
433
453
|
args.push(params[k]);
|
|
@@ -455,7 +475,7 @@ class V12 extends service_1.Service {
|
|
|
455
475
|
if (result.data instanceof Map)
|
|
456
476
|
result.data = [...result.data.values()];
|
|
457
477
|
if (result.data?.message)
|
|
458
|
-
result.data.message = this.adapter.toSegment(
|
|
478
|
+
result.data.message = this.adapter.toSegment("V12", result.data.message);
|
|
459
479
|
if (echo) {
|
|
460
480
|
result.echo = echo;
|
|
461
481
|
}
|
|
@@ -465,12 +485,14 @@ class V12 extends service_1.Service {
|
|
|
465
485
|
throw new onebot_1.NotFoundError();
|
|
466
486
|
}
|
|
467
487
|
async httpAuth(ctx, config) {
|
|
468
|
-
if (ctx.method ===
|
|
469
|
-
return ctx
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
488
|
+
if (ctx.method === "OPTIONS") {
|
|
489
|
+
return ctx
|
|
490
|
+
.writeHead(200, {
|
|
491
|
+
"Access-Control-Allow-Origin": "*",
|
|
492
|
+
"Access-Control-Allow-Methods": "POST, GET, OPTIONS",
|
|
493
|
+
"Access-Control-Allow-Headers": "Content-Type, authorization",
|
|
494
|
+
})
|
|
495
|
+
.end();
|
|
474
496
|
}
|
|
475
497
|
const url = new url_1.URL(ctx.url, `http://127.0.0.1`);
|
|
476
498
|
if (config.access_token) {
|
|
@@ -504,7 +526,10 @@ class V12 extends service_1.Service {
|
|
|
504
526
|
}
|
|
505
527
|
else if (ctx.method === "POST") {
|
|
506
528
|
try {
|
|
507
|
-
const params = {
|
|
529
|
+
const params = {
|
|
530
|
+
...(ctx.request.query || {}),
|
|
531
|
+
...(ctx.request.body || {}),
|
|
532
|
+
};
|
|
508
533
|
const ret = await this.apply(params);
|
|
509
534
|
ctx.res.writeHead(200).end(ret);
|
|
510
535
|
}
|
|
@@ -520,7 +545,7 @@ class V12 extends service_1.Service {
|
|
|
520
545
|
if (await this.httpAuth(ctx, config))
|
|
521
546
|
return;
|
|
522
547
|
const url = new url_1.URL(ctx.url, `http://127.0.0.1`);
|
|
523
|
-
const action = url.pathname.replace(`${this.path}`,
|
|
548
|
+
const action = url.pathname.replace(`${this.path}`, "").slice(1);
|
|
524
549
|
if (ctx.method === "GET") {
|
|
525
550
|
try {
|
|
526
551
|
const ret = await this.apply({ action, params: ctx.query });
|
|
@@ -532,7 +557,10 @@ class V12 extends service_1.Service {
|
|
|
532
557
|
}
|
|
533
558
|
else if (ctx.method === "POST") {
|
|
534
559
|
try {
|
|
535
|
-
const params = {
|
|
560
|
+
const params = {
|
|
561
|
+
...(ctx.request.query || {}),
|
|
562
|
+
...(ctx.request.body || {}),
|
|
563
|
+
};
|
|
536
564
|
const ret = await this.apply({ action, params });
|
|
537
565
|
ctx.res.writeHead(200).end(ret);
|
|
538
566
|
}
|
|
@@ -558,16 +586,29 @@ class V12 extends service_1.Service {
|
|
|
558
586
|
}
|
|
559
587
|
if (event.detail_type === "group") {
|
|
560
588
|
if (res.delete)
|
|
561
|
-
this.adapter.call(this.oneBot.uin,
|
|
589
|
+
this.adapter.call(this.oneBot.uin, "V12", "deleteMsg", [event.message_id]);
|
|
562
590
|
if (res.kick && !event.anonymous)
|
|
563
|
-
this.adapter.call(this.oneBot.uin,
|
|
591
|
+
this.adapter.call(this.oneBot.uin, "V12", "setGroupKick", [
|
|
592
|
+
event.group_id,
|
|
593
|
+
event.user_id,
|
|
594
|
+
res.reject_add_request,
|
|
595
|
+
]);
|
|
564
596
|
if (res.ban)
|
|
565
|
-
this.adapter.call(this.oneBot.uin,
|
|
597
|
+
this.adapter.call(this.oneBot.uin, "V12", "setGroupBan", [
|
|
598
|
+
event.group_id,
|
|
599
|
+
event.user_id,
|
|
600
|
+
res.ban_duration > 0 ? res.ban_duration : 1800,
|
|
601
|
+
]);
|
|
566
602
|
}
|
|
567
603
|
}
|
|
568
604
|
if (event.type === "request" && "approve" in res) {
|
|
569
605
|
const action = event.detail_type === "friend" ? "setFriendAddRequest" : "setGroupAddRequest";
|
|
570
|
-
this.adapter.call(this.oneBot.uin,
|
|
606
|
+
this.adapter.call(this.oneBot.uin, "V12", action, [
|
|
607
|
+
event.flag,
|
|
608
|
+
res.approve,
|
|
609
|
+
res.reason ? res.reason : "",
|
|
610
|
+
!!res.block,
|
|
611
|
+
]);
|
|
571
612
|
}
|
|
572
613
|
}
|
|
573
614
|
/**
|
|
@@ -582,12 +623,12 @@ class V12 extends service_1.Service {
|
|
|
582
623
|
"X-Self-ID": String(this.oneBot.uin),
|
|
583
624
|
"X-Client-Role": "Universal",
|
|
584
625
|
"User-Agent": `OneBot/12 (${this.oneBot.platform}) Node-onebots/V12`,
|
|
585
|
-
"Sec-WebSocket-Protocol": "12.onebots.v" + utils_1.version
|
|
626
|
+
"Sec-WebSocket-Protocol": "12.onebots.v" + utils_1.version,
|
|
586
627
|
};
|
|
587
628
|
if (config.access_token)
|
|
588
629
|
headers.Authorization = "Bearer " + config.access_token;
|
|
589
630
|
const ws = new ws_1.WebSocket(remoteUrl, { headers });
|
|
590
|
-
ws.on("error",
|
|
631
|
+
ws.on("error", err => {
|
|
591
632
|
this.logger.error(err.message);
|
|
592
633
|
});
|
|
593
634
|
ws.on("open", () => {
|
|
@@ -595,7 +636,7 @@ class V12 extends service_1.Service {
|
|
|
595
636
|
this.wsr.add(ws);
|
|
596
637
|
this._webSocketHandler(ws);
|
|
597
638
|
});
|
|
598
|
-
ws.on("close",
|
|
639
|
+
ws.on("close", code => {
|
|
599
640
|
this.wsr.delete(ws);
|
|
600
641
|
if (timestmap < this.timestamp)
|
|
601
642
|
return;
|
|
@@ -625,7 +666,7 @@ class V12 extends service_1.Service {
|
|
|
625
666
|
status: "ok",
|
|
626
667
|
data: null,
|
|
627
668
|
message: null,
|
|
628
|
-
echo: data.echo
|
|
669
|
+
echo: data.echo,
|
|
629
670
|
});
|
|
630
671
|
}
|
|
631
672
|
else {
|
|
@@ -649,29 +690,30 @@ class V12 extends service_1.Service {
|
|
|
649
690
|
status: "failed",
|
|
650
691
|
data: null,
|
|
651
692
|
error: {
|
|
652
|
-
code,
|
|
693
|
+
code,
|
|
694
|
+
message,
|
|
653
695
|
},
|
|
654
|
-
echo: data?.echo
|
|
696
|
+
echo: data?.echo,
|
|
655
697
|
}));
|
|
656
698
|
}
|
|
657
699
|
});
|
|
658
700
|
this.dispatch(V12.formatPayload(this.oneBot.uin, "connect", {
|
|
659
701
|
detail_type: "connect",
|
|
660
|
-
type:
|
|
661
|
-
version: this.action.getVersion.apply(this)
|
|
702
|
+
type: "meta",
|
|
703
|
+
version: this.action.getVersion.apply(this),
|
|
662
704
|
}));
|
|
663
705
|
this.dispatch(V12.formatPayload(this.oneBot.uin, "status_update", {
|
|
664
|
-
detail_type:
|
|
665
|
-
status: this.action.getStatus.apply(this)
|
|
706
|
+
detail_type: "status_update",
|
|
707
|
+
status: this.action.getStatus.apply(this),
|
|
666
708
|
}));
|
|
667
709
|
}
|
|
668
710
|
}
|
|
669
711
|
exports.V12 = V12;
|
|
670
712
|
(function (V12) {
|
|
671
|
-
const fileTypes = [
|
|
713
|
+
const fileTypes = ["image", "file", "record", "video", "flash"];
|
|
672
714
|
V12.defaultConfig = {
|
|
673
715
|
heartbeat: 3,
|
|
674
|
-
access_token:
|
|
716
|
+
access_token: "",
|
|
675
717
|
request_timeout: 15,
|
|
676
718
|
reconnect_interval: 3,
|
|
677
719
|
enable_cors: true,
|
|
@@ -679,25 +721,25 @@ exports.V12 = V12;
|
|
|
679
721
|
use_http: true,
|
|
680
722
|
use_ws: true,
|
|
681
723
|
webhook: [],
|
|
682
|
-
ws_reverse: []
|
|
724
|
+
ws_reverse: [],
|
|
683
725
|
};
|
|
684
726
|
function success(data, retcode = 0, echo) {
|
|
685
727
|
return {
|
|
686
728
|
retcode,
|
|
687
|
-
status: retcode === 0 ?
|
|
729
|
+
status: retcode === 0 ? "ok" : "failed",
|
|
688
730
|
data,
|
|
689
|
-
message:
|
|
690
|
-
echo
|
|
731
|
+
message: "",
|
|
732
|
+
echo,
|
|
691
733
|
};
|
|
692
734
|
}
|
|
693
735
|
V12.success = success;
|
|
694
736
|
function error(message, retcode = 10001, echo) {
|
|
695
737
|
return {
|
|
696
738
|
retcode,
|
|
697
|
-
status:
|
|
739
|
+
status: "failed",
|
|
698
740
|
data: null,
|
|
699
741
|
message,
|
|
700
|
-
echo
|
|
742
|
+
echo,
|
|
701
743
|
};
|
|
702
744
|
}
|
|
703
745
|
V12.error = error;
|
|
@@ -706,12 +748,12 @@ exports.V12 = V12;
|
|
|
706
748
|
self_id: uin,
|
|
707
749
|
time: Math.floor(Date.now() / 1000),
|
|
708
750
|
detail_type: type,
|
|
709
|
-
type:
|
|
710
|
-
sub_type:
|
|
751
|
+
type: "meta",
|
|
752
|
+
sub_type: "",
|
|
711
753
|
...data,
|
|
712
|
-
group: data[
|
|
713
|
-
friend: data[
|
|
714
|
-
member: data[
|
|
754
|
+
group: data["group"]?.info,
|
|
755
|
+
friend: data["friend"]?.info,
|
|
756
|
+
member: data["member"]?.info,
|
|
715
757
|
};
|
|
716
758
|
}
|
|
717
759
|
V12.formatPayload = formatPayload;
|