onebots 0.0.17 → 0.0.19
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/lib/onebot.d.ts +1 -1
- package/lib/onebot.js +8 -10
- package/lib/service/V11/index.d.ts +1 -0
- package/lib/service/V11/index.js +3 -0
- package/lib/service/V12/action/common.d.ts +1 -0
- package/lib/service/V12/action/common.js +1 -0
- package/lib/service/V12/index.d.ts +62 -6
- package/lib/service/V12/index.js +80 -46
- package/package.json +1 -1
package/lib/onebot.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare class OneBot<V extends OneBot.Version> extends EventEmitter {
|
|
|
21
21
|
start(): Promise<void>;
|
|
22
22
|
startListen(): void;
|
|
23
23
|
stop(force?: boolean): Promise<void>;
|
|
24
|
-
dispatch(data: any): void;
|
|
24
|
+
dispatch(event: any, data: any): void;
|
|
25
25
|
}
|
|
26
26
|
export declare enum OneBotStatus {
|
|
27
27
|
Good = 0,
|
package/lib/onebot.js
CHANGED
|
@@ -101,10 +101,10 @@ class OneBot extends events_1.EventEmitter {
|
|
|
101
101
|
await this.client.login(this.password);
|
|
102
102
|
}
|
|
103
103
|
startListen() {
|
|
104
|
-
this.client.on('system', this.dispatch.bind(this));
|
|
105
|
-
this.client.on('notice', this.dispatch.bind(this));
|
|
106
|
-
this.client.on('request', this.dispatch.bind(this));
|
|
107
|
-
this.client.on('message', this.dispatch.bind(this));
|
|
104
|
+
this.client.on('system', this.dispatch.bind(this, 'system'));
|
|
105
|
+
this.client.on('notice', this.dispatch.bind(this, 'notice'));
|
|
106
|
+
this.client.on('request', this.dispatch.bind(this, 'request'));
|
|
107
|
+
this.client.on('message', this.dispatch.bind(this, 'message'));
|
|
108
108
|
for (const instance of this.instances) {
|
|
109
109
|
instance.start(this.instances.length > 1 ? '/' + instance.version : undefined);
|
|
110
110
|
}
|
|
@@ -113,14 +113,12 @@ class OneBot extends events_1.EventEmitter {
|
|
|
113
113
|
for (const instance of this.instances) {
|
|
114
114
|
await instance.stop(force);
|
|
115
115
|
}
|
|
116
|
-
this.client.
|
|
117
|
-
this.client.off('notice', this.dispatch.bind(this));
|
|
118
|
-
this.client.off('request', this.dispatch.bind(this));
|
|
119
|
-
this.client.off('message', this.dispatch.bind(this));
|
|
116
|
+
this.client.removeAllListeners();
|
|
120
117
|
}
|
|
121
|
-
dispatch(data) {
|
|
118
|
+
dispatch(event, data) {
|
|
122
119
|
for (const instance of this.instances) {
|
|
123
|
-
instance.
|
|
120
|
+
const result = instance.format(event, data);
|
|
121
|
+
instance.dispatch(result);
|
|
124
122
|
}
|
|
125
123
|
}
|
|
126
124
|
}
|
|
@@ -33,6 +33,7 @@ export declare class V11 extends EventEmitter implements OneBot.Base {
|
|
|
33
33
|
private startWs;
|
|
34
34
|
private startWsReverse;
|
|
35
35
|
stop(force?: boolean): Promise<void>;
|
|
36
|
+
format(_: any, data: any): any;
|
|
36
37
|
dispatch(data: any): void;
|
|
37
38
|
private _httpRequestHandler;
|
|
38
39
|
/**
|
package/lib/service/V11/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import { Client } from "oicq";
|
|
3
|
+
import { Client, EventMap, MessageElem, Sendable } from "oicq";
|
|
4
4
|
import { Config } from './config';
|
|
5
5
|
import { OneBot } from "../../onebot";
|
|
6
6
|
import { Action } from "./action";
|
|
@@ -29,6 +29,12 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
|
|
|
29
29
|
private startWs;
|
|
30
30
|
private startWsReverse;
|
|
31
31
|
stop(force?: boolean): Promise<void>;
|
|
32
|
+
format<E extends keyof V12.BotEventMap>(event: E, ...args: [V12.BotEventMap[E]]): {
|
|
33
|
+
self_id: number;
|
|
34
|
+
time: number;
|
|
35
|
+
detail_type: E;
|
|
36
|
+
sub_type: string;
|
|
37
|
+
} & Omit<V12.BotEventMap[E], E>;
|
|
32
38
|
dispatch(data: Record<string, any>): void;
|
|
33
39
|
apply(req: any): Promise<string>;
|
|
34
40
|
private _httpRequestHandler;
|
|
@@ -46,6 +52,47 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
|
|
|
46
52
|
protected _webSocketHandler(ws: WebSocket): void;
|
|
47
53
|
}
|
|
48
54
|
export declare namespace V12 {
|
|
55
|
+
function fromSegment(msgList: SegmentElem | string | number | (SegmentElem | string | number)[]): MessageElem[];
|
|
56
|
+
function toSegment(msgList: Sendable): SegmentElem<keyof SegmentMap>[];
|
|
57
|
+
interface SegmentMap {
|
|
58
|
+
face: {
|
|
59
|
+
id: number;
|
|
60
|
+
text: string;
|
|
61
|
+
};
|
|
62
|
+
text: {
|
|
63
|
+
text: string;
|
|
64
|
+
};
|
|
65
|
+
mention: {
|
|
66
|
+
user_id: string;
|
|
67
|
+
};
|
|
68
|
+
mention_all: null;
|
|
69
|
+
image: {
|
|
70
|
+
file_id: string;
|
|
71
|
+
};
|
|
72
|
+
voice: {
|
|
73
|
+
file_id: string;
|
|
74
|
+
};
|
|
75
|
+
audio: {
|
|
76
|
+
file_id: string;
|
|
77
|
+
};
|
|
78
|
+
file: {
|
|
79
|
+
file_id: string;
|
|
80
|
+
};
|
|
81
|
+
location: {
|
|
82
|
+
latitude: number;
|
|
83
|
+
longitude: number;
|
|
84
|
+
title: string;
|
|
85
|
+
content: string;
|
|
86
|
+
};
|
|
87
|
+
reply: {
|
|
88
|
+
message_id: string;
|
|
89
|
+
user_id: string;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
type SegmentElem<K extends keyof SegmentMap = keyof SegmentMap> = {
|
|
93
|
+
type: K;
|
|
94
|
+
data: SegmentMap[K];
|
|
95
|
+
};
|
|
49
96
|
interface Config {
|
|
50
97
|
heartbeat?: number;
|
|
51
98
|
access_token?: string;
|
|
@@ -94,7 +141,8 @@ export declare namespace V12 {
|
|
|
94
141
|
params: any;
|
|
95
142
|
echo?: string;
|
|
96
143
|
}
|
|
97
|
-
type
|
|
144
|
+
type BotEventMap = {
|
|
145
|
+
system: Record<string, any>;
|
|
98
146
|
connect: {
|
|
99
147
|
detail_type: 'connect';
|
|
100
148
|
version: ReturnType<Action['getVersion']>;
|
|
@@ -107,15 +155,23 @@ export declare namespace V12 {
|
|
|
107
155
|
detail_type: 'status_update';
|
|
108
156
|
status: ReturnType<Action['getStatus']>;
|
|
109
157
|
};
|
|
158
|
+
} & TransformEventMap;
|
|
159
|
+
type TransformEventMap = {
|
|
160
|
+
[P in keyof EventMap]: TransformEventParams<Parameters<EventMap[P]>>;
|
|
161
|
+
};
|
|
162
|
+
type TransformEventParams<T extends any[]> = T extends [infer L, ...infer R] ? L extends object ? L & {
|
|
163
|
+
args: R;
|
|
164
|
+
} : {
|
|
165
|
+
args: [L, ...R];
|
|
166
|
+
} : {
|
|
167
|
+
args: T;
|
|
110
168
|
};
|
|
111
169
|
function success<T extends any>(data: T, retcode?: Result<T>['retcode'], echo?: string): Result<T>;
|
|
112
170
|
function error(message: string, retcode?: Result<null>['retcode'], echo?: string): Result<null>;
|
|
113
|
-
function
|
|
171
|
+
function formatPayload<K extends keyof BotEventMap>(uin: number, type: K, data: Omit<BotEventMap[K], K>): {
|
|
114
172
|
self_id: number;
|
|
115
173
|
time: number;
|
|
116
|
-
type: string;
|
|
117
|
-
status: string;
|
|
118
174
|
detail_type: K;
|
|
119
175
|
sub_type: string;
|
|
120
|
-
} & Omit<
|
|
176
|
+
} & Omit<BotEventMap[K], K>;
|
|
121
177
|
}
|
package/lib/service/V12/index.js
CHANGED
|
@@ -13,7 +13,6 @@ const url_1 = require("url");
|
|
|
13
13
|
const http_1 = __importDefault(require("http"));
|
|
14
14
|
const https_1 = __importDefault(require("https"));
|
|
15
15
|
const ws_1 = require("ws");
|
|
16
|
-
const oicq2_cq_enable_1 = require("oicq2-cq-enable");
|
|
17
16
|
const utils_1 = require("../../utils");
|
|
18
17
|
const db_1 = require("../../db");
|
|
19
18
|
const app_1 = require("../../server/app");
|
|
@@ -88,7 +87,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
88
87
|
});
|
|
89
88
|
if (this.config.heartbeat) {
|
|
90
89
|
this.heartbeat = setInterval(() => {
|
|
91
|
-
this.dispatch(V12.
|
|
90
|
+
this.dispatch(V12.formatPayload(this.client.uin, 'heartbeat', {
|
|
92
91
|
detail_type: "heartbeat",
|
|
93
92
|
interval: new Date().getTime() + this.config.heartbeat * 1000
|
|
94
93
|
}));
|
|
@@ -209,27 +208,50 @@ class V12 extends events_1.EventEmitter {
|
|
|
209
208
|
(0, fs_1.rmSync)(this.client.dir, { force: true, recursive: true });
|
|
210
209
|
}
|
|
211
210
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (!data
|
|
216
|
-
data
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
data
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
data.
|
|
211
|
+
format(event, ...args) {
|
|
212
|
+
const data = (typeof args[0]) === 'object' ? args.shift() || {} : {};
|
|
213
|
+
data.type = data.post_type;
|
|
214
|
+
if (!data.type) {
|
|
215
|
+
data.type = 'meta';
|
|
216
|
+
data.detail_type = 'online';
|
|
217
|
+
if (data.image) {
|
|
218
|
+
data.type = 'login';
|
|
219
|
+
data.detail_type = 'qrcode';
|
|
220
|
+
}
|
|
221
|
+
else if (data.url) {
|
|
222
|
+
data.type = 'login';
|
|
223
|
+
data.detail_type = 'slider';
|
|
224
224
|
if (data.phone) {
|
|
225
|
-
data.
|
|
225
|
+
data.detail_type = 'device';
|
|
226
226
|
}
|
|
227
227
|
}
|
|
228
228
|
else if (data.message) {
|
|
229
|
-
data.
|
|
230
|
-
data.
|
|
229
|
+
data.type = 'login';
|
|
230
|
+
data.detial_type = 'error';
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (data.type === 'notice') {
|
|
234
|
+
switch (data.detail_type) {
|
|
235
|
+
case 'friend':
|
|
236
|
+
data.detail_type += data.sub_type;
|
|
237
|
+
break;
|
|
238
|
+
case 'group':
|
|
239
|
+
if (['increase', 'decrease'].includes(data.sub_type))
|
|
240
|
+
data.detail_type = 'group_member_' + data.sub_type;
|
|
241
|
+
else if (data.sub_type === 'recall')
|
|
242
|
+
data.detail_type = 'group_message_delete';
|
|
231
243
|
}
|
|
232
244
|
}
|
|
245
|
+
if (data.type === 'system')
|
|
246
|
+
data.type = 'meta';
|
|
247
|
+
data.alt_message = data.raw_message;
|
|
248
|
+
data.self = this.action.getSelfInfo.apply(this);
|
|
249
|
+
if (!data.detail_type)
|
|
250
|
+
data.detail_type = data.message_type || data.notice_type || data.request_type || data.system_type;
|
|
251
|
+
data.message = data.type === 'message' ? V12.toSegment(data.message) : data.message;
|
|
252
|
+
return V12.formatPayload(this.client.uin, event, data);
|
|
253
|
+
}
|
|
254
|
+
dispatch(data) {
|
|
233
255
|
const payload = {
|
|
234
256
|
id: (0, utils_1.uuid)(),
|
|
235
257
|
impl: 'oicq_onebot',
|
|
@@ -239,23 +261,8 @@ class V12 extends events_1.EventEmitter {
|
|
|
239
261
|
platform: 'qq',
|
|
240
262
|
user_id: `${this.client.uin}`
|
|
241
263
|
},
|
|
242
|
-
type: data.post_type || 'meta',
|
|
243
|
-
alt_message: data.raw_message,
|
|
244
|
-
detail_type: data.message_type || data.notice_type || data.request_type || data.system_type,
|
|
245
264
|
...data,
|
|
246
265
|
};
|
|
247
|
-
if (payload.type === 'notice') {
|
|
248
|
-
switch (payload.detail_type) {
|
|
249
|
-
case 'friend':
|
|
250
|
-
payload.detail_type += payload.sub_type;
|
|
251
|
-
break;
|
|
252
|
-
case 'group':
|
|
253
|
-
if (['increase', 'decrease'].includes(payload.sub_type))
|
|
254
|
-
payload.detail_type = 'group_member_' + payload.sub_type;
|
|
255
|
-
else if (payload.sub_type === 'recall')
|
|
256
|
-
payload.detail_type = 'group_message_delete';
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
266
|
this.emit('dispatch', payload);
|
|
260
267
|
}
|
|
261
268
|
async apply(req) {
|
|
@@ -264,7 +271,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
264
271
|
let is_async = action.includes("_async");
|
|
265
272
|
if (is_async)
|
|
266
273
|
action = action.replace("_async", "");
|
|
267
|
-
if (action === '
|
|
274
|
+
if (action === 'send_message') {
|
|
268
275
|
if (["private", "group", "discuss", 'channel'].includes(params.detail_type)) {
|
|
269
276
|
action = "send_" + params.detail_type + "_msg";
|
|
270
277
|
}
|
|
@@ -292,12 +299,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
292
299
|
if (onebot_1.BOOLS.includes(k))
|
|
293
300
|
params[k] = (0, utils_1.toBool)(params[k]);
|
|
294
301
|
if (k === 'message') {
|
|
295
|
-
|
|
296
|
-
params[k] = (0, oicq2_cq_enable_1.fromCqcode)(params[k]);
|
|
297
|
-
}
|
|
298
|
-
else {
|
|
299
|
-
params[k] = (0, oicq2_cq_enable_1.fromSegment)(params[k]);
|
|
300
|
-
}
|
|
302
|
+
params[k] = V12.fromSegment(params[k]);
|
|
301
303
|
}
|
|
302
304
|
args.push(params[k]);
|
|
303
305
|
}
|
|
@@ -487,12 +489,45 @@ class V12 extends events_1.EventEmitter {
|
|
|
487
489
|
}));
|
|
488
490
|
}
|
|
489
491
|
});
|
|
490
|
-
this.dispatch(V12.
|
|
491
|
-
this.dispatch(V12.
|
|
492
|
+
this.dispatch(V12.formatPayload(this.client.uin, "connect", this.action.getVersion.apply(this)));
|
|
493
|
+
this.dispatch(V12.formatPayload(this.client.uin, "status_update", this.action.getStatus.apply(this)));
|
|
492
494
|
}
|
|
493
495
|
}
|
|
494
496
|
exports.V12 = V12;
|
|
495
497
|
(function (V12) {
|
|
498
|
+
function fromSegment(msgList) {
|
|
499
|
+
msgList = [].concat(msgList);
|
|
500
|
+
return msgList.map((msg) => {
|
|
501
|
+
if (typeof msg !== 'object')
|
|
502
|
+
msg = String(msg);
|
|
503
|
+
if (typeof msg === 'string') {
|
|
504
|
+
return { type: 'text', text: msg };
|
|
505
|
+
}
|
|
506
|
+
const { type, data, ...other } = msg;
|
|
507
|
+
return {
|
|
508
|
+
type: type.replace('mention', 'at').replace('at_all', 'at'),
|
|
509
|
+
...other,
|
|
510
|
+
...data
|
|
511
|
+
};
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
V12.fromSegment = fromSegment;
|
|
515
|
+
function toSegment(msgList) {
|
|
516
|
+
msgList = [].concat(msgList);
|
|
517
|
+
return msgList.map((msg) => {
|
|
518
|
+
if (typeof msg === 'string')
|
|
519
|
+
return { type: 'text', data: { text: msg } };
|
|
520
|
+
let { type, ...other } = msg;
|
|
521
|
+
return {
|
|
522
|
+
type: type === 'at' ? other['qq'] ? 'mention' : "mention_all" : type,
|
|
523
|
+
data: {
|
|
524
|
+
...other,
|
|
525
|
+
user_id: other['qq']
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
V12.toSegment = toSegment;
|
|
496
531
|
V12.defaultConfig = {
|
|
497
532
|
heartbeat: 3,
|
|
498
533
|
access_token: '',
|
|
@@ -524,16 +559,15 @@ exports.V12 = V12;
|
|
|
524
559
|
};
|
|
525
560
|
}
|
|
526
561
|
V12.error = error;
|
|
527
|
-
function
|
|
528
|
-
|
|
562
|
+
function formatPayload(uin, type, data) {
|
|
563
|
+
const result = {
|
|
529
564
|
self_id: uin,
|
|
530
565
|
time: Math.floor(Date.now() / 1000),
|
|
531
|
-
type: "meta",
|
|
532
|
-
status: 'ok',
|
|
533
566
|
detail_type: type,
|
|
534
567
|
sub_type: '',
|
|
535
568
|
...data
|
|
536
569
|
};
|
|
570
|
+
return result;
|
|
537
571
|
}
|
|
538
|
-
V12.
|
|
572
|
+
V12.formatPayload = formatPayload;
|
|
539
573
|
})(V12 = exports.V12 || (exports.V12 = {}));
|