koishi-plugin-echo-cave 1.19.2 → 1.20.0

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.
@@ -10,5 +10,6 @@ export interface Config {
10
10
  maxFileSize?: number;
11
11
  maxRecordSize?: number;
12
12
  useBase64ForMedia?: boolean;
13
+ sendAllAsForwardMsg?: boolean;
13
14
  }
14
15
  export declare const Config: Schema<Config>;
package/lib/index.cjs CHANGED
@@ -42,7 +42,8 @@ var require_zh_CN = __commonJS({
42
42
  maxVideoSize: "\u6700\u5927\u89C6\u9891\u5927\u5C0F (MB)",
43
43
  maxFileSize: "\u6700\u5927\u6587\u4EF6\u5927\u5C0F (MB)",
44
44
  maxRecordSize: "\u6700\u5927\u5F55\u97F3\u5927\u5C0F (MB)",
45
- useBase64ForMedia: "\u662F\u5426\u4F7F\u7528 Base64 \u7F16\u7801\u53D1\u9001\u5A92\u4F53\u6587\u4EF6\uFF0C\u5F00\u542F\u540E\u5C06\u8BFB\u53D6 base64 \u7F16\u7801\u53D1\u9001\u800C\u4E0D\u662F\u4F7F\u7528 file uri"
45
+ useBase64ForMedia: "\u662F\u5426\u4F7F\u7528 Base64 \u7F16\u7801\u53D1\u9001\u5A92\u4F53\u6587\u4EF6\uFF0C\u5F00\u542F\u540E\u5C06\u8BFB\u53D6 base64 \u7F16\u7801\u53D1\u9001\u800C\u4E0D\u662F\u4F7F\u7528 file uri",
46
+ sendAllAsForwardMsg: "\u662F\u5426\u5C06\u6240\u6709\u6D88\u606F\u4EE5\u8F6C\u53D1\u6D88\u606F\u5F62\u5F0F\u53D1\u9001\uFF0C\u5F00\u542F\u540E\u666E\u901A\u6D88\u606F\u4E5F\u4F1A\u8F6C\u6362\u4E3A\u8F6C\u53D1\u6D88\u606F\u683C\u5F0F"
46
47
  };
47
48
  }
48
49
  });
@@ -350,6 +351,19 @@ async function convertFileUriToBase64(ctx, element) {
350
351
  ctx.logger.error(`Failed to convert ${element.type} to base64: ${err}`);
351
352
  return element;
352
353
  }
354
+ } else if (element.type === "node") {
355
+ const processedContent = await Promise.all(
356
+ element.data.content.map(async (contentElement) => {
357
+ return await convertFileUriToBase64(ctx, contentElement);
358
+ })
359
+ );
360
+ return {
361
+ ...element,
362
+ data: {
363
+ ...element.data,
364
+ content: processedContent
365
+ }
366
+ };
353
367
  }
354
368
  return element;
355
369
  }
@@ -420,23 +434,30 @@ async function checkAndCleanMediaFiles(ctx, cfg, type) {
420
434
  }
421
435
  }
422
436
  async function deleteMediaFilesFromMessage(ctx, content) {
437
+ async function processElement(element) {
438
+ if (element.type === "image" || element.type === "video" || element.type === "file" || element.type === "record") {
439
+ const fileUri = element.data?.file;
440
+ if (fileUri && fileUri.startsWith("file:///")) {
441
+ const filePath = decodeURIComponent(fileUri.replace("file:///", ""));
442
+ try {
443
+ await import_node_fs.promises.access(filePath);
444
+ await import_node_fs.promises.unlink(filePath);
445
+ ctx.logger.info(`Deleted media file: ${filePath}`);
446
+ } catch (err) {
447
+ ctx.logger.warn(`Failed to delete media file: ${filePath}, error: ${err}`);
448
+ }
449
+ }
450
+ } else if (element.type === "node" && element.data?.content) {
451
+ for (const contentElement of element.data.content) {
452
+ await processElement(contentElement);
453
+ }
454
+ }
455
+ }
423
456
  try {
424
457
  const elements = JSON.parse(content);
425
458
  const mediaElements = Array.isArray(elements) ? elements : [elements];
426
459
  for (const element of mediaElements) {
427
- if (element.type === "image" || element.type === "video" || element.type === "file" || element.type === "record") {
428
- const fileUri = element.data?.file;
429
- if (fileUri && fileUri.startsWith("file:///")) {
430
- const filePath = decodeURIComponent(fileUri.replace("file:///", ""));
431
- try {
432
- await import_node_fs.promises.access(filePath);
433
- await import_node_fs.promises.unlink(filePath);
434
- ctx.logger.info(`Deleted media file: ${filePath}`);
435
- } catch (err) {
436
- ctx.logger.warn(`Failed to delete media file: ${filePath}, error: ${err}`);
437
- }
438
- }
439
- }
460
+ await processElement(element);
440
461
  }
441
462
  } catch (err) {
442
463
  ctx.logger.error(`Failed to parse message content when deleting media: ${err}`);
@@ -688,7 +709,9 @@ async function sendCaveMsg(ctx, session, caveMsg, cfg) {
688
709
  nl: "\n"
689
710
  };
690
711
  const TEMPLATE_COUNT = 5;
691
- if (caveMsg.type === "forward") {
712
+ const isActualForward = content.some((item) => item.type === "node");
713
+ const shouldSendAsForward = cfg.sendAllAsForwardMsg || caveMsg.type === "forward" && isActualForward;
714
+ if (shouldSendAsForward) {
692
715
  const availableTemplates2 = [];
693
716
  for (let i = 0; i < TEMPLATE_COUNT; i++) {
694
717
  const template = session.text(`echo-cave.templates.forward.${i}`, templateData);
@@ -702,7 +725,21 @@ async function sendCaveMsg(ctx, session, caveMsg, cfg) {
702
725
  }
703
726
  const chosenTemplate2 = availableTemplates2[Math.floor(Math.random() * availableTemplates2.length)];
704
727
  await session.onebot.sendGroupMsg(channelId, [createTextMsg(chosenTemplate2)]);
705
- await session.onebot.sendGroupForwardMsg(channelId, content);
728
+ if (!isActualForward) {
729
+ const forwardContent = [
730
+ {
731
+ type: "node",
732
+ data: {
733
+ user_id: caveMsg.originUserId,
734
+ nickname: originName,
735
+ content
736
+ }
737
+ }
738
+ ];
739
+ await session.onebot.sendGroupForwardMsg(channelId, forwardContent);
740
+ } else {
741
+ await session.onebot.sendGroupForwardMsg(channelId, content);
742
+ }
706
743
  return;
707
744
  }
708
745
  const availableTemplates = [];
@@ -721,7 +758,7 @@ async function sendCaveMsg(ctx, session, caveMsg, cfg) {
721
758
  const last = content.at(-1);
722
759
  const needsNewline = last?.type === "text";
723
760
  content.unshift(createTextMsg(chosenTemplate.prefix + "\n"));
724
- content.push(createTextMsg(`${needsNewline ? "\n\n" : ""}${chosenTemplate.suffix}`));
761
+ content.push(createTextMsg(`${needsNewline ? "\n" : ""}${chosenTemplate.suffix}`));
725
762
  await session.onebot.sendGroupMsg(channelId, content);
726
763
  }
727
764
  function formatDate(date) {
@@ -879,7 +916,8 @@ var Config = import_koishi2.Schema.object({
879
916
  maxVideoSize: import_koishi2.Schema.number().default(512),
880
917
  maxFileSize: import_koishi2.Schema.number().default(512),
881
918
  maxRecordSize: import_koishi2.Schema.number().default(512),
882
- useBase64ForMedia: import_koishi2.Schema.boolean().default(false)
919
+ useBase64ForMedia: import_koishi2.Schema.boolean().default(false),
920
+ sendAllAsForwardMsg: import_koishi2.Schema.boolean().default(false)
883
921
  }).i18n({
884
922
  "zh-CN": require_zh_CN()
885
923
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-echo-cave",
3
3
  "description": "Group echo cave",
4
- "version": "1.19.2",
4
+ "version": "1.20.0",
5
5
  "main": "lib/index.cjs",
6
6
  "typings": "lib/index.d.ts",
7
7
  "type": "module",