@alemonjs/qq-bot 0.0.18 → 0.0.20

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