koishi-plugin-best-cave 2.3.8 → 2.3.10

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.
@@ -1,6 +1,5 @@
1
1
  import { Context, Logger } from 'koishi';
2
2
  import { FileManager } from './FileManager';
3
- import { HashManager } from './HashManager';
4
3
  import { Config } from './index';
5
4
  /**
6
5
  * @class DataManager
@@ -11,16 +10,14 @@ export declare class DataManager {
11
10
  private config;
12
11
  private fileManager;
13
12
  private logger;
14
- private hashManager;
15
13
  /**
16
14
  * @constructor
17
15
  * @param ctx Koishi 上下文,用于数据库操作。
18
16
  * @param config 插件配置。
19
17
  * @param fileManager 文件管理器实例。
20
18
  * @param logger 日志记录器实例。
21
- * @param hashManager 哈希管理器实例,用于增量更新哈希。
22
19
  */
23
- constructor(ctx: Context, config: Config, fileManager: FileManager, logger: Logger, hashManager: HashManager | null);
20
+ constructor(ctx: Context, config: Config, fileManager: FileManager, logger: Logger);
24
21
  /**
25
22
  * @description 注册 `.export` 和 `.import` 子命令。
26
23
  * @param cave - 主 `cave` 命令实例。
package/lib/Utils.d.ts CHANGED
@@ -50,9 +50,11 @@ export declare function updateCooldownTimestamp(session: Session, config: Config
50
50
  * @param sourceElements 原始的 Koishi 消息元素数组。
51
51
  * @param newId 这条回声洞的新 ID。
52
52
  * @param session 触发操作的会话。
53
+ * @param config 插件配置。
54
+ * @param logger 日志实例。
53
55
  * @returns 包含数据库元素和待保存媒体列表的对象。
54
56
  */
55
- export declare function processMessageElements(sourceElements: h[], newId: number, session: Session): Promise<{
57
+ export declare function processMessageElements(sourceElements: h[], newId: number, session: Session, config: Config, logger: Logger): Promise<{
56
58
  finalElementsForDb: StoredElement[];
57
59
  mediaToSave: {
58
60
  sourceUrl: string;
package/lib/index.d.ts CHANGED
@@ -48,6 +48,7 @@ export interface Config {
48
48
  secretAccessKey?: string;
49
49
  bucket?: string;
50
50
  publicUrl?: string;
51
+ debug: boolean;
51
52
  }
52
53
  export declare const Config: Schema<Config>;
53
54
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -216,14 +216,12 @@ var DataManager = class {
216
216
  * @param config 插件配置。
217
217
  * @param fileManager 文件管理器实例。
218
218
  * @param logger 日志记录器实例。
219
- * @param hashManager 哈希管理器实例,用于增量更新哈希。
220
219
  */
221
- constructor(ctx, config, fileManager, logger2, hashManager) {
220
+ constructor(ctx, config, fileManager, logger2) {
222
221
  this.ctx = ctx;
223
222
  this.config = config;
224
223
  this.fileManager = fileManager;
225
224
  this.logger = logger2;
226
- this.hashManager = hashManager;
227
225
  }
228
226
  static {
229
227
  __name(this, "DataManager");
@@ -406,7 +404,7 @@ function updateCooldownTimestamp(session, config, lastUsed) {
406
404
  }
407
405
  }
408
406
  __name(updateCooldownTimestamp, "updateCooldownTimestamp");
409
- async function processMessageElements(sourceElements, newId, session) {
407
+ async function processMessageElements(sourceElements, newId, session, config, logger2) {
410
408
  const mediaToSave = [];
411
409
  let mediaIndex = 0;
412
410
  async function transform(elements) {
@@ -422,22 +420,33 @@ async function processMessageElements(sourceElements, newId, session) {
422
420
  if (type === "text" && el.attrs.content?.trim()) {
423
421
  result.push({ type: "text", content: el.attrs.content.trim() });
424
422
  } else if (type === "at" && el.attrs.id) {
423
+ if (config.debug) logger2.info(`发现 [at] 元素,ID: "${el.attrs.id}"`);
425
424
  result.push({ type: "at", content: el.attrs.id });
426
- } else if (type === "forward" && el.children?.length) {
427
- const transformedChildren = await transform(el.children);
428
- result.push({ type: "forward", content: JSON.stringify(transformedChildren) });
425
+ } else if (type === "forward" && Array.isArray(el.attrs.content)) {
426
+ if (config.debug) logger2.info(`发现 [forward] 元素,开始解析 el.attrs.content...`);
427
+ const allChildElements = [];
428
+ for (const node of el.attrs.content) {
429
+ const senderInfo = `${node.sender?.nickname || node.sender?.user_id}:`;
430
+ allChildElements.push({ type: "text", content: senderInfo });
431
+ if (node.message) {
432
+ const parsedMessage = import_koishi.h.parse(node.message);
433
+ const transformedMessage = await transform(parsedMessage);
434
+ allChildElements.push(...transformedMessage);
435
+ }
436
+ }
437
+ result.push({ type: "forward", content: JSON.stringify(allChildElements) });
429
438
  } else if (["image", "video", "audio", "file"].includes(type) && el.attrs.src) {
430
439
  let fileIdentifier = el.attrs.src;
440
+ if (config.debug) logger2.info(`发现 [${type}] 元素,src: "${fileIdentifier}"`);
431
441
  if (fileIdentifier.startsWith("http")) {
432
442
  const ext = path2.extname(el.attrs.file || "") || defaultExtMap[type];
433
443
  const currentMediaIndex = ++mediaIndex;
434
- const fileName = `${newId}_${currentMediaIndex}_${session.channelId || "private"}_${session.userId}${ext}`;
444
+ const fileName = `${newId}_${currentMediaIndex}_${session.channelId || session.guildId}_${session.userId}${ext}`;
445
+ if (config.debug) logger2.info(`[${type}] 是远程文件,已加入待保存列表。文件名: "${fileName}"`);
435
446
  mediaToSave.push({ sourceUrl: fileIdentifier, fileName });
436
447
  fileIdentifier = fileName;
437
448
  }
438
449
  result.push({ type, file: fileIdentifier });
439
- } else if (el.children) {
440
- result.push(...await transform(el.children));
441
450
  }
442
451
  }
443
452
  return result;
@@ -997,7 +1006,10 @@ var Config = import_koishi3.Schema.intersect([
997
1006
  region: import_koishi3.Schema.string().default("auto").description("区域 (Region)"),
998
1007
  accessKeyId: import_koishi3.Schema.string().description("Access Key ID").role("secret"),
999
1008
  secretAccessKey: import_koishi3.Schema.string().description("Secret Access Key").role("secret")
1000
- }).description("存储配置")
1009
+ }).description("存储配置"),
1010
+ import_koishi3.Schema.object({
1011
+ debug: import_koishi3.Schema.boolean().default(false).description("启用调试模式,将在控制台输出详细的操作日志。")
1012
+ }).description("开发配置")
1001
1013
  ]);
1002
1014
  function apply(ctx, config) {
1003
1015
  ctx.model.extend("cave", {
@@ -1015,7 +1027,7 @@ function apply(ctx, config) {
1015
1027
  const profileManager = config.enableName ? new NameManager(ctx) : null;
1016
1028
  const reviewManager = config.enablePend ? new PendManager(ctx, config, fileManager, logger, reusableIds) : null;
1017
1029
  const hashManager = config.enableSimilarity ? new HashManager(ctx, config, logger, fileManager) : null;
1018
- const dataManager = config.enableIO ? new DataManager(ctx, config, fileManager, logger, hashManager) : null;
1030
+ const dataManager = config.enableIO ? new DataManager(ctx, config, fileManager, logger) : null;
1019
1031
  const cave = ctx.command("cave", "回声洞").option("add", "-a <content:text> 添加回声洞").option("view", "-g <id:posint> 查看指定回声洞").option("delete", "-r <id:posint> 删除指定回声洞").option("list", "-l 查询投稿统计").usage("随机抽取一条已添加的回声洞。").action(async ({ session, options }) => {
1020
1032
  if (options.add) return session.execute(`cave.add ${options.add}`);
1021
1033
  if (options.view) return session.execute(`cave.view ${options.view}`);
@@ -1040,18 +1052,31 @@ function apply(ctx, config) {
1040
1052
  });
1041
1053
  cave.subcommand(".add [content:text]", "添加回声洞").usage("添加一条回声洞。可直接发送内容,也可回复或引用消息。").action(async ({ session }, content) => {
1042
1054
  try {
1043
- let sourceElements = session.quote?.elements;
1044
- if (!sourceElements && content?.trim()) {
1055
+ let sourceElements;
1056
+ if (session.quote?.elements) {
1057
+ sourceElements = session.quote.elements;
1058
+ } else if (content?.trim()) {
1045
1059
  sourceElements = import_koishi3.h.parse(content);
1046
- }
1047
- if (!sourceElements) {
1060
+ } else {
1048
1061
  await session.send("请在一分钟内发送你要添加的内容");
1049
1062
  const reply = await session.prompt(6e4);
1050
1063
  if (!reply) return "等待操作超时";
1051
1064
  sourceElements = import_koishi3.h.parse(reply);
1052
1065
  }
1066
+ if (config.debug) {
1067
+ logger.info(`获取到的消息内容 (sourceElements):
1068
+ ${JSON.stringify(sourceElements, null, 2)}`);
1069
+ logger.info(`完整的会话对象 (session):
1070
+ ${JSON.stringify(session, null, 2)}`);
1071
+ }
1053
1072
  const newId = await getNextCaveId(ctx, getScopeQuery(session, config, false), reusableIds);
1054
- const { finalElementsForDb, mediaToSave } = await processMessageElements(sourceElements, newId, session);
1073
+ const { finalElementsForDb, mediaToSave } = await processMessageElements(sourceElements, newId, session, config, logger);
1074
+ if (config.debug) {
1075
+ logger.info(`提取后数据库元素(finalElementsForDb):
1076
+ ${JSON.stringify(finalElementsForDb, null, 2)}`);
1077
+ logger.info(`提取后待存媒体(mediaToSave):
1078
+ ${JSON.stringify(mediaToSave, null, 2)}`);
1079
+ }
1055
1080
  if (finalElementsForDb.length === 0) return "无可添加内容";
1056
1081
  const textHashesToStore = [];
1057
1082
  if (hashManager) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-best-cave",
3
3
  "description": "功能强大、高度可定制的回声洞。支持丰富的媒体类型、内容查重、人工审核、用户昵称、数据迁移以及本地/S3 双重文件存储后端。",
4
- "version": "2.3.8",
4
+ "version": "2.3.10",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],