onebots 0.0.22 → 0.0.24
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 +1 -2
- package/lib/db.d.ts +1 -1
- package/lib/db.js +8 -20
- package/lib/service/V12/config.d.ts +0 -8
- package/lib/service/V12/index.d.ts +9 -0
- package/lib/service/V12/index.js +82 -25
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -5,10 +5,9 @@
|
|
|
5
5
|
[](https://www.npmjs.com/package/onebots)
|
|
6
6
|
[](https://onebot.dev/)
|
|
7
7
|
[](https://12.onebot.dev/)
|
|
8
|
-
[](https://nodejs.org)
|
|
9
9
|
[](https://jq.qq.com/?_wv=1027&k=B22VGXov)
|
|
10
10
|
|
|
11
|
-
[](https://star-history.com/#lc-cn/onebots&Date)
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
[Type Docs](https://lc-cn.github.io/onebots)
|
package/lib/db.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export declare class Db extends EventEmitter {
|
|
|
9
9
|
set(key: string, value: any, escape?: boolean): any;
|
|
10
10
|
has(key: string, escape?: boolean): boolean;
|
|
11
11
|
delete(key: string, escape?: boolean): boolean;
|
|
12
|
-
write():
|
|
12
|
+
write(): void;
|
|
13
13
|
}
|
|
14
14
|
export declare namespace Db {
|
|
15
15
|
interface Config {
|
package/lib/db.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Db = void 0;
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
|
-
const promises_1 = require("fs/promises");
|
|
6
5
|
const events_1 = require("events");
|
|
7
6
|
class Db extends events_1.EventEmitter {
|
|
8
7
|
constructor(path, force_create = true) {
|
|
@@ -38,33 +37,22 @@ class Db extends events_1.EventEmitter {
|
|
|
38
37
|
throw new Error('不可达的位置:' + key.toString());
|
|
39
38
|
}
|
|
40
39
|
return result && typeof result === 'object' ? new Proxy(result, {
|
|
41
|
-
get(target, p) {
|
|
42
|
-
return target
|
|
40
|
+
get(target, p, receiver) {
|
|
41
|
+
return Reflect.get(target, p, receiver);
|
|
43
42
|
},
|
|
44
43
|
set(target, p, value, receiver) {
|
|
45
|
-
const result = target
|
|
46
|
-
_this.
|
|
44
|
+
const result = Reflect.set(target, p, value, receiver);
|
|
45
|
+
_this.set(`${key}.${value}`, value);
|
|
47
46
|
return result;
|
|
48
47
|
}
|
|
49
48
|
}) : result;
|
|
50
49
|
}
|
|
51
50
|
set(key, value, escape = true) {
|
|
52
51
|
const func = new Function('value', `return this.data.${key}=value`);
|
|
53
|
-
|
|
54
|
-
let result;
|
|
55
|
-
result = escape ? func.apply(this, [value]) : this.data[key] = value;
|
|
52
|
+
let result = escape ? func.apply(this, [value]) : this.data[key] = value;
|
|
56
53
|
this.raw_data = JSON.stringify(this.data, null, 2);
|
|
57
54
|
this.write();
|
|
58
|
-
return result
|
|
59
|
-
get(target, p) {
|
|
60
|
-
return target[p];
|
|
61
|
-
},
|
|
62
|
-
set(target, p, value, receiver) {
|
|
63
|
-
const result = target[p] = value;
|
|
64
|
-
_this.write();
|
|
65
|
-
return result;
|
|
66
|
-
}
|
|
67
|
-
}) : result;
|
|
55
|
+
return result;
|
|
68
56
|
}
|
|
69
57
|
has(key, escape = true) {
|
|
70
58
|
return escape ? new Function(`return !!this.data.${key}`).apply(this) : !!this.data[key];
|
|
@@ -74,11 +62,11 @@ class Db extends events_1.EventEmitter {
|
|
|
74
62
|
this.write();
|
|
75
63
|
return result;
|
|
76
64
|
}
|
|
77
|
-
|
|
65
|
+
write() {
|
|
78
66
|
try {
|
|
79
67
|
const raw_data = JSON.stringify(this.data, null, 2);
|
|
80
68
|
if (raw_data !== this.raw_data) {
|
|
81
|
-
|
|
69
|
+
(0, fs_1.writeFileSync)(this.path, raw_data);
|
|
82
70
|
this.raw_data = raw_data;
|
|
83
71
|
}
|
|
84
72
|
}
|
|
@@ -6,12 +6,4 @@ export declare namespace Config {
|
|
|
6
6
|
event_enabled?: boolean;
|
|
7
7
|
event_buffer_size?: number;
|
|
8
8
|
}
|
|
9
|
-
type WebhookConfig = string | ({
|
|
10
|
-
url: string;
|
|
11
|
-
timeout?: number;
|
|
12
|
-
} & AuthInfo);
|
|
13
|
-
type WsReverseConfig = string | ({
|
|
14
|
-
url: string;
|
|
15
|
-
reconnect_interval?: number;
|
|
16
|
-
} & AuthInfo);
|
|
17
9
|
}
|
|
@@ -26,6 +26,7 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
|
|
|
26
26
|
start(path?: string): void;
|
|
27
27
|
private startHttp;
|
|
28
28
|
private startWebhook;
|
|
29
|
+
private runActions;
|
|
29
30
|
private startWs;
|
|
30
31
|
private startWsReverse;
|
|
31
32
|
stop(force?: boolean): Promise<void>;
|
|
@@ -33,6 +34,7 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
|
|
|
33
34
|
self_id: number;
|
|
34
35
|
time: number;
|
|
35
36
|
detail_type: E;
|
|
37
|
+
type: string;
|
|
36
38
|
sub_type: string;
|
|
37
39
|
} & Omit<V12.BotEventMap[E], E>;
|
|
38
40
|
dispatch(data: Record<string, any>): void;
|
|
@@ -113,6 +115,11 @@ export declare namespace V12 {
|
|
|
113
115
|
}
|
|
114
116
|
interface WebhookConfig extends Config.AuthInfo {
|
|
115
117
|
url: string;
|
|
118
|
+
timeout?: number;
|
|
119
|
+
get_latest_actions?: boolean | string | {
|
|
120
|
+
path?: string;
|
|
121
|
+
interval: number;
|
|
122
|
+
};
|
|
116
123
|
}
|
|
117
124
|
interface WsConfig extends Config.AuthInfo {
|
|
118
125
|
}
|
|
@@ -149,6 +156,7 @@ export declare namespace V12 {
|
|
|
149
156
|
type BotEventMap = {
|
|
150
157
|
system: Record<string, any>;
|
|
151
158
|
connect: {
|
|
159
|
+
type: any;
|
|
152
160
|
detail_type: 'connect';
|
|
153
161
|
version: ReturnType<Action['getVersion']>;
|
|
154
162
|
};
|
|
@@ -177,6 +185,7 @@ export declare namespace V12 {
|
|
|
177
185
|
self_id: number;
|
|
178
186
|
time: number;
|
|
179
187
|
detail_type: K;
|
|
188
|
+
type: string;
|
|
180
189
|
sub_type: string;
|
|
181
190
|
} & Omit<BotEventMap[K], K>;
|
|
182
191
|
}
|
package/lib/service/V12/index.js
CHANGED
|
@@ -44,7 +44,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
44
44
|
this.startHttp({
|
|
45
45
|
access_token: this.config.access_token,
|
|
46
46
|
event_enabled: true,
|
|
47
|
-
event_buffer_size:
|
|
47
|
+
event_buffer_size: 10,
|
|
48
48
|
...config
|
|
49
49
|
});
|
|
50
50
|
}
|
|
@@ -98,7 +98,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
98
98
|
this.oneBot.app.router.all(new RegExp(`^${this.path}/(.*)$`), (ctx) => this._httpRequestHandler(ctx, config));
|
|
99
99
|
this.logger.mark(`开启http服务器成功,监听:http://127.0.0.1:${this.oneBot.app.config.port}${this.path}`);
|
|
100
100
|
this.on('dispatch', (payload) => {
|
|
101
|
-
if (!['message', 'notice', 'request', '
|
|
101
|
+
if (!['message', 'notice', 'request', 'meta'].includes(payload.type))
|
|
102
102
|
return;
|
|
103
103
|
if (config.event_enabled) {
|
|
104
104
|
this.history.push(payload);
|
|
@@ -108,28 +108,37 @@ class V12 extends events_1.EventEmitter {
|
|
|
108
108
|
});
|
|
109
109
|
}
|
|
110
110
|
startWebhook(config) {
|
|
111
|
+
const options = {
|
|
112
|
+
method: "POST",
|
|
113
|
+
timeout: config.timeout || this.config.request_timeout,
|
|
114
|
+
headers: {
|
|
115
|
+
"Content-Type": "application/json",
|
|
116
|
+
"User-Agent": "OneBot/12 (qq) Node-onebots/0.0.15",
|
|
117
|
+
"X-OneBot-Version": 12,
|
|
118
|
+
"X-Impl": "oicq_onebot",
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
if (config.access_token) {
|
|
122
|
+
options.headers['Authorization'] = `Bearer ${config.access_token}`;
|
|
123
|
+
}
|
|
124
|
+
const protocol = config.url.startsWith("https") ? https_1.default : http_1.default;
|
|
111
125
|
this.on('dispatch', (unserialized) => {
|
|
112
|
-
const serialized = JSON.stringify(unserialized);
|
|
113
|
-
const options = {
|
|
114
|
-
method: "POST",
|
|
115
|
-
timeout: this.config.request_timeout * 1000,
|
|
116
|
-
headers: {
|
|
117
|
-
"Content-Type": "application/json",
|
|
118
|
-
"Content-Length": Buffer.byteLength(serialized),
|
|
119
|
-
"X-Self-ID": String(this.client.uin),
|
|
120
|
-
"User-Agent": "OneBot",
|
|
121
|
-
},
|
|
122
|
-
};
|
|
123
|
-
const protocol = config.url.startsWith("https") ? https_1.default : http_1.default;
|
|
124
126
|
try {
|
|
125
|
-
|
|
127
|
+
const serialized = JSON.stringify(unserialized);
|
|
128
|
+
protocol.request(config.url, {
|
|
129
|
+
...options,
|
|
130
|
+
headers: {
|
|
131
|
+
...options.headers,
|
|
132
|
+
"Content-Length": Buffer.byteLength(serialized),
|
|
133
|
+
}
|
|
134
|
+
}, (res) => {
|
|
126
135
|
if (res.statusCode !== 200)
|
|
127
|
-
return this.logger.warn(`
|
|
136
|
+
return this.logger.warn(`Webhook(${config.url})上报事件收到非200响应:` + res.statusCode);
|
|
128
137
|
let data = "";
|
|
129
138
|
res.setEncoding("utf-8");
|
|
130
139
|
res.on("data", (chunk) => data += chunk);
|
|
131
140
|
res.on("end", () => {
|
|
132
|
-
this.logger.debug(`收到
|
|
141
|
+
this.logger.debug(`收到Webhook响应 ${res.statusCode} :` + data);
|
|
133
142
|
if (!data)
|
|
134
143
|
return;
|
|
135
144
|
try {
|
|
@@ -140,15 +149,63 @@ class V12 extends events_1.EventEmitter {
|
|
|
140
149
|
}
|
|
141
150
|
});
|
|
142
151
|
}).on("error", (err) => {
|
|
143
|
-
this.logger.error(`
|
|
152
|
+
this.logger.error(`Webhook(${config.url})上报事件失败:` + err.message);
|
|
144
153
|
}).end(serialized, () => {
|
|
145
|
-
this.logger.debug(`
|
|
154
|
+
this.logger.debug(`Webhook(${config.url})上报事件成功: ` + serialized);
|
|
146
155
|
});
|
|
147
156
|
}
|
|
148
157
|
catch (e) {
|
|
149
|
-
this.logger.error(`
|
|
158
|
+
this.logger.error(`Webhook(${config.url})上报失败:` + e.message);
|
|
150
159
|
}
|
|
151
160
|
});
|
|
161
|
+
if (config.get_latest_actions) {
|
|
162
|
+
const interval = (config.get_latest_actions && typeof config.get_latest_actions === 'object') ?
|
|
163
|
+
config.get_latest_actions.interval * 1000 :
|
|
164
|
+
1000 * 60;
|
|
165
|
+
setInterval(() => {
|
|
166
|
+
try {
|
|
167
|
+
const actionPath = typeof config.get_latest_actions === 'string' ?
|
|
168
|
+
config.get_latest_actions :
|
|
169
|
+
typeof config.get_latest_actions === 'boolean' ?
|
|
170
|
+
'get_latest_actions' :
|
|
171
|
+
config.get_latest_actions.path || '/get_latest_actions';
|
|
172
|
+
protocol.request(`${config.url}${actionPath}`, {
|
|
173
|
+
...options,
|
|
174
|
+
method: 'GET',
|
|
175
|
+
headers: {
|
|
176
|
+
...options.headers,
|
|
177
|
+
}
|
|
178
|
+
}, (res) => {
|
|
179
|
+
if (res.statusCode !== 200)
|
|
180
|
+
return this.logger.warn(`Webhook(${config.url})获取动作队列收到非200响应:` + res.statusCode);
|
|
181
|
+
let data = "";
|
|
182
|
+
res.setEncoding("utf-8");
|
|
183
|
+
res.on("data", (chunk) => data += chunk);
|
|
184
|
+
res.on("end", () => {
|
|
185
|
+
this.logger.info(`获取动作队列响应 ${res.statusCode} :` + data);
|
|
186
|
+
if (!data)
|
|
187
|
+
return;
|
|
188
|
+
try {
|
|
189
|
+
this.runActions(JSON.parse(data));
|
|
190
|
+
}
|
|
191
|
+
catch (e) {
|
|
192
|
+
this.logger.error(`执行动作报错:` + e.message);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}).on("error", (err) => {
|
|
196
|
+
this.logger.error(`Webhook(${config.url})获取动作队列失败:` + err.message);
|
|
197
|
+
}).end();
|
|
198
|
+
}
|
|
199
|
+
catch (e) {
|
|
200
|
+
this.logger.error(`Webhook(${config.url})获取动作队列失败:` + e.message);
|
|
201
|
+
}
|
|
202
|
+
}, interval);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
runActions(actions) {
|
|
206
|
+
for (const action of actions) {
|
|
207
|
+
this.apply(action);
|
|
208
|
+
}
|
|
152
209
|
}
|
|
153
210
|
startWs(config) {
|
|
154
211
|
this.wss = this.oneBot.app.router.ws(this.path, this.oneBot.app.httpServer);
|
|
@@ -170,7 +227,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
170
227
|
if (token)
|
|
171
228
|
req.headers["authorization"] = `Bearer ${token}`;
|
|
172
229
|
if (!req.headers["authorization"] || req.headers["authorization"] !== `Bearer ${config.access_token}`)
|
|
173
|
-
return ws.close(
|
|
230
|
+
return ws.close(401, "wrong access token");
|
|
174
231
|
}
|
|
175
232
|
this._webSocketHandler(ws);
|
|
176
233
|
});
|
|
@@ -416,12 +473,12 @@ class V12 extends events_1.EventEmitter {
|
|
|
416
473
|
const headers = {
|
|
417
474
|
"X-Self-ID": String(this.client.uin),
|
|
418
475
|
"X-Client-Role": "Universal",
|
|
419
|
-
"User-Agent": "OneBot/12 (qq)
|
|
476
|
+
"User-Agent": "OneBot/12 (qq) Node-onebots/0.0.15",
|
|
420
477
|
"Sec-WebSocket-Protocol": "12.onebots.v0.0.15"
|
|
421
478
|
};
|
|
422
479
|
if (config.access_token)
|
|
423
480
|
headers.Authorization = "Bearer " + config.access_token;
|
|
424
|
-
const ws = new ws_1.WebSocket(url, { headers });
|
|
481
|
+
const ws = new ws_1.WebSocket(url, '12.onebots.v0.0.15', { headers });
|
|
425
482
|
ws.on("error", (err) => {
|
|
426
483
|
this.logger.error(err.message);
|
|
427
484
|
});
|
|
@@ -561,14 +618,14 @@ exports.V12 = V12;
|
|
|
561
618
|
}
|
|
562
619
|
V12.error = error;
|
|
563
620
|
function formatPayload(uin, type, data) {
|
|
564
|
-
|
|
621
|
+
return {
|
|
565
622
|
self_id: uin,
|
|
566
623
|
time: Math.floor(Date.now() / 1000),
|
|
567
624
|
detail_type: type,
|
|
625
|
+
type: 'meta',
|
|
568
626
|
sub_type: '',
|
|
569
627
|
...data
|
|
570
628
|
};
|
|
571
|
-
return result;
|
|
572
629
|
}
|
|
573
630
|
V12.formatPayload = formatPayload;
|
|
574
631
|
})(V12 = exports.V12 || (exports.V12 = {}));
|