@alemonjs/qq-bot 2.1.20 → 2.1.21
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/sdk/client.webhook.js +12 -3
- package/lib/sdk/client.websoket.js +10 -1
- package/package.json +1 -1
|
@@ -7,6 +7,15 @@ import { config } from './config.js';
|
|
|
7
7
|
import { v4 } from 'uuid';
|
|
8
8
|
import { WebSocketServer, WebSocket } from 'ws';
|
|
9
9
|
|
|
10
|
+
const normalizeGatewayMessage = (message) => {
|
|
11
|
+
if (!message?.id || !message?.d || typeof message.d !== 'object' || Array.isArray(message.d)) {
|
|
12
|
+
return message;
|
|
13
|
+
}
|
|
14
|
+
if (message.d.id === undefined || message.d.id === null || message.d.id === '') {
|
|
15
|
+
message.d.id = message.id;
|
|
16
|
+
}
|
|
17
|
+
return message;
|
|
18
|
+
};
|
|
10
19
|
class QQBotClient extends QQBotAPI {
|
|
11
20
|
#events = {};
|
|
12
21
|
#app = null;
|
|
@@ -74,7 +83,7 @@ class QQBotClient extends QQBotAPI {
|
|
|
74
83
|
ctx.body = { msg: 'invalid signature' };
|
|
75
84
|
return;
|
|
76
85
|
}
|
|
77
|
-
const body = ctx.request.body;
|
|
86
|
+
const body = normalizeGatewayMessage(ctx.request.body);
|
|
78
87
|
if (+body.op === 13) {
|
|
79
88
|
ctx.status = 200;
|
|
80
89
|
ctx.body = {
|
|
@@ -115,7 +124,7 @@ class QQBotClient extends QQBotAPI {
|
|
|
115
124
|
this.#client.push({ id: clientId, ws });
|
|
116
125
|
ws.on('message', (message) => {
|
|
117
126
|
try {
|
|
118
|
-
const body = JSON.parse(message.toString());
|
|
127
|
+
const body = normalizeGatewayMessage(JSON.parse(message.toString()));
|
|
119
128
|
for (const event of this.#events[body.t] || []) {
|
|
120
129
|
event(body.d);
|
|
121
130
|
}
|
|
@@ -139,7 +148,7 @@ class QQBotClient extends QQBotAPI {
|
|
|
139
148
|
});
|
|
140
149
|
this.#ws.on('message', data => {
|
|
141
150
|
try {
|
|
142
|
-
const body = JSON.parse(data.toString());
|
|
151
|
+
const body = normalizeGatewayMessage(JSON.parse(data.toString()));
|
|
143
152
|
const accessToken = body['access_token'];
|
|
144
153
|
if (accessToken) {
|
|
145
154
|
config.set('access_token', accessToken);
|
|
@@ -4,6 +4,15 @@ import { config } from './config.js';
|
|
|
4
4
|
import { getIntentsMask } from './intents.js';
|
|
5
5
|
import { Counter } from 'alemonjs/utils';
|
|
6
6
|
|
|
7
|
+
const normalizeGatewayMessage = (message) => {
|
|
8
|
+
if (!message?.id || !message?.d || typeof message.d !== 'object' || Array.isArray(message.d)) {
|
|
9
|
+
return message;
|
|
10
|
+
}
|
|
11
|
+
if (message.d.id === undefined || message.d.id === null || message.d.id === '') {
|
|
12
|
+
message.d.id = message.id;
|
|
13
|
+
}
|
|
14
|
+
return message;
|
|
15
|
+
};
|
|
7
16
|
class QQBotClients extends QQBotAPI {
|
|
8
17
|
#counter = new Counter(1);
|
|
9
18
|
#isConnected = false;
|
|
@@ -150,7 +159,7 @@ class QQBotClients extends QQBotAPI {
|
|
|
150
159
|
console.info('[ws-qqbot] open');
|
|
151
160
|
});
|
|
152
161
|
this.#ws.on('message', msg => {
|
|
153
|
-
const message = JSON.parse(msg.toString('utf8'));
|
|
162
|
+
const message = normalizeGatewayMessage(JSON.parse(msg.toString('utf8')));
|
|
154
163
|
if (process.env.NTQQ_WS === 'dev') {
|
|
155
164
|
console.info('message', message);
|
|
156
165
|
}
|