@alemonjs/qq-bot 0.0.19 → 0.0.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.
Files changed (48) hide show
  1. package/README.md +6 -10
  2. package/lib/index.d.ts +2 -2
  3. package/lib/index.group.js +6 -203
  4. package/lib/index.guild.js +25 -298
  5. package/lib/index.js +42 -403
  6. package/lib/index.webhook.js +26 -0
  7. package/lib/register.d.ts +3 -0
  8. package/lib/register.js +405 -0
  9. package/lib/sdk/api.d.ts +5 -10
  10. package/lib/sdk/api.js +5 -10
  11. package/lib/sdk/client.websoket.js +221 -0
  12. package/lib/sdk/intents.js +12 -23
  13. package/lib/send/index.js +417 -366
  14. package/lib/sends.js +576 -0
  15. package/package.json +1 -1
  16. package/lib/api.d.ts +0 -975
  17. package/lib/api.js +0 -1188
  18. package/lib/client.d.ts +0 -26
  19. package/lib/client.js +0 -212
  20. package/lib/config.js +0 -3
  21. package/lib/from.js +0 -37
  22. package/lib/message/AT_MESSAGE_CREATE.d.ts +0 -37
  23. package/lib/message/C2C_MESSAGE_CREATE.d.ts +0 -11
  24. package/lib/message/CHANNEL_CREATE.d.ts +0 -17
  25. package/lib/message/CHANNEL_DELETE.d.ts +0 -22
  26. package/lib/message/CHANNEL_UPDATE.d.ts +0 -22
  27. package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +0 -36
  28. package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +0 -24
  29. package/lib/message/ERROR.d.ts +0 -3
  30. package/lib/message/GROUP_AT_MESSAGE_CREATE.d.ts +0 -16
  31. package/lib/message/GUILD_CREATE.d.ts +0 -22
  32. package/lib/message/GUILD_DELETE.d.ts +0 -22
  33. package/lib/message/GUILD_MEMBER_ADD.d.ts +0 -21
  34. package/lib/message/GUILD_MEMBER_REMOVE.d.ts +0 -21
  35. package/lib/message/GUILD_MEMBER_UPDATE.d.ts +0 -21
  36. package/lib/message/GUILD_UPDATE.d.ts +0 -22
  37. package/lib/message/INTERACTION_CREATE.d.ts +0 -8
  38. package/lib/message/MESSAGE_CREATE.d.ts +0 -11
  39. package/lib/message/MESSAGE_DELETE.d.ts +0 -11
  40. package/lib/message/MESSAGE_REACTION_ADD.d.ts +0 -15
  41. package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +0 -15
  42. package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +0 -21
  43. package/lib/message/READY.d.ts +0 -11
  44. package/lib/message.d.ts +0 -49
  45. package/lib/typing.d.ts +0 -73
  46. package/lib/webhook.js +0 -51
  47. /package/lib/sdk/{websoket.group.js → client.websoket.group.js} +0 -0
  48. /package/lib/sdk/{websoket.guild.js → client.websoket.guild.js} +0 -0
package/lib/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
2
- import { QQBotClient } from './sdk/client.js';
3
- import { AT_MESSAGE_CREATE, GROUP_AT_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, MESSAGE_CREATE } from './send/index.js';
1
+ import { getConfigValue } from 'alemonjs';
4
2
  import QQBotGroup from './index.group.js';
5
3
  import QQBotGuild from './index.guild.js';
6
- import { isGuild } from './utils.js';
4
+ import QQBotHook from './index.webhook.js';
5
+ import { QQBotClients } from './sdk/client.websoket.js';
6
+ import { platform, register, createClientAPI } from './register.js';
7
7
  export { useMode } from './hook.js';
8
8
 
9
9
  const client = new Proxy({}, {
@@ -16,13 +16,12 @@ const client = new Proxy({}, {
16
16
  return undefined;
17
17
  }
18
18
  });
19
- const platform = 'qq-bot';
20
19
  var index = definePlatform(() => {
21
20
  let value = getConfigValue();
22
21
  if (!value)
23
22
  value = {};
24
23
  const config = value[platform];
25
- if (config.mode == 'guild') {
24
+ if (config?.mode == 'guild') {
26
25
  if (typeof QQBotGuild.callback == 'function') {
27
26
  return QQBotGuild.callback();
28
27
  }
@@ -30,7 +29,7 @@ var index = definePlatform(() => {
30
29
  return QQBotGuild.callback;
31
30
  }
32
31
  }
33
- else if (config.mode == 'group') {
32
+ else if (config?.mode == 'group') {
34
33
  if (typeof QQBotGroup.callback == 'function') {
35
34
  return QQBotGroup.callback();
36
35
  }
@@ -38,408 +37,48 @@ var index = definePlatform(() => {
38
37
  return QQBotGroup.callback;
39
38
  }
40
39
  }
41
- const client = new QQBotClient({
42
- secret: config?.secret,
40
+ else if (config?.route || config?.port || config?.ws) {
41
+ if (typeof QQBotHook.callback == 'function') {
42
+ return QQBotHook.callback();
43
+ }
44
+ else {
45
+ return QQBotHook.callback;
46
+ }
47
+ }
48
+ const client = new QQBotClients({
43
49
  app_id: config?.app_id,
44
- route: config?.route,
50
+ intents: config?.intents ?? config?.is_private
51
+ ? [
52
+ 'GUILDS', // base
53
+ 'GUILD_MEMBERS', // base
54
+ 'GUILD_MESSAGES',
55
+ 'GUILD_MESSAGE_REACTIONS',
56
+ 'DIRECT_MESSAGE',
57
+ 'INTERACTION',
58
+ 'FORUMS_EVENT'
59
+ ]
60
+ : [
61
+ 'GUILDS', // base
62
+ 'GUILD_MEMBERS', // base
63
+ 'GUILD_MESSAGE_REACTIONS',
64
+ 'DIRECT_MESSAGE',
65
+ 'INTERACTION',
66
+ 'PUBLIC_GUILD_MESSAGES',
67
+ 'GROUP_AND_C2C_EVENT'
68
+ ],
69
+ is_private: config?.is_private ?? false,
70
+ sandbox: config?.sandbox ?? false,
71
+ secret: config?.secret,
72
+ shard: config?.shard ?? [0, 1],
45
73
  token: config?.token,
46
- port: config?.port,
47
- ws: config?.ws
74
+ mode: config?.mode ?? 'group'
48
75
  });
49
76
  // 连接
50
- client.connect();
51
- /**
52
- * group
53
- *
54
- * GROUP_AT_MESSAGE_CREATE
55
- * C2C_MESSAGE_CREATE
56
- */
57
- const createUserAvatarURL = (author_id) => {
58
- return `https://q.qlogo.cn/qqapp/${config.app_id}/${author_id}/ 640`;
59
- };
60
- // 监听消息
61
- client.on('GROUP_AT_MESSAGE_CREATE', async (event) => {
62
- const master_key = config?.master_key ?? [];
63
- const isMaster = master_key.includes(event.author.id);
64
- const url = createUserAvatarURL(event.author.id);
65
- const UserAvatar = {
66
- toBuffer: async () => {
67
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
68
- return Buffer.from(arrayBuffer);
69
- },
70
- toBase64: async () => {
71
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
72
- return Buffer.from(arrayBuffer).toString('base64');
73
- },
74
- toURL: async () => {
75
- return url;
76
- }
77
- };
78
- const UserId = event.author.id;
79
- const UserKey = useUserHashKey({
80
- Platform: platform,
81
- UserId: UserId
82
- });
83
- // 定义消
84
- const e = {
85
- name: 'message.create',
86
- // 事件类型
87
- Platform: platform,
88
- // guild
89
- GuildId: event.group_id,
90
- ChannelId: event.group_id,
91
- // 用户Id
92
- UserId: event.author.id,
93
- UserKey,
94
- UserAvatar: UserAvatar,
95
- IsMaster: isMaster,
96
- IsBot: false,
97
- // 格式化数据
98
- MessageId: event.id,
99
- MessageText: event.content?.trim(),
100
- OpenId: event.author.member_openid,
101
- CreateAt: Date.now(),
102
- tag: 'GROUP_AT_MESSAGE_CREATE',
103
- value: null
104
- };
105
- // 处理消息
106
- onProcessor('message.create', e, event);
107
- });
108
- client.on('C2C_MESSAGE_CREATE', async (event) => {
109
- const master_key = config?.master_key ?? [];
110
- const isMaster = master_key.includes(event.author.id);
111
- const url = createUserAvatarURL(event.author.id);
112
- const UserAvatar = {
113
- toBuffer: async () => {
114
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
115
- return Buffer.from(arrayBuffer);
116
- },
117
- toBase64: async () => {
118
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
119
- return Buffer.from(arrayBuffer).toString('base64');
120
- },
121
- toURL: async () => {
122
- return url;
123
- }
124
- };
125
- const UserId = event.author.id;
126
- const UserKey = useUserHashKey({
127
- Platform: platform,
128
- UserId: UserId
129
- });
130
- // 定义消
131
- const e = {
132
- name: 'private.message.create',
133
- // 事件类型
134
- Platform: platform,
135
- // 用户Id
136
- UserId: event.author.id,
137
- UserKey,
138
- UserAvatar: UserAvatar,
139
- IsMaster: isMaster,
140
- IsBot: false,
141
- // 格式化数据
142
- MessageId: event.id,
143
- MessageText: event.content?.trim(),
144
- CreateAt: Date.now(),
145
- OpenId: event.author.user_openid,
146
- //
147
- tag: 'C2C_MESSAGE_CREATE',
148
- value: null
149
- };
150
- // 处理消息
151
- onProcessor('private.message.create', e, event);
152
- });
153
- /**
154
- * guild
155
- */
156
- client.on('DIRECT_MESSAGE_CREATE', async (event) => {
157
- // 屏蔽其他机器人的消息
158
- if (event?.author?.bot)
159
- return;
160
- const master_key = config?.master_key ?? [];
161
- const isMaster = master_key.includes(event.author.id);
162
- let msg = event?.content ?? '';
163
- const UserAvatar = {
164
- toBuffer: async () => {
165
- const arrayBuffer = await fetch(event.author.avatar).then(res => res.arrayBuffer());
166
- return Buffer.from(arrayBuffer);
167
- },
168
- toBase64: async () => {
169
- const arrayBuffer = await fetch(event?.author?.avatar).then(res => res.arrayBuffer());
170
- return Buffer.from(arrayBuffer).toString('base64');
171
- },
172
- toURL: async () => {
173
- return event?.author?.avatar;
174
- }
175
- };
176
- const UserId = event.author.id;
177
- const UserKey = useUserHashKey({
178
- Platform: platform,
179
- UserId: UserId
180
- });
181
- // 定义消
182
- const e = {
183
- name: 'private.message.create',
184
- // 事件类型
185
- Platform: platform,
186
- //
187
- // GuildId: event.guild_id,
188
- // ChannelId: event.channel_id,
189
- // 用户Id
190
- UserId: event?.author?.id ?? '',
191
- UserKey,
192
- UserName: event?.author?.username ?? '',
193
- UserAvatar: UserAvatar,
194
- IsMaster: isMaster,
195
- IsBot: event.author?.bot,
196
- // message
197
- MessageId: event.id,
198
- MessageText: msg?.trim(),
199
- OpenId: event.guild_id,
200
- CreateAt: Date.now(),
201
- //
202
- tag: 'DIRECT_MESSAGE_CREATE',
203
- //
204
- value: null
205
- };
206
- // 处理消息
207
- onProcessor('private.message.create', e, event);
208
- });
209
- // 监听消息
210
- client.on('AT_MESSAGE_CREATE', async (event) => {
211
- // 屏蔽其他机器人的消息
212
- if (event?.author?.bot)
213
- return;
214
- const master_key = config?.master_key ?? [];
215
- const isMaster = master_key.includes(event.author.id);
216
- let msg = getMessageContent(event);
217
- const UserAvatar = {
218
- toBuffer: async () => {
219
- const arrayBuffer = await fetch(event.author.avatar).then(res => res.arrayBuffer());
220
- return Buffer.from(arrayBuffer);
221
- },
222
- toBase64: async () => {
223
- const arrayBuffer = await fetch(event?.author?.avatar).then(res => res.arrayBuffer());
224
- return Buffer.from(arrayBuffer).toString('base64');
225
- },
226
- toURL: async () => {
227
- return event?.author?.avatar;
228
- }
229
- };
230
- const UserId = event.author.id;
231
- const UserKey = useUserHashKey({
232
- Platform: platform,
233
- UserId: UserId
234
- });
235
- // 定义消
236
- const e = {
237
- name: 'message.create',
238
- // 事件类型
239
- Platform: platform,
240
- GuildId: event.guild_id,
241
- ChannelId: event.channel_id,
242
- IsMaster: isMaster,
243
- // 用户Id
244
- UserId: event?.author?.id ?? '',
245
- UserKey,
246
- UserName: event?.author?.username ?? '',
247
- UserAvatar: UserAvatar,
248
- IsBot: event.author?.bot,
249
- // message
250
- MessageId: event.id,
251
- MessageText: msg?.trim(),
252
- OpenId: event.guild_id,
253
- CreateAt: Date.now(),
254
- //
255
- tag: 'AT_MESSAGE_CREATE',
256
- //
257
- value: null
258
- };
259
- // 处理消息
260
- onProcessor('message.create', e, event);
261
- });
262
- /**
263
- *
264
- * @param event
265
- * @returns
266
- */
267
- const getMessageContent = event => {
268
- let msg = event?.content ?? '';
269
- // 艾特消息处理
270
- const at_users = [];
271
- if (event.mentions) {
272
- // 去掉@ 转为纯消息
273
- for (const item of event.mentions) {
274
- at_users.push({
275
- id: item.id
276
- });
277
- }
278
- // 循环删除文本中的at信息并去除前后空格
279
- at_users.forEach(item => {
280
- msg = msg.replace(`<@!${item.id}>`, '').trim();
281
- });
282
- }
283
- return msg;
284
- };
285
- // 私域 -
286
- client.on('MESSAGE_CREATE', async (event) => {
287
- // 屏蔽其他机器人的消息
288
- if (event.author?.bot)
289
- return;
290
- // 撤回消息
291
- if (new RegExp(/DELETE$/).test(event.eventType))
292
- return;
293
- const master_key = config?.master_key ?? [];
294
- const UserId = event.author.id;
295
- const isMaster = master_key.includes(UserId);
296
- const msg = getMessageContent(event);
297
- const UserAvatar = {
298
- toBuffer: async () => {
299
- const arrayBuffer = await fetch(event.author.avatar).then(res => res.arrayBuffer());
300
- return Buffer.from(arrayBuffer);
301
- },
302
- toBase64: async () => {
303
- const arrayBuffer = await fetch(event?.author?.avatar).then(res => res.arrayBuffer());
304
- return Buffer.from(arrayBuffer).toString('base64');
305
- },
306
- toURL: async () => {
307
- return event?.author?.avatar;
308
- }
309
- };
310
- const UserKey = useUserHashKey({
311
- Platform: platform,
312
- UserId: UserId
313
- });
314
- // 定义消
315
- const e = {
316
- name: 'message.create',
317
- // 事件类型
318
- Platform: platform,
319
- //
320
- GuildId: event.guild_id,
321
- ChannelId: event.channel_id,
322
- UserId: event?.author?.id ?? '',
323
- UserKey,
324
- UserName: event?.author?.username ?? '',
325
- UserAvatar: UserAvatar,
326
- IsMaster: isMaster,
327
- IsBot: false,
328
- // message
329
- MessageId: event.id,
330
- MessageText: msg?.trim(),
331
- OpenId: event.guild_id,
332
- CreateAt: Date.now(),
333
- //
334
- tag: 'MESSAGE_CREATE',
335
- value: null
336
- };
337
- // 处理消息
338
- onProcessor('message.create', e, event);
339
- });
340
- client.on('ERROR', console.error);
77
+ client.connect(config?.gatewayURL);
78
+ register(client);
341
79
  // FRIEND_ADD
342
80
  global.client = client;
343
- return {
344
- platform,
345
- api: {
346
- // 主动消息
347
- active: {
348
- send: {
349
- channel: async (channel_id, data) => {
350
- if (isGuild(channel_id)) {
351
- return await AT_MESSAGE_CREATE(client, {
352
- ChannelId: channel_id
353
- }, data);
354
- }
355
- else {
356
- // 需要message_id 。如果没有,则是主动消息,在group中,只能发送4条。
357
- return await GROUP_AT_MESSAGE_CREATE(client, {
358
- GuildId: channel_id
359
- }, data);
360
- }
361
- },
362
- user: async (user_id, data) => {
363
- if (isGuild(user_id)) {
364
- return await DIRECT_MESSAGE_CREATE(client, {
365
- OpenId: user_id
366
- }, data);
367
- }
368
- else {
369
- return await C2C_MESSAGE_CREATE(client, {
370
- OpenId: user_id
371
- }, data);
372
- }
373
- }
374
- }
375
- },
376
- // 被动消息
377
- use: {
378
- send: async (event, val) => {
379
- if (val.length < 0)
380
- ;
381
- // 打 tag
382
- const tag = event.tag;
383
- try {
384
- // 群at
385
- if (tag == 'GROUP_AT_MESSAGE_CREATE') {
386
- return await GROUP_AT_MESSAGE_CREATE(client, event, val);
387
- }
388
- // 私聊
389
- if (tag == 'C2C_MESSAGE_CREATE') {
390
- return await C2C_MESSAGE_CREATE(client, event, val);
391
- }
392
- // 频道私聊
393
- if (tag == 'DIRECT_MESSAGE_CREATE') {
394
- return await DIRECT_MESSAGE_CREATE(client, event, val);
395
- }
396
- // 频道at
397
- if (tag == 'AT_MESSAGE_CREATE') {
398
- return await AT_MESSAGE_CREATE(client, event, val);
399
- }
400
- // 频道消息
401
- if (tag == 'MESSAGE_CREATE') {
402
- return await MESSAGE_CREATE(client, event, val);
403
- }
404
- }
405
- catch (error) {
406
- return [error];
407
- }
408
- return [];
409
- },
410
- mention: async (e) => {
411
- const event = e.value;
412
- const tag = e.tag;
413
- // const event = e.value
414
- const Metions = [];
415
- // group
416
- if (tag == 'GROUP_AT_MESSAGE_CREATE' || 'C2C_MESSAGE_CREATE')
417
- return Metions;
418
- // guild
419
- if (event.mentions) {
420
- const mentions = e.value['mentions'];
421
- // 艾特消息处理
422
- const MessageMention = mentions.map(item => {
423
- return {
424
- UserId: item.id,
425
- IsMaster: false,
426
- UserName: item.username,
427
- IsBot: item.bot,
428
- UserKey: useUserHashKey({
429
- Platform: 'qq-guild-bot',
430
- UserId: item.id
431
- })
432
- };
433
- }) ?? [];
434
- return MessageMention;
435
- }
436
- else {
437
- return Metions;
438
- }
439
- }
440
- }
441
- }
442
- };
81
+ return createClientAPI(client);
443
82
  });
444
83
 
445
84
  export { client, index as default, platform };
@@ -0,0 +1,26 @@
1
+ import { getConfigValue } from 'alemonjs';
2
+ import { QQBotClient } from './sdk/client.js';
3
+ import { platform, register, createClientAPI } from './register.js';
4
+
5
+ var QQBotHook = definePlatform(() => {
6
+ let value = getConfigValue();
7
+ if (!value)
8
+ value = {};
9
+ const config = value[platform];
10
+ const client = new QQBotClient({
11
+ secret: config?.secret,
12
+ app_id: config?.app_id,
13
+ route: config?.route,
14
+ token: config?.token,
15
+ port: config?.port,
16
+ ws: config?.ws
17
+ });
18
+ // 连接
19
+ client.connect();
20
+ register(client);
21
+ // FRIEND_ADD
22
+ global.client = client;
23
+ return createClientAPI(client);
24
+ });
25
+
26
+ export { QQBotHook as default };
@@ -0,0 +1,3 @@
1
+ declare const platform = "qq-bot";
2
+
3
+ export { platform };