@alemonjs/qq-bot 2.1.0-alpha.2 → 2.1.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.html CHANGED
@@ -6,7 +6,7 @@
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
7
  <title>AlemonJS</title>
8
8
  <script type="module" crossorigin src="/assets/index.js"></script>
9
- <link rel="stylesheet" crossorigin href="/assets/index.css">
9
+ <link rel="stylesheet" crossorigin href="/assets/index.css" />
10
10
  </head>
11
11
 
12
12
  <body>
package/lib/register.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getConfigValue, cbpPlatform, useUserHashKey } from 'alemonjs';
1
+ import { getConfigValue, cbpPlatform, useUserHashKey, createResult, ResultCode } from 'alemonjs';
2
2
  import { GROUP_AT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, AT_MESSAGE_CREATE, MESSAGE_CREATE } from './sends.js';
3
3
 
4
4
  const platform = 'qq-bot';
@@ -238,56 +238,64 @@ const register = (client) => {
238
238
  cbp.send(e);
239
239
  });
240
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);
249
- }
250
- // 私聊
251
- if (tag == 'C2C_MESSAGE_CREATE') {
252
- return await C2C_MESSAGE_CREATE(client, event, val);
253
- }
254
- // 频道私聊
255
- if (tag == 'DIRECT_MESSAGE_CREATE') {
256
- return await DIRECT_MESSAGE_CREATE(client, event, val);
257
- }
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');
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');
241
+ const api = {
242
+ active: {
243
+ send: {
244
+ channel: async (channel_id, val) => {
245
+ if (!channel_id || typeof channel_id !== 'string') {
246
+ throw new Error('Invalid channel_id: channel_id must be a string');
247
+ }
248
+ return await AT_MESSAGE_CREATE(client, {
249
+ ChannelId: channel_id
250
+ }, val);
251
+ },
252
+ user: async (user_id, val) => {
253
+ if (!user_id || typeof user_id !== 'string') {
254
+ throw new Error('Invalid user_id: user_id must be a string');
255
+ }
256
+ return await C2C_MESSAGE_CREATE(client, {
257
+ OpenId: user_id
258
+ }, val);
259
+ }
260
+ }
261
+ },
262
+ use: {
263
+ send: async (event, val) => {
264
+ if (val.length < 0)
265
+ Promise.all([]);
266
+ // 打 tag
267
+ const tag = event.tag;
268
+ // 群at
269
+ if (tag == 'GROUP_AT_MESSAGE_CREATE') {
270
+ return await GROUP_AT_MESSAGE_CREATE(client, event, val);
271
+ }
272
+ // 私聊
273
+ if (tag == 'C2C_MESSAGE_CREATE') {
274
+ return await C2C_MESSAGE_CREATE(client, event, val);
275
+ }
276
+ // 频道私聊
277
+ if (tag == 'DIRECT_MESSAGE_CREATE') {
278
+ return await DIRECT_MESSAGE_CREATE(client, event, val);
279
+ }
280
+ // 频道at
281
+ if (tag == 'AT_MESSAGE_CREATE') {
282
+ return await AT_MESSAGE_CREATE(client, event, val);
283
+ }
284
+ // 频道消息
285
+ if (tag == 'MESSAGE_CREATE') {
286
+ return await MESSAGE_CREATE(client, event, val);
287
+ }
288
+ return Promise.all([]);
289
+ },
290
+ mention: async (event) => {
291
+ event.value;
292
+ event.tag;
293
+ // const event = e.value
294
+ const Metions = [];
295
+ // group
296
+ return Metions;
297
+ }
287
298
  }
288
- return await C2C_MESSAGE_CREATE(client, {
289
- OpenId: user_id
290
- }, data);
291
299
  };
292
300
  // 处理行为
293
301
  cbp.onactions(async (data, consume) => {
@@ -296,28 +304,28 @@ const register = (client) => {
296
304
  const event = data.payload.event;
297
305
  const paramFormat = data.payload.params.format;
298
306
  // 消费
299
- const res = await sendMesage(event, paramFormat);
307
+ const res = await api.use.send(event, paramFormat);
300
308
  consume(res);
301
309
  }
302
310
  else if (data.action === 'mention.get') {
303
311
  const event = data.payload.event;
304
312
  // 获取提及
305
- const metions = await getMesion(event);
313
+ const metions = await api.use.mention(event);
306
314
  // 消费
307
- consume(metions);
315
+ consume([createResult(ResultCode.Ok, '请求完成', metions)]);
308
316
  }
309
317
  else if (data.action === 'message.send.channel') {
310
318
  // 主动发送消息到频道
311
319
  const channel_id = data.payload.ChannelId;
312
320
  const paramFormat = data.payload.params.format;
313
- const res = await sendMessageChannel(channel_id, paramFormat);
321
+ const res = await api.active.send.channel(channel_id, paramFormat);
314
322
  consume(res);
315
323
  }
316
324
  else if (data.action === 'message.send.user') {
317
325
  // 主动发送消息到用户
318
326
  const user_id = data.payload.UserId;
319
327
  const paramFormat = data.payload.params.format;
320
- const res = await sendMessageUser(user_id, paramFormat);
328
+ const res = await api.active.send.user(user_id, paramFormat);
321
329
  consume(res);
322
330
  }
323
331
  });
package/lib/sends.js CHANGED
@@ -212,9 +212,8 @@ const GROUP_AT_MESSAGE_CREATE = async (client, event, val) => {
212
212
  });
213
213
  }
214
214
  try {
215
- const file_data = item.type == 'ImageFile'
216
- ? readFileSync(item.value, 'base64')
217
- : item.value;
215
+ const getFileBase64 = () => readFileSync(item.value, 'base64');
216
+ const file_data = item.type == 'ImageFile' ? getFileBase64() : item.value;
218
217
  const file_info = await client
219
218
  .postRichMediaByGroup(event.GuildId, {
220
219
  file_type: 1,
@@ -374,9 +373,8 @@ const C2C_MESSAGE_CREATE = (client, event, val) => {
374
373
  id: res.id
375
374
  });
376
375
  }
377
- const file_data = item.type == 'ImageFile'
378
- ? readFileSync(item.value, 'base64')
379
- : item.value;
376
+ const getFileBase64 = () => readFileSync(item.value, 'base64');
377
+ const file_data = item.type == 'ImageFile' ? getFileBase64() : item.value;
380
378
  const file_info = await client
381
379
  .postRichMediaByUsers(event.OpenId, {
382
380
  file_type: 1,
@@ -521,10 +519,10 @@ const DIRECT_MESSAGE_CREATE = (client, event, val) => {
521
519
  });
522
520
  return createResult(ResultCode.Ok, 'client.postDirectImage', { id: res?.id });
523
521
  }
524
- const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : item.value;
522
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : Buffer.from(item.value, 'base64');
525
523
  const res = await client.postDirectImage(event.OpenId, {
526
524
  msg_id: event.MessageId,
527
- image: Buffer.isBuffer(file_data) ? file_data : Buffer.from(file_data)
525
+ image: file_data
528
526
  });
529
527
  return createResult(ResultCode.Ok, 'client.postDirectImage', { id: res?.id });
530
528
  })).catch(err => [
@@ -587,10 +585,10 @@ const AT_MESSAGE_CREATE = (client, event, val) => {
587
585
  });
588
586
  return createResult(ResultCode.Ok, 'client.postImage', { id: res?.id });
589
587
  }
590
- const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : item.value;
588
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : Buffer.from(item.value, 'base64');
591
589
  const res = await client.postImage(event.ChannelId, {
592
590
  msg_id: event.MessageId,
593
- image: Buffer.isBuffer(file_data) ? file_data : Buffer.from(file_data)
591
+ image: file_data
594
592
  });
595
593
  return createResult(ResultCode.Ok, 'client.postImage', { id: res?.id });
596
594
  })).catch(err => [
@@ -659,10 +657,10 @@ const MESSAGE_CREATE = (client, event, val) => {
659
657
  });
660
658
  return createResult(ResultCode.Ok, 'client.postImage', { id: res?.id });
661
659
  }
662
- const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : item.value;
660
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : Buffer.from(item.value, 'base64');
663
661
  const res = await client.postImage(event.ChannelId, {
664
662
  msg_id: event.MessageId,
665
- image: Buffer.isBuffer(file_data) ? file_data : Buffer.from(file_data)
663
+ image: file_data
666
664
  });
667
665
  return createResult(ResultCode.Ok, 'client.postImage', { id: res?.id });
668
666
  })).catch(err => [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/qq-bot",
3
- "version": "2.1.0-alpha.2",
3
+ "version": "2.1.0-alpha.4",
4
4
  "description": "阿柠檬qqbot平台连接",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",