@alemonjs/qq-bot 2.1.0-alpha.9 → 2.1.1
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 +15 -45
- package/lib/config.d.ts +9 -0
- package/lib/config.js +20 -0
- package/lib/desktop.d.ts +1 -3
- package/lib/desktop.js +3 -12
- package/lib/format.d.ts +5 -0
- package/lib/format.js +94 -0
- package/lib/hook.d.ts +36 -9
- package/lib/hook.js +21 -19
- package/lib/index.d.ts +5 -7
- package/lib/index.js +11 -21
- package/lib/index.webhook.d.ts +1 -0
- package/lib/index.webhook.js +5 -4
- package/lib/index.websoket.d.ts +1 -0
- package/lib/index.websoket.js +31 -23
- package/lib/message/AT_MESSAGE_CREATE.d.ts +35 -0
- package/lib/message/AT_MESSAGE_CREATE.js +1 -0
- package/lib/message/CHANNEL_CREATE.d.ts +15 -0
- package/lib/message/CHANNEL_CREATE.js +1 -0
- package/lib/message/CHANNEL_DELETE.d.ts +15 -0
- package/lib/message/CHANNEL_DELETE.js +1 -0
- package/lib/message/CHANNEL_UPDATE.d.ts +15 -0
- package/lib/message/CHANNEL_UPDATE.js +1 -0
- package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +29 -0
- package/lib/message/DIRECT_MESSAGE_CREATE.js +1 -0
- package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +17 -0
- package/lib/message/DIRECT_MESSAGE_DELETE.js +1 -0
- package/lib/message/ERROR.d.ts +1 -0
- package/lib/message/ERROR.js +1 -0
- package/lib/message/GUILD_CREATE.d.ts +15 -0
- package/lib/message/GUILD_CREATE.js +1 -0
- package/lib/message/GUILD_DELETE.d.ts +15 -0
- package/lib/message/GUILD_DELETE.js +1 -0
- package/lib/message/GUILD_MEMBER_ADD.d.ts +14 -0
- package/lib/message/GUILD_MEMBER_ADD.js +1 -0
- package/lib/message/GUILD_MEMBER_REMOVE.d.ts +14 -0
- package/lib/message/GUILD_MEMBER_REMOVE.js +1 -0
- package/lib/message/GUILD_MEMBER_UPDATE.d.ts +14 -0
- package/lib/message/GUILD_MEMBER_UPDATE.js +1 -0
- package/lib/message/GUILD_UPDATE.d.ts +15 -0
- package/lib/message/GUILD_UPDATE.js +1 -0
- package/lib/message/INTERACTION_CREATE.d.ts +53 -0
- package/lib/message/INTERACTION_CREATE.js +1 -0
- package/lib/message/MESSAGE_CREATE.d.ts +1 -0
- package/lib/message/MESSAGE_CREATE.js +1 -0
- package/lib/message/MESSAGE_DELETE.d.ts +1 -0
- package/lib/message/MESSAGE_DELETE.js +1 -0
- package/lib/message/MESSAGE_REACTION_ADD.d.ts +13 -0
- package/lib/message/MESSAGE_REACTION_ADD.js +1 -0
- package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +13 -0
- package/lib/message/MESSAGE_REACTION_REMOVE.js +1 -0
- package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +15 -0
- package/lib/message/PUBLIC_MESSAGE_DELETE.js +1 -0
- package/lib/message/READY.d.ts +6 -0
- package/lib/message/READY.js +1 -0
- package/lib/message/group/C2C_MESSAGE_CREATE.d.ts +9 -0
- package/lib/message/group/C2C_MESSAGE_CREATE.js +1 -0
- package/lib/message/group/C2C_MSG_RECEIVE.d.ts +4 -0
- package/lib/message/group/C2C_MSG_RECEIVE.js +1 -0
- package/lib/message/group/C2C_MSG_REJECT.d.ts +4 -0
- package/lib/message/group/C2C_MSG_REJECT.js +1 -0
- package/lib/message/group/FRIEND_ADD.d.ts +4 -0
- package/lib/message/group/FRIEND_ADD.js +1 -0
- package/lib/message/group/FRIEND_DEL.d.ts +4 -0
- package/lib/message/group/FRIEND_DEL.js +1 -0
- package/lib/message/group/GROUP_ADD_ROBOT.d.ts +5 -0
- package/lib/message/group/GROUP_ADD_ROBOT.js +1 -0
- package/lib/message/group/GROUP_AT_MESSAGE_CREATE.d.ts +11 -0
- package/lib/message/group/GROUP_AT_MESSAGE_CREATE.js +1 -0
- package/lib/message/group/GROUP_DEL_ROBOT.d.ts +5 -0
- package/lib/message/group/GROUP_DEL_ROBOT.js +1 -0
- package/lib/message/group/GROUP_MSG_RECEIVE.d.ts +5 -0
- package/lib/message/group/GROUP_MSG_RECEIVE.js +1 -0
- package/lib/message/group/GROUP_MSG_REJECT.d.ts +5 -0
- package/lib/message/group/GROUP_MSG_REJECT.js +1 -0
- package/lib/register.d.ts +2 -3
- package/lib/register.js +426 -252
- package/lib/sdk/api.d.ts +10 -567
- package/lib/sdk/api.js +224 -790
- package/lib/sdk/client.webhook.d.ts +9 -0
- package/lib/sdk/{client.js → client.webhook.js} +18 -55
- package/lib/sdk/client.websoket.d.ts +9 -0
- package/lib/sdk/client.websoket.js +52 -97
- package/lib/sdk/config.d.ts +1 -0
- package/lib/sdk/instance.d.ts +3 -0
- package/lib/sdk/instance.js +93 -0
- package/lib/sdk/intents.d.ts +3 -0
- package/lib/sdk/intents.js +25 -90
- package/lib/sdk/message.d.ts +4 -0
- package/lib/sdk/message.group.d.ts +24 -0
- package/lib/sdk/message.group.js +1 -0
- package/lib/sdk/message.guild.d.ts +38 -0
- package/lib/sdk/message.guild.js +1 -0
- package/lib/sdk/message.js +1 -0
- package/lib/sdk/message.public.d.ts +6 -0
- package/lib/sdk/message.public.js +1 -0
- package/lib/sdk/typing.d.ts +22 -11
- package/lib/sdk/typing.js +1 -0
- package/lib/sdk/webhook.secret.d.ts +14 -0
- package/lib/sdk/webhook.secret.js +6 -21
- package/lib/sends.d.ts +29 -0
- package/lib/sends.js +384 -745
- package/lib/types.d.ts +48 -0
- package/lib/types.js +1 -0
- package/lib/utils.d.ts +1 -0
- package/lib/utils.js +7 -12
- package/package.json +9 -5
- package/lib/index.group.js +0 -19
- package/lib/index.guild.js +0 -36
- package/lib/sdk/client.websoket.group.js +0 -221
- package/lib/sdk/client.websoket.guild.js +0 -205
- package/lib/sdk/counter.js +0 -19
- package/lib/sdk/from.js +0 -44
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { QQBotAPI } from './api.js';
|
|
2
|
+
import { QQBotEventMap } from './message.js';
|
|
3
|
+
import { Options } from '../config.js';
|
|
4
|
+
export declare class QQBotClient extends QQBotAPI {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(opstion: Options);
|
|
7
|
+
on<T extends keyof QQBotEventMap>(key: T, val: (event: QQBotEventMap[T]) => any): this;
|
|
8
|
+
connect(): void;
|
|
9
|
+
}
|
|
@@ -13,21 +13,12 @@ class QQBotClient extends QQBotAPI {
|
|
|
13
13
|
#count = 0;
|
|
14
14
|
#client = [];
|
|
15
15
|
#ws = null;
|
|
16
|
-
/**
|
|
17
|
-
* 设置配置
|
|
18
|
-
* @param opstion
|
|
19
|
-
*/
|
|
20
16
|
constructor(opstion) {
|
|
21
17
|
super();
|
|
22
18
|
for (const key in opstion) {
|
|
23
19
|
config.set(key, opstion[key]);
|
|
24
20
|
}
|
|
25
21
|
}
|
|
26
|
-
/**
|
|
27
|
-
* 注册事件处理程序
|
|
28
|
-
* @param key 事件名称
|
|
29
|
-
* @param val 事件处理函数
|
|
30
|
-
*/
|
|
31
22
|
on(key, val) {
|
|
32
23
|
if (!this.#events[key]) {
|
|
33
24
|
this.#events[key] = [];
|
|
@@ -35,37 +26,25 @@ class QQBotClient extends QQBotAPI {
|
|
|
35
26
|
this.#events[key].push(val);
|
|
36
27
|
return this;
|
|
37
28
|
}
|
|
38
|
-
/**
|
|
39
|
-
* 定时鉴权
|
|
40
|
-
* @param cfg
|
|
41
|
-
* @returns
|
|
42
|
-
*/
|
|
43
29
|
async #setTimeoutBotConfig() {
|
|
44
30
|
const callBack = async () => {
|
|
45
31
|
const app_id = config.get('app_id');
|
|
46
|
-
if (!app_id)
|
|
47
|
-
return;
|
|
48
32
|
const secret = config.get('secret');
|
|
49
|
-
if (!secret)
|
|
33
|
+
if (!app_id || !secret) {
|
|
50
34
|
return;
|
|
51
|
-
|
|
52
|
-
const data = await this.getAuthentication(
|
|
35
|
+
}
|
|
36
|
+
const data = await this.getAuthentication();
|
|
53
37
|
config.set('access_token', data.access_token);
|
|
54
38
|
console.info('refresh', data.expires_in, 's');
|
|
55
|
-
setTimeout(callBack, data.expires_in * 1000);
|
|
39
|
+
setTimeout(() => void callBack(), data.expires_in * 1000);
|
|
56
40
|
};
|
|
57
41
|
await callBack();
|
|
58
42
|
}
|
|
59
|
-
/**
|
|
60
|
-
*
|
|
61
|
-
* @param cfg
|
|
62
|
-
* @param conversation
|
|
63
|
-
*/
|
|
64
43
|
connect() {
|
|
65
44
|
try {
|
|
66
45
|
const ws = config.get('ws');
|
|
67
46
|
if (!ws) {
|
|
68
|
-
this.#setTimeoutBotConfig();
|
|
47
|
+
void this.#setTimeoutBotConfig();
|
|
69
48
|
this.#app = new Koa();
|
|
70
49
|
this.#app.use(bodyParser());
|
|
71
50
|
const router = new Router();
|
|
@@ -85,8 +64,7 @@ class QQBotClient extends QQBotAPI {
|
|
|
85
64
|
ctx.req.on('end', () => (ctx.request.rawBody = rawData));
|
|
86
65
|
await next();
|
|
87
66
|
});
|
|
88
|
-
|
|
89
|
-
router.post(route, async (ctx) => {
|
|
67
|
+
router.post(route, ctx => {
|
|
90
68
|
const sign = ctx.req.headers['x-signature-ed25519'];
|
|
91
69
|
const timestamp = ctx.req.headers['x-signature-timestamp'];
|
|
92
70
|
const rawBody = ctx.request.rawBody;
|
|
@@ -97,27 +75,24 @@ class QQBotClient extends QQBotAPI {
|
|
|
97
75
|
return;
|
|
98
76
|
}
|
|
99
77
|
const body = ctx.request.body;
|
|
100
|
-
if (body.op
|
|
78
|
+
if (+body.op === 13) {
|
|
101
79
|
ctx.status = 200;
|
|
102
80
|
ctx.body = {
|
|
103
|
-
// 返回明文 token
|
|
104
81
|
plain_token: body.d.plain_token,
|
|
105
|
-
// 生成签名
|
|
106
82
|
signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
|
|
107
83
|
};
|
|
108
84
|
}
|
|
109
|
-
else if (body.op
|
|
85
|
+
else if (+body.op === 0) {
|
|
110
86
|
ctx.status = 204;
|
|
111
|
-
// 根据事件类型,处理事件
|
|
112
87
|
for (const event of this.#events[body.t] || []) {
|
|
113
88
|
event(body.d);
|
|
114
89
|
}
|
|
115
|
-
const
|
|
116
|
-
// 也可以分法到客户端。 发送失败需要处理 或清理调
|
|
90
|
+
const accessToken = config.get('access_token');
|
|
117
91
|
for (const client of this.#client) {
|
|
118
92
|
try {
|
|
119
|
-
if (
|
|
120
|
-
body['access_token'] =
|
|
93
|
+
if (accessToken) {
|
|
94
|
+
body['access_token'] = accessToken;
|
|
95
|
+
}
|
|
121
96
|
client.ws.send(JSON.stringify(body));
|
|
122
97
|
}
|
|
123
98
|
catch (e) {
|
|
@@ -128,22 +103,17 @@ class QQBotClient extends QQBotAPI {
|
|
|
128
103
|
});
|
|
129
104
|
this.#app.use(router.routes());
|
|
130
105
|
this.#app.use(router.allowedMethods());
|
|
131
|
-
// 启动服务
|
|
132
106
|
const server = this.#app.listen(cfg.port, () => {
|
|
133
107
|
console.log('Server running at http://localhost:' + cfg.port + route);
|
|
134
108
|
});
|
|
135
|
-
// 创建 WebSocketServer 并监听同一个端口
|
|
136
109
|
const wss = new WebSocketServer({ server: server });
|
|
137
110
|
console.log('Server running at ws://localhost:' + cfg.port + '/');
|
|
138
|
-
// 处理客户端连接
|
|
139
111
|
wss.on('connection', ws => {
|
|
140
112
|
const clientId = v4();
|
|
141
113
|
ws['clientId'] = clientId;
|
|
142
114
|
console.log(clientId, 'connection');
|
|
143
115
|
this.#client.push({ id: clientId, ws });
|
|
144
|
-
// 处理消息事件
|
|
145
116
|
ws.on('message', (message) => {
|
|
146
|
-
// 拿到消息
|
|
147
117
|
try {
|
|
148
118
|
const body = JSON.parse(message.toString());
|
|
149
119
|
for (const event of this.#events[body.t] || []) {
|
|
@@ -154,7 +124,6 @@ class QQBotClient extends QQBotAPI {
|
|
|
154
124
|
this.#error(e);
|
|
155
125
|
}
|
|
156
126
|
});
|
|
157
|
-
// 处理关闭事件
|
|
158
127
|
ws.on('close', () => {
|
|
159
128
|
console.log(`${clientId} disconnected`);
|
|
160
129
|
this.#client = this.#client.filter(client => client.id !== clientId);
|
|
@@ -163,7 +132,6 @@ class QQBotClient extends QQBotAPI {
|
|
|
163
132
|
}
|
|
164
133
|
else {
|
|
165
134
|
const reConnect = () => {
|
|
166
|
-
// 使用了ws服务器
|
|
167
135
|
this.#ws = new WebSocket(ws);
|
|
168
136
|
this.#ws.on('open', () => {
|
|
169
137
|
this.#count = 0;
|
|
@@ -171,11 +139,11 @@ class QQBotClient extends QQBotAPI {
|
|
|
171
139
|
});
|
|
172
140
|
this.#ws.on('message', data => {
|
|
173
141
|
try {
|
|
174
|
-
// 拿到消息
|
|
175
142
|
const body = JSON.parse(data.toString());
|
|
176
|
-
const
|
|
177
|
-
if (
|
|
178
|
-
config.set('access_token',
|
|
143
|
+
const accessToken = body['access_token'];
|
|
144
|
+
if (accessToken) {
|
|
145
|
+
config.set('access_token', accessToken);
|
|
146
|
+
}
|
|
179
147
|
for (const event of this.#events[body.t] || []) {
|
|
180
148
|
event(body.d);
|
|
181
149
|
}
|
|
@@ -186,10 +154,9 @@ class QQBotClient extends QQBotAPI {
|
|
|
186
154
|
});
|
|
187
155
|
this.#ws.on('close', () => {
|
|
188
156
|
console.log('ws closed');
|
|
189
|
-
|
|
190
|
-
if (this.#count > 5)
|
|
157
|
+
if (this.#count > 5) {
|
|
191
158
|
return;
|
|
192
|
-
|
|
159
|
+
}
|
|
193
160
|
setTimeout(() => {
|
|
194
161
|
reConnect();
|
|
195
162
|
}, 1300);
|
|
@@ -205,10 +172,6 @@ class QQBotClient extends QQBotAPI {
|
|
|
205
172
|
this.#error(e);
|
|
206
173
|
}
|
|
207
174
|
}
|
|
208
|
-
/**
|
|
209
|
-
*
|
|
210
|
-
* @param error
|
|
211
|
-
*/
|
|
212
175
|
#error(error) {
|
|
213
176
|
if (this.#events['ERROR']) {
|
|
214
177
|
for (const event of this.#events['ERROR'] || []) {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { QQBotAPI } from './api.js';
|
|
2
|
+
import { QQBotEventMap } from './message.js';
|
|
3
|
+
import { Options } from './typing.js';
|
|
4
|
+
export declare class QQBotClients extends QQBotAPI {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(opstion: Options);
|
|
7
|
+
on<T extends keyof QQBotEventMap>(key: T, val: (event: QQBotEventMap[T]) => any): this;
|
|
8
|
+
connect(gatewayURL?: string): Promise<void>;
|
|
9
|
+
}
|
|
@@ -2,62 +2,40 @@ import WebSocket from 'ws';
|
|
|
2
2
|
import { QQBotAPI } from './api.js';
|
|
3
3
|
import { config } from './config.js';
|
|
4
4
|
import { getIntentsMask } from './intents.js';
|
|
5
|
-
import { Counter } from '
|
|
5
|
+
import { Counter } from 'alemonjs/utils';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* 连接
|
|
9
|
-
*/
|
|
10
7
|
class QQBotClients extends QQBotAPI {
|
|
11
|
-
|
|
12
|
-
#counter = new Counter(1); // 初始值为1
|
|
13
|
-
// 标记是否已连接
|
|
8
|
+
#counter = new Counter(1);
|
|
14
9
|
#isConnected = false;
|
|
15
|
-
// 存储最新的消息序号
|
|
16
10
|
#heartbeat_interval = 30000;
|
|
17
|
-
// 鉴权
|
|
18
11
|
#IntervalId = null;
|
|
19
|
-
// url
|
|
20
12
|
#gatewayUrl = null;
|
|
21
|
-
/**
|
|
22
|
-
* 设置配置
|
|
23
|
-
* @param opstion
|
|
24
|
-
*/
|
|
25
13
|
constructor(opstion) {
|
|
26
14
|
super();
|
|
27
15
|
for (const key in opstion) {
|
|
28
16
|
config.set(key, opstion[key]);
|
|
29
17
|
}
|
|
30
18
|
}
|
|
31
|
-
/**
|
|
32
|
-
* 定时鉴权
|
|
33
|
-
* @param cfg
|
|
34
|
-
* @returns
|
|
35
|
-
*/
|
|
36
19
|
async #setTimeoutBotConfig() {
|
|
37
20
|
const accessToken = async () => {
|
|
38
21
|
const app_id = config.get('app_id');
|
|
39
22
|
const secret = config.get('secret');
|
|
40
|
-
if (!app_id || !secret)
|
|
23
|
+
if (!app_id || !secret) {
|
|
41
24
|
return;
|
|
42
|
-
|
|
43
|
-
const data = await this.getAuthentication(
|
|
25
|
+
}
|
|
26
|
+
const data = await this.getAuthentication();
|
|
44
27
|
config.set('access_token', data.access_token);
|
|
45
28
|
console.info('refresh', data.expires_in, 's');
|
|
46
|
-
setTimeout(accessToken, data.expires_in * 1000);
|
|
29
|
+
setTimeout(() => void accessToken(), data.expires_in * 1000);
|
|
47
30
|
};
|
|
48
31
|
await accessToken();
|
|
49
|
-
return;
|
|
50
32
|
}
|
|
51
|
-
/**
|
|
52
|
-
* 鉴权数据
|
|
53
|
-
* @returns
|
|
54
|
-
*/
|
|
55
33
|
#aut() {
|
|
56
34
|
const token = config.get('access_token');
|
|
57
35
|
const intents = config.get('intents');
|
|
58
36
|
const shard = config.get('shard');
|
|
59
37
|
return {
|
|
60
|
-
op: 2,
|
|
38
|
+
op: 2,
|
|
61
39
|
d: {
|
|
62
40
|
token: `QQBot ${token}`,
|
|
63
41
|
intents: getIntentsMask(intents),
|
|
@@ -72,145 +50,122 @@ class QQBotClients extends QQBotAPI {
|
|
|
72
50
|
}
|
|
73
51
|
#ws = null;
|
|
74
52
|
#events = {};
|
|
75
|
-
/**
|
|
76
|
-
* 注册事件处理程序
|
|
77
|
-
* @param key 事件名称
|
|
78
|
-
* @param val 事件处理函数
|
|
79
|
-
*/
|
|
80
53
|
on(key, val) {
|
|
81
|
-
if (!this.#events[key])
|
|
54
|
+
if (!this.#events[key]) {
|
|
82
55
|
this.#events[key] = [];
|
|
56
|
+
}
|
|
83
57
|
this.#events[key].push(val);
|
|
84
58
|
return this;
|
|
85
59
|
}
|
|
86
|
-
/**
|
|
87
|
-
*
|
|
88
|
-
* @param cfg
|
|
89
|
-
* @param conversation
|
|
90
|
-
*/
|
|
91
60
|
async connect(gatewayURL) {
|
|
92
|
-
// 定时模式
|
|
93
61
|
await this.#setTimeoutBotConfig();
|
|
94
|
-
// 请求url
|
|
95
62
|
if (!this.#gatewayUrl) {
|
|
96
63
|
this.#gatewayUrl = gatewayURL ?? (await this.gateway().then(res => res?.url));
|
|
97
64
|
}
|
|
98
|
-
if (!this.#gatewayUrl)
|
|
65
|
+
if (!this.#gatewayUrl) {
|
|
99
66
|
return;
|
|
100
|
-
|
|
101
|
-
const reconnect =
|
|
102
|
-
if (this.#counter.
|
|
67
|
+
}
|
|
68
|
+
const reconnect = () => {
|
|
69
|
+
if (this.#counter.value >= 5) {
|
|
103
70
|
console.info('The maximum number of reconnections has been reached, cancel reconnection');
|
|
104
71
|
return;
|
|
105
72
|
}
|
|
106
73
|
setTimeout(() => {
|
|
107
|
-
console.info('[ws] reconnecting...');
|
|
108
|
-
// 重新starrt
|
|
74
|
+
console.info('[ws-qqbot] reconnecting...');
|
|
109
75
|
start();
|
|
110
|
-
|
|
111
|
-
this.#counter.getNextId();
|
|
76
|
+
this.#counter.next();
|
|
112
77
|
}, 5000);
|
|
113
78
|
};
|
|
114
79
|
const start = () => {
|
|
115
80
|
if (this.#gatewayUrl) {
|
|
116
81
|
const map = {
|
|
117
|
-
0:
|
|
82
|
+
0: ({ t, d }) => {
|
|
118
83
|
if (this.#events[t]) {
|
|
119
84
|
try {
|
|
120
85
|
for (const event of this.#events[t]) {
|
|
121
|
-
|
|
122
|
-
if (typeof event != 'function')
|
|
86
|
+
if (typeof event !== 'function') {
|
|
123
87
|
continue;
|
|
88
|
+
}
|
|
124
89
|
event(d);
|
|
125
90
|
}
|
|
126
91
|
}
|
|
127
92
|
catch (err) {
|
|
128
93
|
if (this.#events['ERROR']) {
|
|
129
94
|
for (const event of this.#events['ERROR']) {
|
|
130
|
-
|
|
131
|
-
if (typeof event != 'function')
|
|
95
|
+
if (typeof event !== 'function') {
|
|
132
96
|
continue;
|
|
97
|
+
}
|
|
133
98
|
event(err);
|
|
134
99
|
}
|
|
135
100
|
}
|
|
136
101
|
}
|
|
137
102
|
}
|
|
138
|
-
// Ready Event,鉴权成功
|
|
139
103
|
if (t === 'READY') {
|
|
104
|
+
if (this.#IntervalId) {
|
|
105
|
+
clearInterval(this.#IntervalId);
|
|
106
|
+
}
|
|
140
107
|
this.#IntervalId = setInterval(() => {
|
|
141
|
-
if (this.#isConnected) {
|
|
142
|
-
this.#ws
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}));
|
|
108
|
+
if (this.#isConnected && this.#ws) {
|
|
109
|
+
this.#ws.send(JSON.stringify({
|
|
110
|
+
op: 1,
|
|
111
|
+
d: null
|
|
112
|
+
}));
|
|
147
113
|
}
|
|
148
114
|
}, this.#heartbeat_interval);
|
|
149
115
|
}
|
|
150
|
-
// Resumed Event,恢复连接成功
|
|
151
116
|
if (t === 'RESUMED') {
|
|
152
|
-
console.info('[ws] restore connection');
|
|
153
|
-
// 重制次数
|
|
117
|
+
console.info('[ws-qqbot] restore connection');
|
|
154
118
|
this.#counter.reStart();
|
|
155
119
|
}
|
|
156
|
-
return;
|
|
157
120
|
},
|
|
158
121
|
6: ({ d }) => {
|
|
159
|
-
console.info('[ws] connection attempt', d);
|
|
160
|
-
return;
|
|
122
|
+
console.info('[ws-qqbot] connection attempt', d);
|
|
161
123
|
},
|
|
162
|
-
7:
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
// 取消鉴权发送
|
|
166
|
-
if (this.#IntervalId)
|
|
124
|
+
7: ({ d }) => {
|
|
125
|
+
console.info('[ws-qqbot] reconnect', d);
|
|
126
|
+
if (this.#IntervalId) {
|
|
167
127
|
clearInterval(this.#IntervalId);
|
|
168
|
-
|
|
128
|
+
}
|
|
169
129
|
},
|
|
170
130
|
9: ({ d }) => {
|
|
171
|
-
console.info('[ws] parameter error', d);
|
|
172
|
-
return;
|
|
131
|
+
console.info('[ws-qqbot] parameter error', d);
|
|
173
132
|
},
|
|
174
133
|
10: ({ d }) => {
|
|
175
|
-
// 重制次数
|
|
176
134
|
this.#isConnected = true;
|
|
177
|
-
// 记录新循环
|
|
178
135
|
this.#heartbeat_interval = d.heartbeat_interval;
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
136
|
+
if (this.#ws) {
|
|
137
|
+
this.#ws.send(JSON.stringify(this.#aut()));
|
|
138
|
+
}
|
|
182
139
|
},
|
|
183
140
|
11: () => {
|
|
184
|
-
|
|
185
|
-
console.info('[ws] heartbeat transmission');
|
|
186
|
-
// 重制次数
|
|
141
|
+
console.info('[ws-qqbot] heartbeat transmission');
|
|
187
142
|
this.#counter.reStart();
|
|
188
|
-
return;
|
|
189
143
|
},
|
|
190
144
|
12: ({ d }) => {
|
|
191
|
-
console.
|
|
192
|
-
return;
|
|
145
|
+
console.debug('[ws-qqbot] platform data', d);
|
|
193
146
|
}
|
|
194
147
|
};
|
|
195
|
-
// 连接
|
|
196
148
|
this.#ws = new WebSocket(this.#gatewayUrl);
|
|
197
149
|
this.#ws.on('open', () => {
|
|
198
|
-
console.info('[ws] open');
|
|
150
|
+
console.info('[ws-qqbot] open');
|
|
199
151
|
});
|
|
200
|
-
|
|
201
|
-
this.#ws.on('message', async (msg) => {
|
|
152
|
+
this.#ws.on('message', msg => {
|
|
202
153
|
const message = JSON.parse(msg.toString('utf8'));
|
|
203
|
-
if (process.env.NTQQ_WS
|
|
154
|
+
if (process.env.NTQQ_WS === 'dev') {
|
|
204
155
|
console.info('message', message);
|
|
205
|
-
|
|
156
|
+
}
|
|
206
157
|
if (map[message.op]) {
|
|
207
158
|
map[message.op](message);
|
|
208
159
|
}
|
|
209
160
|
});
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
161
|
+
this.#ws.on('close', err => {
|
|
162
|
+
this.#isConnected = false;
|
|
163
|
+
if (this.#IntervalId) {
|
|
164
|
+
clearInterval(this.#IntervalId);
|
|
165
|
+
this.#IntervalId = null;
|
|
166
|
+
}
|
|
167
|
+
void reconnect();
|
|
168
|
+
console.info('[ws-qqbot] close', err);
|
|
214
169
|
});
|
|
215
170
|
}
|
|
216
171
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const config: Map<string, any>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const filterHeaders = (headers = {}) => {
|
|
2
|
+
if (!headers) {
|
|
3
|
+
return headers;
|
|
4
|
+
}
|
|
5
|
+
const filtered = {};
|
|
6
|
+
const sensitiveKeys = [/^authorization$/i, /^cookie$/i, /^set-cookie$/i, /token/i, /key/i, /jwt/i, /^session[-_]id$/i, /^uid$/i, /^user[-_]id$/i];
|
|
7
|
+
for (const key in headers) {
|
|
8
|
+
if (/^_/.test(key)) {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
if (typeof key === 'symbol') {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
if (typeof headers[key] === 'function') {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (sensitiveKeys.some(re => re.test(key))) {
|
|
18
|
+
filtered[key] = '******';
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
filtered[key] = headers[key];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return filtered;
|
|
25
|
+
};
|
|
26
|
+
const filterConfig = (config = {}) => {
|
|
27
|
+
if (!config) {
|
|
28
|
+
return config;
|
|
29
|
+
}
|
|
30
|
+
const filtered = {};
|
|
31
|
+
for (const key in config) {
|
|
32
|
+
if (/^_/.test(key)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (typeof key === 'symbol') {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (typeof config[key] === 'function') {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
filtered[key] = config[key];
|
|
42
|
+
}
|
|
43
|
+
return filtered;
|
|
44
|
+
};
|
|
45
|
+
const filterRequest = (request = {}) => {
|
|
46
|
+
if (!request) {
|
|
47
|
+
return request;
|
|
48
|
+
}
|
|
49
|
+
const filtered = {};
|
|
50
|
+
for (const key in request) {
|
|
51
|
+
if (/^_/.test(key)) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (typeof key === 'symbol') {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (typeof request[key] === 'function') {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
filtered[key] = request[key];
|
|
61
|
+
}
|
|
62
|
+
return filtered;
|
|
63
|
+
};
|
|
64
|
+
const loggerError = err => {
|
|
65
|
+
logger.error('[axios] error', {
|
|
66
|
+
config: {
|
|
67
|
+
headers: filterHeaders(err?.config?.headers),
|
|
68
|
+
params: err?.config?.params,
|
|
69
|
+
data: err?.config?.data
|
|
70
|
+
},
|
|
71
|
+
response: {
|
|
72
|
+
status: err?.response?.status,
|
|
73
|
+
statusText: err?.response?.statusText,
|
|
74
|
+
headers: filterHeaders(err?.response?.headers),
|
|
75
|
+
config: filterConfig(err?.response?.config),
|
|
76
|
+
request: filterRequest(err?.response?.request),
|
|
77
|
+
data: err?.response?.data
|
|
78
|
+
},
|
|
79
|
+
message: err?.message
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
const createAxiosInstance = async (service, options) => {
|
|
83
|
+
try {
|
|
84
|
+
const res = await service(options);
|
|
85
|
+
return res?.data ?? {};
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
loggerError(err);
|
|
89
|
+
throw err?.response?.data ?? err;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export { createAxiosInstance };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const AvailableIntentsEventsEnum: readonly ["GUILDS", "GUILD_MEMBERS", "GUILD_MESSAGES", "GUILD_MESSAGE_REACTIONS", "DIRECT_MESSAGE", "INTERACTION", "MESSAGE_AUDIT", "FORUMS_EVENT", "AUDIO_ACTION", "PUBLIC_GUILD_MESSAGES", "GROUP_AND_C2C_EVENT"];
|
|
2
|
+
export type IntentsEnum = (typeof AvailableIntentsEventsEnum)[number];
|
|
3
|
+
export declare function getIntentsMask(intents: IntentsEnum[]): number;
|