@alemonjs/qq-bot 2.1.0-alpha.2 → 2.1.0-alpha.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 (109) hide show
  1. package/README.md +7 -30
  2. package/lib/config.d.ts +8 -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/hook.d.ts +30 -9
  7. package/lib/hook.js +19 -17
  8. package/lib/index.d.ts +5 -6
  9. package/lib/index.js +37 -19
  10. package/lib/index.webhook.d.ts +1 -0
  11. package/lib/index.webhook.js +5 -4
  12. package/lib/index.websoket.d.ts +1 -0
  13. package/lib/index.websoket.js +41 -22
  14. package/lib/message/AT_MESSAGE_CREATE.d.ts +35 -0
  15. package/lib/message/AT_MESSAGE_CREATE.js +1 -0
  16. package/lib/message/CHANNEL_CREATE.d.ts +15 -0
  17. package/lib/message/CHANNEL_CREATE.js +1 -0
  18. package/lib/message/CHANNEL_DELETE.d.ts +15 -0
  19. package/lib/message/CHANNEL_DELETE.js +1 -0
  20. package/lib/message/CHANNEL_UPDATE.d.ts +15 -0
  21. package/lib/message/CHANNEL_UPDATE.js +1 -0
  22. package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +29 -0
  23. package/lib/message/DIRECT_MESSAGE_CREATE.js +1 -0
  24. package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +17 -0
  25. package/lib/message/DIRECT_MESSAGE_DELETE.js +1 -0
  26. package/lib/message/ERROR.d.ts +1 -0
  27. package/lib/message/ERROR.js +1 -0
  28. package/lib/message/GUILD_CREATE.d.ts +15 -0
  29. package/lib/message/GUILD_CREATE.js +1 -0
  30. package/lib/message/GUILD_DELETE.d.ts +15 -0
  31. package/lib/message/GUILD_DELETE.js +1 -0
  32. package/lib/message/GUILD_MEMBER_ADD.d.ts +14 -0
  33. package/lib/message/GUILD_MEMBER_ADD.js +1 -0
  34. package/lib/message/GUILD_MEMBER_REMOVE.d.ts +14 -0
  35. package/lib/message/GUILD_MEMBER_REMOVE.js +1 -0
  36. package/lib/message/GUILD_MEMBER_UPDATE.d.ts +14 -0
  37. package/lib/message/GUILD_MEMBER_UPDATE.js +1 -0
  38. package/lib/message/GUILD_UPDATE.d.ts +15 -0
  39. package/lib/message/GUILD_UPDATE.js +1 -0
  40. package/lib/message/INTERACTION_CREATE.d.ts +53 -0
  41. package/lib/message/INTERACTION_CREATE.js +1 -0
  42. package/lib/message/MESSAGE_CREATE.d.ts +1 -0
  43. package/lib/message/MESSAGE_CREATE.js +1 -0
  44. package/lib/message/MESSAGE_DELETE.d.ts +1 -0
  45. package/lib/message/MESSAGE_DELETE.js +1 -0
  46. package/lib/message/MESSAGE_REACTION_ADD.d.ts +13 -0
  47. package/lib/message/MESSAGE_REACTION_ADD.js +1 -0
  48. package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +13 -0
  49. package/lib/message/MESSAGE_REACTION_REMOVE.js +1 -0
  50. package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +15 -0
  51. package/lib/message/PUBLIC_MESSAGE_DELETE.js +1 -0
  52. package/lib/message/READY.d.ts +6 -0
  53. package/lib/message/READY.js +1 -0
  54. package/lib/message/group/C2C_MESSAGE_CREATE.d.ts +9 -0
  55. package/lib/message/group/C2C_MESSAGE_CREATE.js +1 -0
  56. package/lib/message/group/C2C_MSG_RECEIVE.d.ts +4 -0
  57. package/lib/message/group/C2C_MSG_RECEIVE.js +1 -0
  58. package/lib/message/group/C2C_MSG_REJECT.d.ts +4 -0
  59. package/lib/message/group/C2C_MSG_REJECT.js +1 -0
  60. package/lib/message/group/FRIEND_ADD.d.ts +4 -0
  61. package/lib/message/group/FRIEND_ADD.js +1 -0
  62. package/lib/message/group/FRIEND_DEL.d.ts +4 -0
  63. package/lib/message/group/FRIEND_DEL.js +1 -0
  64. package/lib/message/group/GROUP_ADD_ROBOT.d.ts +5 -0
  65. package/lib/message/group/GROUP_ADD_ROBOT.js +1 -0
  66. package/lib/message/group/GROUP_AT_MESSAGE_CREATE.d.ts +11 -0
  67. package/lib/message/group/GROUP_AT_MESSAGE_CREATE.js +1 -0
  68. package/lib/message/group/GROUP_DEL_ROBOT.d.ts +5 -0
  69. package/lib/message/group/GROUP_DEL_ROBOT.js +1 -0
  70. package/lib/message/group/GROUP_MSG_RECEIVE.d.ts +5 -0
  71. package/lib/message/group/GROUP_MSG_RECEIVE.js +1 -0
  72. package/lib/message/group/GROUP_MSG_REJECT.d.ts +5 -0
  73. package/lib/message/group/GROUP_MSG_REJECT.js +1 -0
  74. package/lib/register.d.ts +2 -3
  75. package/lib/register.js +270 -176
  76. package/lib/sdk/api.d.ts +196 -969
  77. package/lib/sdk/api.js +267 -916
  78. package/lib/sdk/client.webhook.d.ts +9 -0
  79. package/lib/sdk/{client.js → client.webhook.js} +19 -53
  80. package/lib/sdk/client.websoket.d.ts +9 -0
  81. package/lib/sdk/client.websoket.js +43 -96
  82. package/lib/sdk/config.d.ts +1 -0
  83. package/lib/sdk/instance.d.ts +3 -0
  84. package/lib/sdk/instance.js +93 -0
  85. package/lib/sdk/intents.d.ts +3 -0
  86. package/lib/sdk/intents.js +25 -90
  87. package/lib/sdk/message.d.ts +4 -0
  88. package/lib/sdk/message.group.d.ts +24 -0
  89. package/lib/sdk/message.group.js +1 -0
  90. package/lib/sdk/message.guild.d.ts +38 -0
  91. package/lib/sdk/message.guild.js +1 -0
  92. package/lib/sdk/message.js +1 -0
  93. package/lib/sdk/message.public.d.ts +6 -0
  94. package/lib/sdk/message.public.js +1 -0
  95. package/lib/sdk/typing.d.ts +67 -59
  96. package/lib/sdk/typing.js +1 -0
  97. package/lib/sdk/webhook.secret.d.ts +14 -0
  98. package/lib/sdk/webhook.secret.js +6 -21
  99. package/lib/sends.d.ts +29 -0
  100. package/lib/sends.js +546 -477
  101. package/lib/utils.d.ts +1 -0
  102. package/lib/utils.js +7 -12
  103. package/package.json +9 -5
  104. package/lib/index.group.js +0 -19
  105. package/lib/index.guild.js +0 -36
  106. package/lib/sdk/client.websoket.group.js +0 -221
  107. package/lib/sdk/client.websoket.guild.js +0 -205
  108. package/lib/sdk/counter.js +0 -19
  109. package/lib/sdk/from.js +0 -44
package/lib/register.js CHANGED
@@ -1,326 +1,420 @@
1
- import { getConfigValue, cbpPlatform, useUserHashKey } 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
- const platform = 'qq-bot';
5
- const getQQBotConfig = () => {
6
- let value = getConfigValue();
7
- if (!value)
8
- value = {};
9
- return value[platform] || {};
10
- };
11
5
  const register = (client) => {
12
6
  const config = getQQBotConfig();
13
- /**
14
- * 连接 alemonjs 服务器。
15
- * 向 alemonjs 推送标准信息
16
- */
17
7
  const port = process.env?.port || config?.port || 17117;
18
8
  const url = `ws://127.0.0.1:${port}`;
19
9
  const cbp = cbpPlatform(url);
20
- /**
21
- * group
22
- *
23
- * GROUP_AT_MESSAGE_CREATE
24
- * C2C_MESSAGE_CREATE
25
- */
26
10
  const createUserAvatarURL = (author_id) => {
27
11
  return `https://q.qlogo.cn/qqapp/${config.app_id}/${author_id}/640`;
28
12
  };
29
- // 监听消息
30
- client.on('GROUP_AT_MESSAGE_CREATE', async (event) => {
31
- const master_key = config?.master_key ?? [];
32
- const isMaster = master_key.includes(event.author.id);
33
- const UserAvatar = createUserAvatarURL(event.author.id);
34
- const UserId = event.author.id;
35
- const UserKey = useUserHashKey({
13
+ client.on('GROUP_ADD_ROBOT', event => {
14
+ const e = {
15
+ name: 'guild.join',
16
+ Platform: platform,
17
+ GuildId: event.group_openid,
18
+ ChannelId: event.group_openid,
19
+ SpaceId: `GROUP:${event.group_openid}`,
20
+ UserId: event.op_member_openid,
21
+ UserKey: event.op_member_openid,
22
+ UserAvatar: createUserAvatarURL(event.op_member_openid),
23
+ IsMaster: false,
24
+ IsBot: false,
25
+ OpenId: `C2C:${event.op_member_openid}`,
26
+ MessageId: '',
27
+ CreateAt: Date.now(),
28
+ tag: 'GROUP_DEL_ROBOT',
29
+ value: event
30
+ };
31
+ cbp.send(e);
32
+ });
33
+ client.on('GROUP_DEL_ROBOT', event => {
34
+ const e = {
35
+ name: 'guild.exit',
36
36
  Platform: platform,
37
- UserId: UserId
38
- });
39
- // 定义消
37
+ GuildId: event.group_openid,
38
+ ChannelId: event.group_openid,
39
+ SpaceId: `GROUP:${event.group_openid}`,
40
+ UserId: event.op_member_openid,
41
+ UserKey: event.op_member_openid,
42
+ UserAvatar: createUserAvatarURL(event.op_member_openid),
43
+ IsMaster: false,
44
+ IsBot: false,
45
+ OpenId: `C2C:${event.op_member_openid}`,
46
+ MessageId: '',
47
+ CreateAt: Date.now(),
48
+ tag: 'GROUP_DEL_ROBOT',
49
+ value: event
50
+ };
51
+ cbp.send(e);
52
+ });
53
+ client.on('GROUP_AT_MESSAGE_CREATE', event => {
54
+ const UserId = event.author.id;
55
+ const [isMaster, UserKey] = getMaster(UserId);
56
+ const UserAvatar = createUserAvatarURL(event.author.id);
40
57
  const e = {
41
58
  name: 'message.create',
42
- // 事件类型
43
59
  Platform: platform,
44
- // guild
45
60
  GuildId: event.group_id,
46
61
  ChannelId: event.group_id,
47
- // 用户Id
62
+ SpaceId: `GROUP:${event.group_id}`,
48
63
  UserId: event.author.id,
49
64
  UserKey,
50
65
  UserAvatar: UserAvatar,
51
66
  IsMaster: isMaster,
52
67
  IsBot: false,
53
- // 格式化数据
54
68
  MessageId: event.id,
55
69
  MessageText: event.content?.trim(),
56
- OpenId: event.author.member_openid,
70
+ OpenId: `C2C:${event.author.member_openid}`,
57
71
  CreateAt: Date.now(),
58
72
  tag: 'GROUP_AT_MESSAGE_CREATE',
59
73
  value: event
60
74
  };
61
75
  cbp.send(e);
62
76
  });
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);
77
+ client.on('C2C_MESSAGE_CREATE', event => {
67
78
  const UserId = event.author.id;
68
- const UserKey = useUserHashKey({
69
- Platform: platform,
70
- UserId: UserId
71
- });
72
- // 定义消
79
+ const [isMaster, UserKey] = getMaster(UserId);
80
+ const UserAvatar = createUserAvatarURL(event.author.id);
73
81
  const e = {
74
82
  name: 'private.message.create',
75
- // 事件类型
76
83
  Platform: platform,
77
- // 用户Id
78
84
  UserId: event.author.id,
79
85
  UserKey,
80
86
  UserAvatar: UserAvatar,
81
87
  IsMaster: isMaster,
82
88
  IsBot: false,
83
- // 格式化数据
84
89
  MessageId: event.id,
85
90
  MessageText: event.content?.trim(),
86
91
  CreateAt: Date.now(),
87
- OpenId: event.author.user_openid,
88
- //
92
+ OpenId: `C2C:${event.author.user_openid}`,
89
93
  tag: 'C2C_MESSAGE_CREATE',
90
94
  value: event
91
95
  };
92
96
  cbp.send(e);
93
97
  });
94
- /**
95
- * guild
96
- */
97
- client.on('DIRECT_MESSAGE_CREATE', async (event) => {
98
- // 屏蔽其他机器人的消息
99
- if (event?.author?.bot)
98
+ client.on('DIRECT_MESSAGE_CREATE', event => {
99
+ if (event?.author?.bot) {
100
100
  return;
101
- const master_key = config?.master_key ?? [];
102
- const isMaster = master_key.includes(event.author.id);
103
- let msg = event?.content ?? '';
101
+ }
102
+ const msg = event?.content ?? '';
104
103
  const UserAvatar = event?.author?.avatar;
105
104
  const UserId = event.author.id;
106
- const UserKey = useUserHashKey({
107
- Platform: platform,
108
- UserId: UserId
109
- });
110
- // 定义消
105
+ const [isMaster, UserKey] = getMaster(UserId);
111
106
  const e = {
112
107
  name: 'private.message.create',
113
- // 事件类型
114
108
  Platform: platform,
115
- //
116
- // GuildId: event.guild_id,
117
- // ChannelId: event.channel_id,
118
- // 用户Id
119
109
  UserId: event?.author?.id ?? '',
120
110
  UserKey,
121
111
  UserName: event?.author?.username ?? '',
122
112
  UserAvatar: UserAvatar,
123
113
  IsMaster: isMaster,
124
114
  IsBot: event.author?.bot,
125
- // message
126
115
  MessageId: event.id,
127
116
  MessageText: msg?.trim(),
128
- OpenId: event.guild_id,
117
+ OpenId: `DIRECT:${event.guild_id}`,
129
118
  CreateAt: Date.now(),
130
- //
131
119
  tag: 'DIRECT_MESSAGE_CREATE',
132
120
  value: event
133
121
  };
134
122
  cbp.send(e);
135
123
  });
136
- // 监听消息
137
- client.on('AT_MESSAGE_CREATE', async (event) => {
138
- // 屏蔽其他机器人的消息
139
- if (event?.author?.bot)
124
+ client.on('AT_MESSAGE_CREATE', event => {
125
+ if (event?.author?.bot) {
140
126
  return;
141
- const master_key = config?.master_key ?? [];
142
- const isMaster = master_key.includes(event.author.id);
143
- let msg = getMessageContent(event);
127
+ }
128
+ const msg = getMessageContent(event);
144
129
  const UserAvatar = event?.author?.avatar;
145
130
  const UserId = event.author.id;
146
- const UserKey = useUserHashKey({
147
- Platform: platform,
148
- UserId: UserId
149
- });
150
- // 定义消
131
+ const [isMaster, UserKey] = getMaster(UserId);
151
132
  const e = {
152
133
  name: 'message.create',
153
- // 事件类型
154
134
  Platform: platform,
155
135
  GuildId: event.guild_id,
156
136
  ChannelId: event.channel_id,
137
+ SpaceId: `GUILD:${event.channel_id}`,
157
138
  IsMaster: isMaster,
158
- // 用户Id
159
139
  UserId: event?.author?.id ?? '',
160
140
  UserKey,
161
141
  UserName: event?.author?.username ?? '',
162
142
  UserAvatar: UserAvatar,
163
143
  IsBot: event.author?.bot,
164
- // message
165
144
  MessageId: event.id,
166
145
  MessageText: msg?.trim(),
167
- OpenId: event.guild_id,
146
+ OpenId: `DIRECT:${event.guild_id}`,
168
147
  CreateAt: Date.now(),
169
- //
170
148
  tag: 'AT_MESSAGE_CREATE',
171
149
  value: event
172
150
  };
173
151
  cbp.send(e);
174
152
  });
175
- /**
176
- *
177
- * @param event
178
- * @returns
179
- */
180
153
  const getMessageContent = event => {
181
154
  let msg = event?.content ?? '';
182
- // 艾特消息处理
183
- const at_users = [];
155
+ const atUsers = [];
184
156
  if (event.mentions) {
185
- // 去掉@ 转为纯消息
186
157
  for (const item of event.mentions) {
187
- at_users.push({
158
+ atUsers.push({
188
159
  id: item.id
189
160
  });
190
161
  }
191
- // 循环删除文本中的at信息并去除前后空格
192
- at_users.forEach(item => {
162
+ atUsers.forEach(item => {
193
163
  msg = msg.replace(`<@!${item.id}>`, '').trim();
194
164
  });
195
165
  }
196
166
  return msg;
197
167
  };
198
- // 私域 -
199
- client.on('MESSAGE_CREATE', async (event) => {
200
- // 屏蔽其他机器人的消息
201
- if (event.author?.bot)
168
+ client.on('MESSAGE_CREATE', event => {
169
+ if (event.author?.bot) {
202
170
  return;
203
- // 撤回消息
204
- if (new RegExp(/DELETE$/).test(event.eventType))
171
+ }
172
+ if (new RegExp(/DELETE$/).test(event.eventType)) {
205
173
  return;
206
- const master_key = config?.master_key ?? [];
174
+ }
207
175
  const UserId = event.author.id;
208
- const isMaster = master_key.includes(UserId);
209
176
  const msg = getMessageContent(event);
210
177
  const UserAvatar = event?.author?.avatar;
211
- const UserKey = useUserHashKey({
212
- Platform: platform,
213
- UserId: UserId
214
- });
215
- // 定义消
178
+ const [isMaster, UserKey] = getMaster(UserId);
216
179
  const e = {
217
180
  name: 'message.create',
218
- // 事件类型
219
181
  Platform: platform,
220
- //
221
182
  GuildId: event.guild_id,
222
183
  ChannelId: event.channel_id,
184
+ SpaceId: `GUILD:${event.channel_id}`,
223
185
  UserId: event?.author?.id ?? '',
224
186
  UserKey,
225
187
  UserName: event?.author?.username ?? '',
226
188
  UserAvatar: UserAvatar,
227
189
  IsMaster: isMaster,
228
190
  IsBot: false,
229
- // message
230
191
  MessageId: event.id,
231
192
  MessageText: msg?.trim(),
232
- OpenId: event.guild_id,
193
+ OpenId: `DIRECT:${event.guild_id}`,
233
194
  CreateAt: Date.now(),
234
- //
235
195
  tag: 'MESSAGE_CREATE',
236
196
  value: event
237
197
  };
238
198
  cbp.send(e);
239
199
  });
240
- client.on('ERROR', console.error);
241
- const sendMesage = async (event, val) => {
242
- if (val.length < 0)
243
- Promise.all([]);
244
- // 打 tag
245
- const tag = event.tag;
246
- // 群at
247
- if (tag == 'GROUP_AT_MESSAGE_CREATE') {
248
- return await GROUP_AT_MESSAGE_CREATE(client, event, val);
200
+ client.on('INTERACTION_CREATE', event => {
201
+ if (event.scene === 'group') {
202
+ const UserAvatar = createUserAvatarURL(event.group_member_openid);
203
+ const UserId = event.group_member_openid;
204
+ const [isMaster, UserKey] = getMaster(UserId);
205
+ const MessageText = event.data.resolved.button_data?.trim() || '';
206
+ const e = {
207
+ name: 'interaction.create',
208
+ Platform: platform,
209
+ GuildId: event.group_openid,
210
+ ChannelId: event.group_openid,
211
+ SpaceId: `GROUP:${event.group_openid}`,
212
+ UserId: event.group_member_openid,
213
+ UserKey,
214
+ UserAvatar: UserAvatar,
215
+ IsMaster: isMaster,
216
+ IsBot: false,
217
+ MessageId: `INTERACTION_CREATE:${event.id}`,
218
+ MessageText: MessageText,
219
+ OpenId: `C2C:${event.group_member_openid}`,
220
+ tag: 'INTERACTION_CREATE_GROUP',
221
+ CreateAt: Date.now(),
222
+ value: event
223
+ };
224
+ cbp.send(e);
249
225
  }
250
- // 私聊
251
- if (tag == 'C2C_MESSAGE_CREATE') {
252
- return await C2C_MESSAGE_CREATE(client, event, val);
226
+ else if (event.scene === 'c2c') {
227
+ const UserAvatar = createUserAvatarURL(event.user_openid);
228
+ const UserId = event.user_openid;
229
+ const [isMaster, UserKey] = getMaster(UserId);
230
+ const MessageText = event.data.resolved.button_data?.trim() || '';
231
+ const e = {
232
+ name: 'private.interaction.create',
233
+ Platform: platform,
234
+ UserId: event.user_openid,
235
+ UserKey,
236
+ UserAvatar: UserAvatar,
237
+ IsMaster: isMaster,
238
+ IsBot: false,
239
+ MessageId: event.id,
240
+ MessageText: MessageText,
241
+ OpenId: `C2C:${event.user_openid}`,
242
+ CreateAt: Date.now(),
243
+ tag: 'INTERACTION_CREATE_C2C',
244
+ value: event
245
+ };
246
+ cbp.send(e);
253
247
  }
254
- // 频道私聊
255
- if (tag == 'DIRECT_MESSAGE_CREATE') {
256
- return await DIRECT_MESSAGE_CREATE(client, event, val);
248
+ else if (event.scene === 'guild') {
249
+ const UserAvatar = createUserAvatarURL(event.data.resolved.user_id);
250
+ const UserId = event.data.resolved.user_id;
251
+ const [isMaster, UserKey] = getMaster(UserId);
252
+ const MessageText = event.data.resolved.button_data?.trim() || '';
253
+ const e = {
254
+ name: 'interaction.create',
255
+ Platform: platform,
256
+ GuildId: event.guild_id,
257
+ ChannelId: event.channel_id,
258
+ SpaceId: `GUILD:${event.channel_id}`,
259
+ UserId: event.data.resolved.user_id,
260
+ UserKey,
261
+ UserAvatar: UserAvatar,
262
+ IsMaster: isMaster,
263
+ IsBot: false,
264
+ MessageId: event.data.resolved.message_id,
265
+ MessageText: MessageText,
266
+ OpenId: `DIRECT:${event.guild_id}`,
267
+ CreateAt: Date.now(),
268
+ tag: 'INTERACTION_CREATE_GUILD',
269
+ value: event
270
+ };
271
+ cbp.send(e);
257
272
  }
258
- // 频道at
259
- if (tag == 'AT_MESSAGE_CREATE') {
260
- return await AT_MESSAGE_CREATE(client, event, val);
261
- }
262
- // 频道消息
263
- if (tag == 'MESSAGE_CREATE') {
264
- return await MESSAGE_CREATE(client, event, val);
265
- }
266
- return Promise.all([]);
267
- };
268
- const getMesion = async (event) => {
269
- event.value;
270
- event.tag;
271
- // const event = e.value
272
- const Metions = [];
273
- // group
274
- return Metions;
275
- };
276
- const sendMessageChannel = async (channel_id, data) => {
277
- if (!channel_id || typeof channel_id !== 'string') {
278
- throw new Error('Invalid channel_id: channel_id must be a string');
273
+ else {
274
+ logger.warn({
275
+ code: ResultCode.Fail,
276
+ message: '暂未更新支持此类型的交互事件',
277
+ data: event
278
+ });
279
279
  }
280
- return await AT_MESSAGE_CREATE(client, {
281
- ChannelId: channel_id
282
- }, data);
283
- };
284
- const sendMessageUser = async (user_id, data) => {
285
- if (!user_id || typeof user_id !== 'string') {
286
- throw new Error('Invalid user_id: user_id must be a string');
280
+ });
281
+ client.on('ERROR', console.error);
282
+ const api = {
283
+ active: {
284
+ send: {
285
+ channel: async (SpaceId, val) => {
286
+ if (/^GUILD:/.test(SpaceId)) {
287
+ const id = SpaceId.replace('GUILD:', '');
288
+ return await AT_MESSAGE_CREATE(client, {
289
+ ChannelId: id
290
+ }, val);
291
+ }
292
+ if (/^GROUP:/.test(SpaceId)) {
293
+ const id = SpaceId.replace('GROUP:', '');
294
+ return await GROUP_AT_MESSAGE_CREATE(client, {
295
+ ChannelId: id
296
+ }, val);
297
+ }
298
+ return [];
299
+ },
300
+ user: async (OpenId, val) => {
301
+ if (/^C2C:/.test(OpenId)) {
302
+ const id = OpenId.replace('C2C:', '');
303
+ return await C2C_MESSAGE_CREATE(client, {
304
+ UserId: id
305
+ }, val);
306
+ }
307
+ else if (/^DIRECT:/.test(OpenId)) {
308
+ const id = OpenId.replace('DIRECT:', '');
309
+ return await DIRECT_MESSAGE_CREATE(client, {
310
+ UserId: id
311
+ }, val);
312
+ }
313
+ else if (/^GUILD:/.test(OpenId)) {
314
+ const id = OpenId.replace('GUILD:', '');
315
+ return await AT_MESSAGE_CREATE(client, {
316
+ ChannelId: id
317
+ }, val);
318
+ }
319
+ return [];
320
+ }
321
+ }
322
+ },
323
+ use: {
324
+ send: async (event, val) => {
325
+ if (val.length < 0) {
326
+ return [];
327
+ }
328
+ const tag = event.tag;
329
+ if (tag === 'GROUP_AT_MESSAGE_CREATE') {
330
+ return await GROUP_AT_MESSAGE_CREATE(client, event, val);
331
+ }
332
+ if (tag === 'C2C_MESSAGE_CREATE') {
333
+ return await C2C_MESSAGE_CREATE(client, event, val);
334
+ }
335
+ if (tag === 'DIRECT_MESSAGE_CREATE') {
336
+ return await DIRECT_MESSAGE_CREATE(client, event, val);
337
+ }
338
+ if (tag === 'AT_MESSAGE_CREATE') {
339
+ return await AT_MESSAGE_CREATE(client, event, val);
340
+ }
341
+ if (tag === 'MESSAGE_CREATE') {
342
+ return await MESSAGE_CREATE(client, event, val);
343
+ }
344
+ if (tag === 'INTERACTION_CREATE_GROUP') {
345
+ return await GROUP_AT_MESSAGE_CREATE(client, event, val);
346
+ }
347
+ if (tag === 'INTERACTION_CREATE_C2C') {
348
+ return await C2C_MESSAGE_CREATE(client, event, val);
349
+ }
350
+ if (tag === 'INTERACTION_CREATE_GUILD') {
351
+ return await AT_MESSAGE_CREATE(client, event, val);
352
+ }
353
+ return [];
354
+ },
355
+ mention: event => {
356
+ const value = event.value || {};
357
+ const tag = event.tag;
358
+ const Metions = [];
359
+ if (tag === 'GROUP_AT_MESSAGE_CREATE' || tag === 'C2C_MESSAGE_CREATE') {
360
+ return new Promise(resolve => resolve(Metions));
361
+ }
362
+ if (value.mentions) {
363
+ const mentions = event.value['mentions'];
364
+ const MessageMention = mentions.map(item => {
365
+ const UserId = item.id;
366
+ const [isMaster, UserKey] = getMaster(UserId);
367
+ return {
368
+ UserId: item.id,
369
+ IsMaster: isMaster,
370
+ UserName: item.username,
371
+ IsBot: item.bot,
372
+ UserKey: UserKey
373
+ };
374
+ });
375
+ return new Promise(resolve => resolve(MessageMention));
376
+ }
377
+ else {
378
+ return new Promise(resolve => resolve(Metions));
379
+ }
380
+ }
287
381
  }
288
- return await C2C_MESSAGE_CREATE(client, {
289
- OpenId: user_id
290
- }, data);
291
382
  };
292
- // 处理行为
293
- cbp.onactions(async (data, consume) => {
383
+ const onactions = async (data, consume) => {
294
384
  if (data.action === 'message.send') {
295
- // 消息发送
296
385
  const event = data.payload.event;
297
386
  const paramFormat = data.payload.params.format;
298
- // 消费
299
- const res = await sendMesage(event, paramFormat);
387
+ const res = await api.use.send(event, paramFormat);
300
388
  consume(res);
301
389
  }
302
390
  else if (data.action === 'mention.get') {
303
391
  const event = data.payload.event;
304
- // 获取提及
305
- const metions = await getMesion(event);
306
- // 消费
307
- consume(metions);
392
+ const metions = await api.use.mention(event);
393
+ consume([createResult(ResultCode.Ok, '请求完成', metions)]);
308
394
  }
309
395
  else if (data.action === 'message.send.channel') {
310
- // 主动发送消息到频道
311
396
  const channel_id = data.payload.ChannelId;
312
397
  const paramFormat = data.payload.params.format;
313
- const res = await sendMessageChannel(channel_id, paramFormat);
398
+ const res = await api.active.send.channel(channel_id, paramFormat);
314
399
  consume(res);
315
400
  }
316
401
  else if (data.action === 'message.send.user') {
317
- // 主动发送消息到用户
318
402
  const user_id = data.payload.UserId;
319
403
  const paramFormat = data.payload.params.format;
320
- const res = await sendMessageUser(user_id, paramFormat);
404
+ const res = await api.active.send.user(user_id, paramFormat);
321
405
  consume(res);
322
406
  }
323
- });
407
+ };
408
+ cbp.onactions((data, consume) => void onactions(data, consume));
409
+ const onapis = async (data, consume) => {
410
+ const key = data.payload?.key;
411
+ if (client[key]) {
412
+ const params = data.payload.params;
413
+ const res = await client[key](...params);
414
+ consume([createResult(ResultCode.Ok, '请求完成', res)]);
415
+ }
416
+ };
417
+ cbp.onapis((data, consume) => void onapis(data, consume));
324
418
  };
325
419
 
326
- export { getQQBotConfig, platform, register };
420
+ export { register };