@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.
Files changed (113) hide show
  1. package/README.md +15 -45
  2. package/lib/config.d.ts +9 -0
  3. package/lib/config.js +20 -0
  4. package/lib/desktop.d.ts +1 -3
  5. package/lib/desktop.js +3 -12
  6. package/lib/format.d.ts +5 -0
  7. package/lib/format.js +94 -0
  8. package/lib/hook.d.ts +36 -9
  9. package/lib/hook.js +21 -19
  10. package/lib/index.d.ts +5 -7
  11. package/lib/index.js +11 -21
  12. package/lib/index.webhook.d.ts +1 -0
  13. package/lib/index.webhook.js +5 -4
  14. package/lib/index.websoket.d.ts +1 -0
  15. package/lib/index.websoket.js +31 -23
  16. package/lib/message/AT_MESSAGE_CREATE.d.ts +35 -0
  17. package/lib/message/AT_MESSAGE_CREATE.js +1 -0
  18. package/lib/message/CHANNEL_CREATE.d.ts +15 -0
  19. package/lib/message/CHANNEL_CREATE.js +1 -0
  20. package/lib/message/CHANNEL_DELETE.d.ts +15 -0
  21. package/lib/message/CHANNEL_DELETE.js +1 -0
  22. package/lib/message/CHANNEL_UPDATE.d.ts +15 -0
  23. package/lib/message/CHANNEL_UPDATE.js +1 -0
  24. package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +29 -0
  25. package/lib/message/DIRECT_MESSAGE_CREATE.js +1 -0
  26. package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +17 -0
  27. package/lib/message/DIRECT_MESSAGE_DELETE.js +1 -0
  28. package/lib/message/ERROR.d.ts +1 -0
  29. package/lib/message/ERROR.js +1 -0
  30. package/lib/message/GUILD_CREATE.d.ts +15 -0
  31. package/lib/message/GUILD_CREATE.js +1 -0
  32. package/lib/message/GUILD_DELETE.d.ts +15 -0
  33. package/lib/message/GUILD_DELETE.js +1 -0
  34. package/lib/message/GUILD_MEMBER_ADD.d.ts +14 -0
  35. package/lib/message/GUILD_MEMBER_ADD.js +1 -0
  36. package/lib/message/GUILD_MEMBER_REMOVE.d.ts +14 -0
  37. package/lib/message/GUILD_MEMBER_REMOVE.js +1 -0
  38. package/lib/message/GUILD_MEMBER_UPDATE.d.ts +14 -0
  39. package/lib/message/GUILD_MEMBER_UPDATE.js +1 -0
  40. package/lib/message/GUILD_UPDATE.d.ts +15 -0
  41. package/lib/message/GUILD_UPDATE.js +1 -0
  42. package/lib/message/INTERACTION_CREATE.d.ts +53 -0
  43. package/lib/message/INTERACTION_CREATE.js +1 -0
  44. package/lib/message/MESSAGE_CREATE.d.ts +1 -0
  45. package/lib/message/MESSAGE_CREATE.js +1 -0
  46. package/lib/message/MESSAGE_DELETE.d.ts +1 -0
  47. package/lib/message/MESSAGE_DELETE.js +1 -0
  48. package/lib/message/MESSAGE_REACTION_ADD.d.ts +13 -0
  49. package/lib/message/MESSAGE_REACTION_ADD.js +1 -0
  50. package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +13 -0
  51. package/lib/message/MESSAGE_REACTION_REMOVE.js +1 -0
  52. package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +15 -0
  53. package/lib/message/PUBLIC_MESSAGE_DELETE.js +1 -0
  54. package/lib/message/READY.d.ts +6 -0
  55. package/lib/message/READY.js +1 -0
  56. package/lib/message/group/C2C_MESSAGE_CREATE.d.ts +9 -0
  57. package/lib/message/group/C2C_MESSAGE_CREATE.js +1 -0
  58. package/lib/message/group/C2C_MSG_RECEIVE.d.ts +4 -0
  59. package/lib/message/group/C2C_MSG_RECEIVE.js +1 -0
  60. package/lib/message/group/C2C_MSG_REJECT.d.ts +4 -0
  61. package/lib/message/group/C2C_MSG_REJECT.js +1 -0
  62. package/lib/message/group/FRIEND_ADD.d.ts +4 -0
  63. package/lib/message/group/FRIEND_ADD.js +1 -0
  64. package/lib/message/group/FRIEND_DEL.d.ts +4 -0
  65. package/lib/message/group/FRIEND_DEL.js +1 -0
  66. package/lib/message/group/GROUP_ADD_ROBOT.d.ts +5 -0
  67. package/lib/message/group/GROUP_ADD_ROBOT.js +1 -0
  68. package/lib/message/group/GROUP_AT_MESSAGE_CREATE.d.ts +11 -0
  69. package/lib/message/group/GROUP_AT_MESSAGE_CREATE.js +1 -0
  70. package/lib/message/group/GROUP_DEL_ROBOT.d.ts +5 -0
  71. package/lib/message/group/GROUP_DEL_ROBOT.js +1 -0
  72. package/lib/message/group/GROUP_MSG_RECEIVE.d.ts +5 -0
  73. package/lib/message/group/GROUP_MSG_RECEIVE.js +1 -0
  74. package/lib/message/group/GROUP_MSG_REJECT.d.ts +5 -0
  75. package/lib/message/group/GROUP_MSG_REJECT.js +1 -0
  76. package/lib/register.d.ts +2 -3
  77. package/lib/register.js +426 -252
  78. package/lib/sdk/api.d.ts +10 -567
  79. package/lib/sdk/api.js +224 -790
  80. package/lib/sdk/client.webhook.d.ts +9 -0
  81. package/lib/sdk/{client.js → client.webhook.js} +18 -55
  82. package/lib/sdk/client.websoket.d.ts +9 -0
  83. package/lib/sdk/client.websoket.js +52 -97
  84. package/lib/sdk/config.d.ts +1 -0
  85. package/lib/sdk/instance.d.ts +3 -0
  86. package/lib/sdk/instance.js +93 -0
  87. package/lib/sdk/intents.d.ts +3 -0
  88. package/lib/sdk/intents.js +25 -90
  89. package/lib/sdk/message.d.ts +4 -0
  90. package/lib/sdk/message.group.d.ts +24 -0
  91. package/lib/sdk/message.group.js +1 -0
  92. package/lib/sdk/message.guild.d.ts +38 -0
  93. package/lib/sdk/message.guild.js +1 -0
  94. package/lib/sdk/message.js +1 -0
  95. package/lib/sdk/message.public.d.ts +6 -0
  96. package/lib/sdk/message.public.js +1 -0
  97. package/lib/sdk/typing.d.ts +22 -11
  98. package/lib/sdk/typing.js +1 -0
  99. package/lib/sdk/webhook.secret.d.ts +14 -0
  100. package/lib/sdk/webhook.secret.js +6 -21
  101. package/lib/sends.d.ts +29 -0
  102. package/lib/sends.js +384 -745
  103. package/lib/types.d.ts +48 -0
  104. package/lib/types.js +1 -0
  105. package/lib/utils.d.ts +1 -0
  106. package/lib/utils.js +7 -12
  107. package/package.json +9 -5
  108. package/lib/index.group.js +0 -19
  109. package/lib/index.guild.js +0 -36
  110. package/lib/sdk/client.websoket.group.js +0 -221
  111. package/lib/sdk/client.websoket.guild.js +0 -205
  112. package/lib/sdk/counter.js +0 -19
  113. package/lib/sdk/from.js +0 -44
package/lib/register.js CHANGED
@@ -1,345 +1,272 @@
1
- import { getConfigValue, cbpPlatform, useUserHashKey, createResult, ResultCode } from 'alemonjs';
1
+ import { cbpPlatform, 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
4
 
4
- // import dayjs from 'dayjs'
5
- const platform = 'qq-bot';
6
- const getQQBotConfig = () => {
7
- let value = getConfigValue();
8
- if (!value)
9
- value = {};
10
- return value[platform] || {};
11
- };
12
5
  const register = (client) => {
13
6
  const config = getQQBotConfig();
14
- /**
15
- * 连接 alemonjs 服务器。
16
- * 向 alemonjs 推送标准信息
17
- */
7
+ const botId = String(config?.app_id ?? '');
18
8
  const port = process.env?.port || config?.port || 17117;
19
9
  const url = `ws://127.0.0.1:${port}`;
20
10
  const cbp = cbpPlatform(url);
21
- /**
22
- * group
23
- *
24
- * GROUP_AT_MESSAGE_CREATE
25
- * C2C_MESSAGE_CREATE
26
- */
27
- const createUserAvatarURL = (author_id) => {
28
- return `https://q.qlogo.cn/qqapp/${config.app_id}/${author_id}/640`;
11
+ const createUserAvatarURL = (authorId) => {
12
+ return `https://q.qlogo.cn/qqapp/${config.app_id}/${authorId}/640`;
29
13
  };
30
- // 监听消息
31
- client.on('GROUP_AT_MESSAGE_CREATE', async (event) => {
32
- const master_key = config?.master_key ?? [];
33
- const isMaster = master_key.includes(event.author.id);
34
- const UserAvatar = createUserAvatarURL(event.author.id);
35
- const UserId = event.author.id;
36
- const UserKey = useUserHashKey({
14
+ client.on('GROUP_ADD_ROBOT', event => {
15
+ const e = {
16
+ name: 'guild.join',
37
17
  Platform: platform,
38
- UserId: UserId
39
- });
40
- // 定义消
18
+ GuildId: event.group_openid,
19
+ ChannelId: event.group_openid,
20
+ SpaceId: `GROUP:${event.group_openid}`,
21
+ UserId: event.op_member_openid,
22
+ UserKey: event.op_member_openid,
23
+ UserAvatar: createUserAvatarURL(event.op_member_openid),
24
+ IsMaster: false,
25
+ IsBot: false,
26
+ OpenId: `C2C:${event.op_member_openid}`,
27
+ MessageId: '',
28
+ BotId: botId,
29
+ _tag: 'GROUP_ADD_ROBOT',
30
+ value: event
31
+ };
32
+ cbp.send(e);
33
+ });
34
+ client.on('GROUP_DEL_ROBOT', event => {
35
+ const e = {
36
+ name: 'guild.exit',
37
+ Platform: platform,
38
+ GuildId: event.group_openid,
39
+ ChannelId: event.group_openid,
40
+ SpaceId: `GROUP:${event.group_openid}`,
41
+ UserId: event.op_member_openid,
42
+ UserKey: event.op_member_openid,
43
+ UserAvatar: createUserAvatarURL(event.op_member_openid),
44
+ IsMaster: false,
45
+ IsBot: false,
46
+ OpenId: `C2C:${event.op_member_openid}`,
47
+ MessageId: '',
48
+ BotId: botId,
49
+ _tag: 'GROUP_DEL_ROBOT',
50
+ value: event
51
+ };
52
+ cbp.send(e);
53
+ });
54
+ client.on('GROUP_AT_MESSAGE_CREATE', event => {
55
+ const UserId = event.author.id;
56
+ const [isMaster, UserKey] = getMaster(UserId);
57
+ const UserAvatar = createUserAvatarURL(event.author.id);
41
58
  const e = {
42
59
  name: 'message.create',
43
60
  Platform: platform,
44
- // guild
45
61
  GuildId: event.group_id,
46
62
  ChannelId: event.group_id,
47
- // 用户Id
63
+ SpaceId: `GROUP:${event.group_id}`,
48
64
  UserId: event.author.id,
49
65
  UserKey,
50
66
  UserAvatar: UserAvatar,
51
67
  IsMaster: isMaster,
52
68
  IsBot: false,
53
- // 格式化数据
54
69
  MessageId: event.id,
55
70
  MessageText: event.content?.trim(),
56
- OpenId: event.author.member_openid,
57
- CreateAt: Date.now(),
58
- tag: 'GROUP_AT_MESSAGE_CREATE',
71
+ OpenId: `C2C:${event.author.member_openid}`,
72
+ BotId: botId,
73
+ _tag: 'GROUP_AT_MESSAGE_CREATE',
59
74
  value: event
60
75
  };
61
76
  cbp.send(e);
62
77
  });
63
- client.on('C2C_MESSAGE_CREATE', async (event) => {
64
- const master_key = config?.master_key ?? [];
65
- const isMaster = master_key.includes(event.author.id);
66
- const UserAvatar = createUserAvatarURL(event.author.id);
78
+ client.on('C2C_MESSAGE_CREATE', event => {
67
79
  const UserId = event.author.id;
68
- const UserKey = useUserHashKey({
69
- Platform: platform,
70
- UserId: UserId
71
- });
72
- // 定义消
80
+ const [isMaster, UserKey] = getMaster(UserId);
81
+ const UserAvatar = createUserAvatarURL(event.author.id);
73
82
  const e = {
74
83
  name: 'private.message.create',
75
- // 事件类型
76
84
  Platform: platform,
77
- // 用户Id
78
85
  UserId: event.author.id,
79
86
  UserKey,
80
87
  UserAvatar: UserAvatar,
81
88
  IsMaster: isMaster,
82
89
  IsBot: false,
83
- // 格式化数据
84
90
  MessageId: event.id,
85
91
  MessageText: event.content?.trim(),
86
- CreateAt: Date.now(),
87
- OpenId: event.author.user_openid,
88
- //
89
- tag: 'C2C_MESSAGE_CREATE',
92
+ OpenId: `C2C:${event.author.user_openid}`,
93
+ BotId: botId,
94
+ _tag: 'C2C_MESSAGE_CREATE',
90
95
  value: event
91
96
  };
92
97
  cbp.send(e);
93
98
  });
94
- /**
95
- * guild
96
- */
97
- client.on('DIRECT_MESSAGE_CREATE', async (event) => {
98
- // 屏蔽其他机器人的消息
99
- if (event?.author?.bot)
99
+ client.on('DIRECT_MESSAGE_CREATE', event => {
100
+ if (event?.author?.bot) {
100
101
  return;
101
- const master_key = config?.master_key ?? [];
102
- const isMaster = master_key.includes(event.author.id);
103
- let msg = event?.content ?? '';
102
+ }
103
+ const msg = event?.content ?? '';
104
104
  const UserAvatar = event?.author?.avatar;
105
105
  const UserId = event.author.id;
106
- const UserKey = useUserHashKey({
107
- Platform: platform,
108
- UserId: UserId
109
- });
110
- // 定义消
106
+ const [isMaster, UserKey] = getMaster(UserId);
111
107
  const e = {
112
108
  name: 'private.message.create',
113
- // 事件类型
114
109
  Platform: platform,
115
- //
116
- // GuildId: event.guild_id,
117
- // ChannelId: event.channel_id,
118
- // 用户Id
119
110
  UserId: event?.author?.id ?? '',
120
111
  UserKey,
121
112
  UserName: event?.author?.username ?? '',
122
113
  UserAvatar: UserAvatar,
123
114
  IsMaster: isMaster,
124
115
  IsBot: event.author?.bot,
125
- // message
126
116
  MessageId: event.id,
127
117
  MessageText: msg?.trim(),
128
- OpenId: event.guild_id,
129
- CreateAt: Date.now(),
130
- //
131
- tag: 'DIRECT_MESSAGE_CREATE',
118
+ OpenId: `DIRECT:${event.guild_id}`,
119
+ BotId: botId,
120
+ _tag: 'DIRECT_MESSAGE_CREATE',
132
121
  value: event
133
122
  };
134
123
  cbp.send(e);
135
124
  });
136
- // 监听消息
137
- client.on('AT_MESSAGE_CREATE', async (event) => {
138
- // 屏蔽其他机器人的消息
139
- if (event?.author?.bot)
125
+ client.on('AT_MESSAGE_CREATE', event => {
126
+ if (event?.author?.bot) {
140
127
  return;
141
- const master_key = config?.master_key ?? [];
142
- const isMaster = master_key.includes(event.author.id);
143
- let msg = getMessageContent(event);
128
+ }
129
+ const msg = getMessageContent(event);
144
130
  const UserAvatar = event?.author?.avatar;
145
131
  const UserId = event.author.id;
146
- const UserKey = useUserHashKey({
147
- Platform: platform,
148
- UserId: UserId
149
- });
150
- // 定义消
132
+ const [isMaster, UserKey] = getMaster(UserId);
151
133
  const e = {
152
134
  name: 'message.create',
153
- // 事件类型
154
135
  Platform: platform,
155
136
  GuildId: event.guild_id,
156
137
  ChannelId: event.channel_id,
138
+ SpaceId: `GUILD:${event.channel_id}`,
157
139
  IsMaster: isMaster,
158
- // 用户Id
159
140
  UserId: event?.author?.id ?? '',
160
141
  UserKey,
161
142
  UserName: event?.author?.username ?? '',
162
143
  UserAvatar: UserAvatar,
163
144
  IsBot: event.author?.bot,
164
- // message
165
145
  MessageId: event.id,
166
146
  MessageText: msg?.trim(),
167
- OpenId: event.guild_id,
168
- CreateAt: Date.now(),
169
- //
170
- tag: 'AT_MESSAGE_CREATE',
147
+ OpenId: `DIRECT:${event.guild_id}`,
148
+ BotId: botId,
149
+ _tag: 'AT_MESSAGE_CREATE',
171
150
  value: event
172
151
  };
173
152
  cbp.send(e);
174
153
  });
175
- /**
176
- *
177
- * @param event
178
- * @returns
179
- */
180
154
  const getMessageContent = event => {
181
155
  let msg = event?.content ?? '';
182
- // 艾特消息处理
183
- const at_users = [];
156
+ const atUsers = [];
184
157
  if (event.mentions) {
185
- // 去掉@ 转为纯消息
186
158
  for (const item of event.mentions) {
187
- at_users.push({
159
+ atUsers.push({
188
160
  id: item.id
189
161
  });
190
162
  }
191
- // 循环删除文本中的at信息并去除前后空格
192
- at_users.forEach(item => {
163
+ atUsers.forEach(item => {
193
164
  msg = msg.replace(`<@!${item.id}>`, '').trim();
194
165
  });
195
166
  }
196
167
  return msg;
197
168
  };
198
- // 私域 -
199
- client.on('MESSAGE_CREATE', async (event) => {
200
- // 屏蔽其他机器人的消息
201
- if (event.author?.bot)
169
+ client.on('MESSAGE_CREATE', event => {
170
+ if (event.author?.bot) {
202
171
  return;
203
- // 撤回消息
204
- if (new RegExp(/DELETE$/).test(event.eventType))
172
+ }
173
+ if (new RegExp(/DELETE$/).test(event.eventType)) {
205
174
  return;
206
- const master_key = config?.master_key ?? [];
175
+ }
207
176
  const UserId = event.author.id;
208
- const isMaster = master_key.includes(UserId);
209
177
  const msg = getMessageContent(event);
210
178
  const UserAvatar = event?.author?.avatar;
211
- const UserKey = useUserHashKey({
212
- Platform: platform,
213
- UserId: UserId
214
- });
215
- // 定义消
179
+ const [isMaster, UserKey] = getMaster(UserId);
216
180
  const e = {
217
181
  name: 'message.create',
218
- // 事件类型
219
182
  Platform: platform,
220
- //
221
183
  GuildId: event.guild_id,
222
184
  ChannelId: event.channel_id,
185
+ SpaceId: `GUILD:${event.channel_id}`,
223
186
  UserId: event?.author?.id ?? '',
224
187
  UserKey,
225
188
  UserName: event?.author?.username ?? '',
226
189
  UserAvatar: UserAvatar,
227
190
  IsMaster: isMaster,
228
191
  IsBot: false,
229
- // message
230
192
  MessageId: event.id,
231
193
  MessageText: msg?.trim(),
232
- OpenId: event.guild_id,
233
- CreateAt: Date.now(),
234
- //
235
- tag: 'MESSAGE_CREATE',
194
+ OpenId: `DIRECT:${event.guild_id}`,
195
+ BotId: botId,
196
+ _tag: 'MESSAGE_CREATE',
236
197
  value: event
237
198
  };
238
199
  cbp.send(e);
239
200
  });
240
- client.on('INTERACTION_CREATE', async (event) => {
241
- try {
242
- if (event.scene === 'group' || event.scene === 'c2c') {
243
- await client.interactionResponse('group', event.id);
244
- }
245
- else if (event.scene === 'guild') {
246
- await client.interactionResponse('guild', event.id);
247
- }
248
- }
249
- catch (err) {
250
- createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null);
251
- }
201
+ client.on('INTERACTION_CREATE', event => {
252
202
  if (event.scene === 'group') {
253
- const master_key = config?.master_key ?? [];
254
- const isMaster = master_key.includes(event.group_member_openid);
255
203
  const UserAvatar = createUserAvatarURL(event.group_member_openid);
256
204
  const UserId = event.group_member_openid;
257
- const UserKey = useUserHashKey({
258
- Platform: platform,
259
- UserId: UserId
260
- });
205
+ const [isMaster, UserKey] = getMaster(UserId);
261
206
  const MessageText = event.data.resolved.button_data?.trim() || '';
262
207
  const e = {
263
208
  name: 'interaction.create',
264
209
  Platform: platform,
265
- // guild
266
210
  GuildId: event.group_openid,
267
211
  ChannelId: event.group_openid,
268
- // 用户Id
212
+ SpaceId: `GROUP:${event.group_openid}`,
269
213
  UserId: event.group_member_openid,
270
214
  UserKey,
271
215
  UserAvatar: UserAvatar,
272
216
  IsMaster: isMaster,
273
217
  IsBot: false,
274
- // 格式化数据
275
218
  MessageId: `INTERACTION_CREATE:${event.id}`,
276
219
  MessageText: MessageText,
277
- OpenId: event.group_member_openid,
278
- tag: 'INTERACTION_CREATE_GROUP',
279
- CreateAt: Date.now(),
220
+ OpenId: `C2C:${event.group_member_openid}`,
221
+ BotId: botId,
222
+ _tag: 'INTERACTION_CREATE_GROUP',
280
223
  value: event
281
224
  };
282
225
  cbp.send(e);
283
226
  }
284
227
  else if (event.scene === 'c2c') {
285
- const master_key = config?.master_key ?? [];
286
- const isMaster = master_key.includes(event.user_openid);
287
228
  const UserAvatar = createUserAvatarURL(event.user_openid);
288
229
  const UserId = event.user_openid;
289
- const UserKey = useUserHashKey({
290
- Platform: platform,
291
- UserId: UserId
292
- });
230
+ const [isMaster, UserKey] = getMaster(UserId);
293
231
  const MessageText = event.data.resolved.button_data?.trim() || '';
294
- // 处理消息
295
232
  const e = {
296
233
  name: 'private.interaction.create',
297
234
  Platform: platform,
298
- // 用户Id
299
235
  UserId: event.user_openid,
300
236
  UserKey,
301
237
  UserAvatar: UserAvatar,
302
238
  IsMaster: isMaster,
303
239
  IsBot: false,
304
- // 格式化数据
305
240
  MessageId: event.id,
306
241
  MessageText: MessageText,
307
- OpenId: event.user_openid,
308
- CreateAt: Date.now(),
309
- tag: 'INTERACTION_CREATE_C2C',
242
+ OpenId: `C2C:${event.user_openid}`,
243
+ BotId: botId,
244
+ _tag: 'INTERACTION_CREATE_C2C',
310
245
  value: event
311
246
  };
312
247
  cbp.send(e);
313
248
  }
314
249
  else if (event.scene === 'guild') {
315
- const master_key = config?.master_key ?? [];
316
- const isMaster = master_key.includes(event.data.resolved.user_id);
317
250
  const UserAvatar = createUserAvatarURL(event.data.resolved.user_id);
318
251
  const UserId = event.data.resolved.user_id;
319
- const UserKey = useUserHashKey({
320
- Platform: platform,
321
- UserId: UserId
322
- });
252
+ const [isMaster, UserKey] = getMaster(UserId);
323
253
  const MessageText = event.data.resolved.button_data?.trim() || '';
324
- // 处理消息
325
254
  const e = {
326
255
  name: 'interaction.create',
327
256
  Platform: platform,
328
- // guild
329
257
  GuildId: event.guild_id,
330
258
  ChannelId: event.channel_id,
331
- // 用户Id
259
+ SpaceId: `GUILD:${event.channel_id}`,
332
260
  UserId: event.data.resolved.user_id,
333
261
  UserKey,
334
262
  UserAvatar: UserAvatar,
335
263
  IsMaster: isMaster,
336
264
  IsBot: false,
337
- // 格式化数据
338
265
  MessageId: event.data.resolved.message_id,
339
266
  MessageText: MessageText,
340
- OpenId: event.guild_id,
341
- CreateAt: Date.now(),
342
- tag: 'INTERACTION_CREATE_GUILD',
267
+ OpenId: `DIRECT:${event.guild_id}`,
268
+ BotId: botId,
269
+ _tag: 'INTERACTION_CREATE_GUILD',
343
270
  value: event
344
271
  };
345
272
  cbp.send(e);
@@ -352,119 +279,366 @@ const register = (client) => {
352
279
  });
353
280
  }
354
281
  });
282
+ client.on('MESSAGE_DELETE', event => {
283
+ const msg = event?.message ?? event;
284
+ const e = {
285
+ name: 'message.delete',
286
+ Platform: platform,
287
+ GuildId: msg?.guild_id ?? '',
288
+ ChannelId: msg?.channel_id ?? '',
289
+ SpaceId: `GUILD:${msg?.channel_id ?? ''}`,
290
+ MessageId: msg?.id ?? '',
291
+ BotId: botId,
292
+ _tag: 'MESSAGE_DELETE',
293
+ value: event
294
+ };
295
+ cbp.send(e);
296
+ });
297
+ client.on('PUBLIC_MESSAGE_DELETE', event => {
298
+ const msg = event.message;
299
+ const e = {
300
+ name: 'message.delete',
301
+ Platform: platform,
302
+ GuildId: msg.guild_id ?? '',
303
+ ChannelId: msg.channel_id ?? '',
304
+ SpaceId: `GUILD:${msg.channel_id ?? ''}`,
305
+ MessageId: msg.id ?? '',
306
+ BotId: botId,
307
+ _tag: 'PUBLIC_MESSAGE_DELETE',
308
+ value: event
309
+ };
310
+ cbp.send(e);
311
+ });
312
+ client.on('DIRECT_MESSAGE_DELETE', event => {
313
+ const msg = event.message;
314
+ const e = {
315
+ name: 'private.message.delete',
316
+ Platform: platform,
317
+ MessageId: msg.id ?? '',
318
+ BotId: botId,
319
+ _tag: 'DIRECT_MESSAGE_DELETE',
320
+ value: event
321
+ };
322
+ cbp.send(e);
323
+ });
324
+ client.on('MESSAGE_REACTION_ADD', event => {
325
+ const e = {
326
+ name: 'message.reaction.add',
327
+ Platform: platform,
328
+ GuildId: event.guild_id ?? '',
329
+ ChannelId: event.channel_id ?? '',
330
+ SpaceId: `GUILD:${event.channel_id ?? ''}`,
331
+ MessageId: event.target?.id ?? '',
332
+ BotId: botId,
333
+ _tag: 'MESSAGE_REACTION_ADD',
334
+ value: event
335
+ };
336
+ cbp.send(e);
337
+ });
338
+ client.on('MESSAGE_REACTION_REMOVE', event => {
339
+ const e = {
340
+ name: 'message.reaction.remove',
341
+ Platform: platform,
342
+ GuildId: event.guild_id ?? '',
343
+ ChannelId: event.channel_id ?? '',
344
+ SpaceId: `GUILD:${event.channel_id ?? ''}`,
345
+ MessageId: event.target?.id ?? '',
346
+ BotId: botId,
347
+ _tag: 'MESSAGE_REACTION_REMOVE',
348
+ value: event
349
+ };
350
+ cbp.send(e);
351
+ });
352
+ client.on('CHANNEL_CREATE', event => {
353
+ const e = {
354
+ name: 'channel.create',
355
+ Platform: platform,
356
+ GuildId: event.guild_id ?? '',
357
+ ChannelId: event.id ?? '',
358
+ SpaceId: `GUILD:${event.guild_id ?? ''}`,
359
+ MessageId: '',
360
+ BotId: botId,
361
+ _tag: 'CHANNEL_CREATE',
362
+ value: event
363
+ };
364
+ cbp.send(e);
365
+ });
366
+ client.on('CHANNEL_DELETE', event => {
367
+ const e = {
368
+ name: 'channel.delete',
369
+ Platform: platform,
370
+ GuildId: event.guild_id ?? '',
371
+ ChannelId: event.id ?? '',
372
+ SpaceId: `GUILD:${event.guild_id ?? ''}`,
373
+ MessageId: '',
374
+ BotId: botId,
375
+ _tag: 'CHANNEL_DELETE',
376
+ value: event
377
+ };
378
+ cbp.send(e);
379
+ });
380
+ client.on('GUILD_CREATE', event => {
381
+ const e = {
382
+ name: 'guild.join',
383
+ Platform: platform,
384
+ GuildId: event.id ?? '',
385
+ ChannelId: '',
386
+ SpaceId: `GUILD:${event.id ?? ''}`,
387
+ UserId: event.op_user_id ?? '',
388
+ UserKey: '',
389
+ IsMaster: false,
390
+ IsBot: false,
391
+ MessageId: '',
392
+ BotId: botId,
393
+ _tag: 'GUILD_CREATE',
394
+ value: event
395
+ };
396
+ cbp.send(e);
397
+ });
398
+ client.on('GUILD_DELETE', event => {
399
+ const e = {
400
+ name: 'guild.exit',
401
+ Platform: platform,
402
+ GuildId: event.id ?? '',
403
+ ChannelId: '',
404
+ SpaceId: `GUILD:${event.id ?? ''}`,
405
+ UserId: event.op_user_id ?? '',
406
+ UserKey: '',
407
+ IsMaster: false,
408
+ IsBot: false,
409
+ MessageId: '',
410
+ BotId: botId,
411
+ _tag: 'GUILD_DELETE',
412
+ value: event
413
+ };
414
+ cbp.send(e);
415
+ });
416
+ client.on('GUILD_MEMBER_ADD', event => {
417
+ const UserId = event.user?.id ?? '';
418
+ const [isMaster, UserKey] = getMaster(UserId);
419
+ const e = {
420
+ name: 'member.add',
421
+ Platform: platform,
422
+ GuildId: event.guild_id ?? '',
423
+ ChannelId: '',
424
+ SpaceId: `GUILD:${event.guild_id ?? ''}`,
425
+ UserId: UserId,
426
+ UserKey,
427
+ UserName: event.user?.username ?? '',
428
+ UserAvatar: createUserAvatarURL(UserId),
429
+ IsMaster: isMaster,
430
+ IsBot: false,
431
+ MessageId: '',
432
+ BotId: botId,
433
+ _tag: 'GUILD_MEMBER_ADD',
434
+ value: event
435
+ };
436
+ cbp.send(e);
437
+ });
438
+ client.on('GUILD_MEMBER_REMOVE', event => {
439
+ const UserId = event.user?.id ?? '';
440
+ const [isMaster, UserKey] = getMaster(UserId);
441
+ const e = {
442
+ name: 'member.remove',
443
+ Platform: platform,
444
+ GuildId: event.guild_id ?? '',
445
+ ChannelId: '',
446
+ SpaceId: `GUILD:${event.guild_id ?? ''}`,
447
+ UserId: UserId,
448
+ UserKey,
449
+ UserName: event.user?.username ?? '',
450
+ UserAvatar: createUserAvatarURL(UserId),
451
+ IsMaster: isMaster,
452
+ IsBot: false,
453
+ MessageId: '',
454
+ BotId: botId,
455
+ _tag: 'GUILD_MEMBER_REMOVE',
456
+ value: event
457
+ };
458
+ cbp.send(e);
459
+ });
460
+ client.on('FRIEND_ADD', event => {
461
+ const e = {
462
+ name: 'private.friend.add',
463
+ Platform: platform,
464
+ UserId: event.openid ?? '',
465
+ UserKey: '',
466
+ IsMaster: false,
467
+ IsBot: false,
468
+ MessageId: '',
469
+ BotId: botId,
470
+ _tag: 'FRIEND_ADD',
471
+ value: event
472
+ };
473
+ cbp.send(e);
474
+ });
355
475
  client.on('ERROR', console.error);
356
476
  const api = {
357
477
  active: {
358
478
  send: {
359
- channel: async (channel_id, val) => {
360
- if (!channel_id || typeof channel_id !== 'string') {
361
- throw new Error('Invalid channel_id: channel_id must be a string');
479
+ channel: async (SpaceId, val) => {
480
+ if (/^GUILD:/.test(SpaceId)) {
481
+ const id = SpaceId.replace('GUILD:', '');
482
+ return await AT_MESSAGE_CREATE(client, {
483
+ ChannelId: id
484
+ }, val);
485
+ }
486
+ if (/^GROUP:/.test(SpaceId)) {
487
+ const id = SpaceId.replace('GROUP:', '');
488
+ return await GROUP_AT_MESSAGE_CREATE(client, {
489
+ ChannelId: id
490
+ }, val);
362
491
  }
363
- return await AT_MESSAGE_CREATE(client, {
364
- ChannelId: channel_id
365
- }, val);
492
+ return [];
366
493
  },
367
- user: async (user_id, val) => {
368
- if (!user_id || typeof user_id !== 'string') {
369
- throw new Error('Invalid user_id: user_id must be a string');
494
+ user: async (OpenId, val) => {
495
+ if (/^C2C:/.test(OpenId)) {
496
+ const id = OpenId.replace('C2C:', '');
497
+ return await C2C_MESSAGE_CREATE(client, {
498
+ UserId: id
499
+ }, val);
370
500
  }
371
- return await C2C_MESSAGE_CREATE(client, {
372
- OpenId: user_id
373
- }, val);
501
+ else if (/^DIRECT:/.test(OpenId)) {
502
+ const id = OpenId.replace('DIRECT:', '');
503
+ return await DIRECT_MESSAGE_CREATE(client, {
504
+ UserId: id
505
+ }, val);
506
+ }
507
+ else if (/^GUILD:/.test(OpenId)) {
508
+ const id = OpenId.replace('GUILD:', '');
509
+ return await AT_MESSAGE_CREATE(client, {
510
+ ChannelId: id
511
+ }, val);
512
+ }
513
+ return [];
374
514
  }
375
515
  }
376
516
  },
377
517
  use: {
378
518
  send: async (event, val) => {
379
- if (val.length < 0)
380
- Promise.all([]);
381
- // 打 tag
382
- const tag = event.tag;
383
- // 群at
384
- if (tag == 'GROUP_AT_MESSAGE_CREATE') {
519
+ if (!val || val.length <= 0) {
520
+ return [];
521
+ }
522
+ const tag = event._tag;
523
+ if (tag === 'GROUP_AT_MESSAGE_CREATE') {
385
524
  return await GROUP_AT_MESSAGE_CREATE(client, event, val);
386
525
  }
387
- // 私聊
388
- if (tag == 'C2C_MESSAGE_CREATE') {
526
+ if (tag === 'C2C_MESSAGE_CREATE') {
389
527
  return await C2C_MESSAGE_CREATE(client, event, val);
390
528
  }
391
- // 频道私聊
392
- if (tag == 'DIRECT_MESSAGE_CREATE') {
529
+ if (tag === 'DIRECT_MESSAGE_CREATE') {
393
530
  return await DIRECT_MESSAGE_CREATE(client, event, val);
394
531
  }
395
- // 频道at
396
- if (tag == 'AT_MESSAGE_CREATE') {
532
+ if (tag === 'AT_MESSAGE_CREATE') {
397
533
  return await AT_MESSAGE_CREATE(client, event, val);
398
534
  }
399
- // 频道消息
400
- if (tag == 'MESSAGE_CREATE') {
535
+ if (tag === 'MESSAGE_CREATE') {
401
536
  return await MESSAGE_CREATE(client, event, val);
402
537
  }
403
- // 交互
404
- if (tag == 'INTERACTION_CREATE_GROUP') {
538
+ if (tag === 'INTERACTION_CREATE_GROUP') {
405
539
  return await GROUP_AT_MESSAGE_CREATE(client, event, val);
406
540
  }
407
- if (tag == 'INTERACTION_CREATE_C2C') {
541
+ if (tag === 'INTERACTION_CREATE_C2C') {
408
542
  return await C2C_MESSAGE_CREATE(client, event, val);
409
543
  }
410
- if (tag == 'INTERACTION_CREATE_GUILD') {
544
+ if (tag === 'INTERACTION_CREATE_GUILD') {
411
545
  return await AT_MESSAGE_CREATE(client, event, val);
412
546
  }
413
- return Promise.all([]);
547
+ return [];
414
548
  },
415
- mention: async (event) => {
416
- event.value || {};
417
- event.tag;
418
- // const event = e.value
549
+ mention: event => {
550
+ const value = event.value || {};
551
+ const tag = event._tag;
419
552
  const Metions = [];
420
- // group
421
- return Metions;
553
+ if (tag === 'GROUP_AT_MESSAGE_CREATE' || tag === 'C2C_MESSAGE_CREATE') {
554
+ return new Promise(resolve => resolve(Metions));
555
+ }
556
+ if (value.mentions) {
557
+ const mentions = event.value['mentions'];
558
+ const MessageMention = mentions.map(item => {
559
+ const UserId = item.id;
560
+ const [isMaster, UserKey] = getMaster(UserId);
561
+ return {
562
+ UserId: item.id,
563
+ IsMaster: isMaster,
564
+ UserName: item.username,
565
+ IsBot: item.bot,
566
+ UserKey: UserKey
567
+ };
568
+ });
569
+ return new Promise(resolve => resolve(MessageMention));
570
+ }
571
+ else {
572
+ return new Promise(resolve => resolve(Metions));
573
+ }
422
574
  }
423
575
  }
424
576
  };
425
- // 处理行为
426
- cbp.onactions(async (data, consume) => {
427
- if (data.action === 'message.send') {
428
- // 消息发送
429
- const event = data.payload.event;
430
- const paramFormat = data.payload.params.format;
431
- // 消费
432
- const res = await api.use.send(event, paramFormat);
433
- consume(res);
434
- }
435
- else if (data.action === 'mention.get') {
436
- const event = data.payload.event;
437
- // 获取提及
438
- const metions = await api.use.mention(event);
439
- // 消费
440
- consume([createResult(ResultCode.Ok, '请求完成', metions)]);
441
- }
442
- else if (data.action === 'message.send.channel') {
443
- // 主动发送消息到频道
444
- const channel_id = data.payload.ChannelId;
445
- const paramFormat = data.payload.params.format;
446
- const res = await api.active.send.channel(channel_id, paramFormat);
447
- consume(res);
577
+ const onactions = async (data, consume) => {
578
+ try {
579
+ if (data.action === 'me.info') {
580
+ const res = await client.usersMe();
581
+ const UserId = res.id;
582
+ const [isMaster, UserKey] = getMaster(UserId);
583
+ const botInfo = {
584
+ UserId: res?.id,
585
+ UserName: res?.username,
586
+ UserAvatar: createUserAvatarURL(res?.id),
587
+ IsBot: true,
588
+ IsMaster: isMaster,
589
+ UserKey: UserKey
590
+ };
591
+ consume([createResult(ResultCode.Ok, '请求完成', botInfo)]);
592
+ }
593
+ else if (data.action === 'message.send') {
594
+ const event = data.payload.event;
595
+ const paramFormat = data.payload.params.format;
596
+ const res = await api.use.send(event, paramFormat);
597
+ consume(res);
598
+ }
599
+ else if (data.action === 'mention.get') {
600
+ const event = data.payload.event;
601
+ const metions = await api.use.mention(event);
602
+ consume([createResult(ResultCode.Ok, '请求完成', metions)]);
603
+ }
604
+ else if (data.action === 'message.send.channel') {
605
+ const channelId = data.payload.ChannelId;
606
+ const paramFormat = data.payload.params.format;
607
+ const res = await api.active.send.channel(channelId, paramFormat);
608
+ consume(res);
609
+ }
610
+ else if (data.action === 'message.send.user') {
611
+ const userId = data.payload.UserId;
612
+ const paramFormat = data.payload.params.format;
613
+ const res = await api.active.send.user(userId, paramFormat);
614
+ consume(res);
615
+ }
616
+ else {
617
+ consume([createResult(ResultCode.Fail, '未知请求,请尝试升级版本', null)]);
618
+ }
448
619
  }
449
- else if (data.action === 'message.send.user') {
450
- // 主动发送消息到用户
451
- const user_id = data.payload.UserId;
452
- const paramFormat = data.payload.params.format;
453
- const res = await api.active.send.user(user_id, paramFormat);
454
- consume(res);
620
+ catch (error) {
621
+ consume([createResult(ResultCode.Fail, '请求失败', error)]);
455
622
  }
456
- });
457
- // 处理 api 调用
458
- cbp?.onapis &&
459
- cbp.onapis(async (data, consume) => {
460
- const key = data.payload?.key;
461
- if (client[key]) {
462
- // 如果 client 上有对应的 key,直接调用。
463
- const params = data.payload.params;
623
+ };
624
+ cbp.onactions((data, consume) => void onactions(data, consume));
625
+ const onapis = async (data, consume) => {
626
+ const key = data.payload?.key;
627
+ if (client[key]) {
628
+ const params = data.payload.params;
629
+ try {
464
630
  const res = await client[key](...params);
465
631
  consume([createResult(ResultCode.Ok, '请求完成', res)]);
466
632
  }
467
- });
633
+ catch (error) {
634
+ consume([createResult(ResultCode.Fail, '请求失败', error)]);
635
+ }
636
+ }
637
+ else {
638
+ consume([createResult(ResultCode.Fail, '未知请求,请尝试升级版本', null)]);
639
+ }
640
+ };
641
+ cbp.onapis((data, consume) => void onapis(data, consume));
468
642
  };
469
643
 
470
- export { getQQBotConfig, platform, register };
644
+ export { register };