@alemonjs/qq-bot 2.1.19 → 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/config.d.ts +1 -0
- package/lib/config.js +6 -3
- package/lib/register.js +19 -44
- package/lib/sdk/client.webhook.js +12 -3
- package/lib/sdk/client.websoket.js +10 -1
- package/package.json +1 -1
package/lib/config.d.ts
CHANGED
|
@@ -7,4 +7,5 @@ export type Options = {
|
|
|
7
7
|
hideUnsupported?: boolean | number;
|
|
8
8
|
} & sdkOptions;
|
|
9
9
|
export declare const getQQBotConfig: () => Options;
|
|
10
|
+
export declare const getIdentity: (UserId: string) => readonly [boolean, string];
|
|
10
11
|
export declare const getMaster: (UserId: string) => readonly [boolean, string];
|
package/lib/config.js
CHANGED
|
@@ -5,13 +5,16 @@ const getQQBotConfig = () => {
|
|
|
5
5
|
const value = getConfigValue() || {};
|
|
6
6
|
return value[platform] || {};
|
|
7
7
|
};
|
|
8
|
-
const
|
|
9
|
-
const isMasterUser = isMaster(UserId, platform);
|
|
8
|
+
const getIdentity = (UserId) => {
|
|
9
|
+
const isMasterUser = UserId ? isMaster(UserId, platform) : false;
|
|
10
10
|
const UserKey = createUserHashKey({
|
|
11
11
|
Platform: platform,
|
|
12
12
|
UserId
|
|
13
13
|
});
|
|
14
14
|
return [isMasterUser, UserKey];
|
|
15
15
|
};
|
|
16
|
+
const getMaster = (UserId) => {
|
|
17
|
+
return getIdentity(UserId);
|
|
18
|
+
};
|
|
16
19
|
|
|
17
|
-
export { getMaster, getQQBotConfig, platform };
|
|
20
|
+
export { getIdentity, getMaster, getQQBotConfig, platform };
|
package/lib/register.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { cbpPlatform, FormatEvent, ResultCode, createResult } from 'alemonjs';
|
|
2
2
|
import { GROUP_AT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, AT_MESSAGE_CREATE, MESSAGE_CREATE } from './sends.js';
|
|
3
|
-
import { getQQBotConfig, platform, getMaster } from './config.js';
|
|
3
|
+
import { getQQBotConfig, platform, getMaster, getIdentity } from './config.js';
|
|
4
4
|
|
|
5
5
|
const register = (client) => {
|
|
6
6
|
const config = getQQBotConfig();
|
|
@@ -39,19 +39,22 @@ const register = (client) => {
|
|
|
39
39
|
auditTime
|
|
40
40
|
};
|
|
41
41
|
};
|
|
42
|
+
const createUserMeta = (UserId, extra = {}) => {
|
|
43
|
+
const [IsMaster, UserKey] = getIdentity(UserId);
|
|
44
|
+
return {
|
|
45
|
+
UserId,
|
|
46
|
+
UserKey,
|
|
47
|
+
IsMaster,
|
|
48
|
+
IsBot: false,
|
|
49
|
+
...extra
|
|
50
|
+
};
|
|
51
|
+
};
|
|
42
52
|
client.on('GROUP_ADD_ROBOT', event => {
|
|
43
53
|
cbp.send(FormatEvent.create('guild.join')
|
|
44
54
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
45
55
|
.addGuild({ GuildId: event.group_openid, SpaceId: `GROUP:${event.group_openid}` })
|
|
46
56
|
.addChannel({ ChannelId: event.group_openid })
|
|
47
|
-
.addUser({
|
|
48
|
-
UserId: event.op_member_openid,
|
|
49
|
-
UserKey: event.op_member_openid,
|
|
50
|
-
UserAvatar: createUserAvatarURL(event.op_member_openid),
|
|
51
|
-
IsMaster: false,
|
|
52
|
-
IsBot: false
|
|
53
|
-
})
|
|
54
|
-
.addMessage({ MessageId: '' })
|
|
57
|
+
.addUser(createUserMeta(event.op_member_openid, { UserAvatar: createUserAvatarURL(event.op_member_openid) }))
|
|
55
58
|
.add({ tag: 'GROUP_ADD_ROBOT' }).value);
|
|
56
59
|
});
|
|
57
60
|
client.on('GROUP_DEL_ROBOT', event => {
|
|
@@ -59,14 +62,7 @@ const register = (client) => {
|
|
|
59
62
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
60
63
|
.addGuild({ GuildId: event.group_openid, SpaceId: `GROUP:${event.group_openid}` })
|
|
61
64
|
.addChannel({ ChannelId: event.group_openid })
|
|
62
|
-
.addUser({
|
|
63
|
-
UserId: event.op_member_openid,
|
|
64
|
-
UserKey: event.op_member_openid,
|
|
65
|
-
UserAvatar: createUserAvatarURL(event.op_member_openid),
|
|
66
|
-
IsMaster: false,
|
|
67
|
-
IsBot: false
|
|
68
|
-
})
|
|
69
|
-
.addMessage({ MessageId: '' })
|
|
65
|
+
.addUser(createUserMeta(event.op_member_openid, { UserAvatar: createUserAvatarURL(event.op_member_openid) }))
|
|
70
66
|
.add({ tag: 'GROUP_DEL_ROBOT' }).value);
|
|
71
67
|
});
|
|
72
68
|
client.on('GROUP_MESSAGE_CREATE', event => {
|
|
@@ -338,7 +334,6 @@ const register = (client) => {
|
|
|
338
334
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
339
335
|
.addGuild({ GuildId: event.guild_id ?? '', SpaceId: `GUILD:${event.guild_id ?? ''}` })
|
|
340
336
|
.addChannel({ ChannelId: event.id ?? '' })
|
|
341
|
-
.addMessage({ MessageId: '' })
|
|
342
337
|
.add({ tag: 'CHANNEL_CREATE' }).value);
|
|
343
338
|
});
|
|
344
339
|
client.on('CHANNEL_DELETE', event => {
|
|
@@ -346,25 +341,20 @@ const register = (client) => {
|
|
|
346
341
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
347
342
|
.addGuild({ GuildId: event.guild_id ?? '', SpaceId: `GUILD:${event.guild_id ?? ''}` })
|
|
348
343
|
.addChannel({ ChannelId: event.id ?? '' })
|
|
349
|
-
.addMessage({ MessageId: '' })
|
|
350
344
|
.add({ tag: 'CHANNEL_DELETE' }).value);
|
|
351
345
|
});
|
|
352
346
|
client.on('GUILD_CREATE', event => {
|
|
353
347
|
cbp.send(FormatEvent.create('guild.join')
|
|
354
348
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
355
349
|
.addGuild({ GuildId: event.id ?? '', SpaceId: `GUILD:${event.id ?? ''}` })
|
|
356
|
-
.addChannel({ ChannelId: '' })
|
|
357
350
|
.addUser({ UserId: event.op_user_id ?? '', UserKey: '', IsMaster: false, IsBot: false })
|
|
358
|
-
.addMessage({ MessageId: '' })
|
|
359
351
|
.add({ tag: 'GUILD_CREATE' }).value);
|
|
360
352
|
});
|
|
361
353
|
client.on('GUILD_DELETE', event => {
|
|
362
354
|
cbp.send(FormatEvent.create('guild.exit')
|
|
363
355
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
364
356
|
.addGuild({ GuildId: event.id ?? '', SpaceId: `GUILD:${event.id ?? ''}` })
|
|
365
|
-
.addChannel({ ChannelId: '' })
|
|
366
357
|
.addUser({ UserId: event.op_user_id ?? '', UserKey: '', IsMaster: false, IsBot: false })
|
|
367
|
-
.addMessage({ MessageId: '' })
|
|
368
358
|
.add({ tag: 'GUILD_DELETE' }).value);
|
|
369
359
|
});
|
|
370
360
|
client.on('GUILD_MEMBER_ADD', event => {
|
|
@@ -373,9 +363,7 @@ const register = (client) => {
|
|
|
373
363
|
cbp.send(FormatEvent.create('member.add')
|
|
374
364
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
375
365
|
.addGuild({ GuildId: event.guild_id ?? '', SpaceId: `GUILD:${event.guild_id ?? ''}` })
|
|
376
|
-
.addChannel({ ChannelId: '' })
|
|
377
366
|
.addUser({ UserId: UserId, UserKey, UserName: event.user?.username ?? '', UserAvatar: createUserAvatarURL(UserId), IsMaster: isMaster, IsBot: false })
|
|
378
|
-
.addMessage({ MessageId: '' })
|
|
379
367
|
.add({ tag: 'GUILD_MEMBER_ADD' }).value);
|
|
380
368
|
});
|
|
381
369
|
client.on('GUILD_MEMBER_REMOVE', event => {
|
|
@@ -384,9 +372,7 @@ const register = (client) => {
|
|
|
384
372
|
cbp.send(FormatEvent.create('member.remove')
|
|
385
373
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
386
374
|
.addGuild({ GuildId: event.guild_id ?? '', SpaceId: `GUILD:${event.guild_id ?? ''}` })
|
|
387
|
-
.addChannel({ ChannelId: '' })
|
|
388
375
|
.addUser({ UserId: UserId, UserKey, UserName: event.user?.username ?? '', UserAvatar: createUserAvatarURL(UserId), IsMaster: isMaster, IsBot: false })
|
|
389
|
-
.addMessage({ MessageId: '' })
|
|
390
376
|
.add({ tag: 'GUILD_MEMBER_REMOVE' }).value);
|
|
391
377
|
});
|
|
392
378
|
client.on('GUILD_MEMBER_UPDATE', event => {
|
|
@@ -395,23 +381,19 @@ const register = (client) => {
|
|
|
395
381
|
cbp.send(FormatEvent.create('member.update')
|
|
396
382
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
397
383
|
.addGuild({ GuildId: event.guild_id ?? '', SpaceId: `GUILD:${event.guild_id ?? ''}` })
|
|
398
|
-
.addChannel({ ChannelId: '' })
|
|
399
384
|
.addUser({ UserId: UserId, UserKey, UserName: event.user?.username ?? '', UserAvatar: createUserAvatarURL(UserId), IsMaster: isMaster, IsBot: false })
|
|
400
|
-
.addMessage({ MessageId: '' })
|
|
401
385
|
.add({ tag: 'GUILD_MEMBER_UPDATE' }).value);
|
|
402
386
|
});
|
|
403
387
|
client.on('FRIEND_ADD', event => {
|
|
404
388
|
cbp.send(FormatEvent.create('private.friend.add')
|
|
405
389
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
406
|
-
.addUser(
|
|
407
|
-
.addMessage({ MessageId: '' })
|
|
390
|
+
.addUser(createUserMeta(event.openid ?? ''))
|
|
408
391
|
.add({ tag: 'FRIEND_ADD' }).value);
|
|
409
392
|
});
|
|
410
393
|
client.on('FRIEND_DEL', event => {
|
|
411
394
|
cbp.send(FormatEvent.create('private.friend.remove')
|
|
412
395
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
413
|
-
.addUser(
|
|
414
|
-
.addMessage({ MessageId: '' })
|
|
396
|
+
.addUser(createUserMeta(event.openid ?? ''))
|
|
415
397
|
.add({ tag: 'FRIEND_DEL' }).value);
|
|
416
398
|
});
|
|
417
399
|
client.on('CHANNEL_UPDATE', event => {
|
|
@@ -419,15 +401,12 @@ const register = (client) => {
|
|
|
419
401
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
420
402
|
.addGuild({ GuildId: event.guild_id ?? '', SpaceId: `GUILD:${event.guild_id ?? ''}` })
|
|
421
403
|
.addChannel({ ChannelId: event.id ?? '' })
|
|
422
|
-
.addMessage({ MessageId: '' })
|
|
423
404
|
.add({ tag: 'CHANNEL_UPDATE' }).value);
|
|
424
405
|
});
|
|
425
406
|
client.on('GUILD_UPDATE', event => {
|
|
426
407
|
cbp.send(FormatEvent.create('guild.update')
|
|
427
408
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
428
409
|
.addGuild({ GuildId: event.id ?? '', SpaceId: `GUILD:${event.id ?? ''}` })
|
|
429
|
-
.addChannel({ ChannelId: '' })
|
|
430
|
-
.addMessage({ MessageId: '' })
|
|
431
410
|
.add({ tag: 'GUILD_UPDATE' }).value);
|
|
432
411
|
});
|
|
433
412
|
client.on('GROUP_MSG_RECEIVE', event => {
|
|
@@ -435,7 +414,7 @@ const register = (client) => {
|
|
|
435
414
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
436
415
|
.addGuild({ GuildId: event.group_openid, SpaceId: `GROUP:${event.group_openid}` })
|
|
437
416
|
.addChannel({ ChannelId: event.group_openid })
|
|
438
|
-
.addUser(
|
|
417
|
+
.addUser(createUserMeta(event.op_member_openid))
|
|
439
418
|
.addMessage({ MessageId: `group_msg_receive_${event.group_openid}_${event.timestamp}` })
|
|
440
419
|
.add({ tag: 'GROUP_MSG_RECEIVE' }).value);
|
|
441
420
|
});
|
|
@@ -444,7 +423,7 @@ const register = (client) => {
|
|
|
444
423
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
445
424
|
.addGuild({ GuildId: event.group_openid, SpaceId: `GROUP:${event.group_openid}` })
|
|
446
425
|
.addChannel({ ChannelId: event.group_openid })
|
|
447
|
-
.addUser(
|
|
426
|
+
.addUser(createUserMeta(event.op_member_openid))
|
|
448
427
|
.addMessage({ MessageId: `group_msg_reject_${event.group_openid}_${event.timestamp}` })
|
|
449
428
|
.add({ tag: 'GROUP_MSG_REJECT' }).value);
|
|
450
429
|
});
|
|
@@ -454,9 +433,7 @@ const register = (client) => {
|
|
|
454
433
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
455
434
|
.addGuild({ GuildId: meta.groupId, SpaceId: `GROUP:${meta.groupId}` })
|
|
456
435
|
.addChannel({ ChannelId: meta.groupId })
|
|
457
|
-
.addUser({ UserId: '', UserKey: '', IsMaster: false, IsBot: false })
|
|
458
436
|
.addMessage({ MessageId: meta.messageId })
|
|
459
|
-
.addText({ MessageText: meta.auditTime })
|
|
460
437
|
.add({ tag: 'MESSAGE_AUDIT_PASS' }).value);
|
|
461
438
|
});
|
|
462
439
|
client.on('MESSAGE_AUDIT_REJECT', event => {
|
|
@@ -465,22 +442,20 @@ const register = (client) => {
|
|
|
465
442
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
466
443
|
.addGuild({ GuildId: meta.groupId, SpaceId: `GROUP:${meta.groupId}` })
|
|
467
444
|
.addChannel({ ChannelId: meta.groupId })
|
|
468
|
-
.addUser({ UserId: '', UserKey: '', IsMaster: false, IsBot: false })
|
|
469
445
|
.addMessage({ MessageId: meta.messageId })
|
|
470
|
-
.addText({ MessageText: meta.auditTime })
|
|
471
446
|
.add({ tag: 'MESSAGE_AUDIT_REJECT' }).value);
|
|
472
447
|
});
|
|
473
448
|
client.on('C2C_MSG_RECEIVE', event => {
|
|
474
449
|
cbp.send(FormatEvent.create('private.notice.create')
|
|
475
450
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
476
|
-
.addUser(
|
|
451
|
+
.addUser(createUserMeta(event.openid ?? ''))
|
|
477
452
|
.addMessage({ MessageId: `c2c_msg_receive_${event.openid}_${event.timestamp}` })
|
|
478
453
|
.add({ tag: 'C2C_MSG_RECEIVE' }).value);
|
|
479
454
|
});
|
|
480
455
|
client.on('C2C_MSG_REJECT', event => {
|
|
481
456
|
cbp.send(FormatEvent.create('private.notice.create')
|
|
482
457
|
.addPlatform({ Platform: platform, value: event, BotId: botId })
|
|
483
|
-
.addUser(
|
|
458
|
+
.addUser(createUserMeta(event.openid ?? ''))
|
|
484
459
|
.addMessage({ MessageId: `c2c_msg_reject_${event.openid}_${event.timestamp}` })
|
|
485
460
|
.add({ tag: 'C2C_MSG_REJECT' }).value);
|
|
486
461
|
});
|
|
@@ -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
|
}
|