koishi-plugin-best-cave 2.0.4 → 2.0.5

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.
@@ -2,9 +2,8 @@ import { Context, Logger } from 'koishi';
2
2
  import { FileManager } from './FileManager';
3
3
  import { Config } from './index';
4
4
  /**
5
- * 数据管理器 (DataManager)
6
- * @description
7
- * 负责处理回声洞数据的导入和导出功能。
5
+ * @class DataManager
6
+ * @description 负责处理回声洞数据的导入和导出功能,提供数据迁移和备份的能力。
8
7
  */
9
8
  export declare class DataManager {
10
9
  private ctx;
@@ -12,21 +11,19 @@ export declare class DataManager {
12
11
  private fileManager;
13
12
  private logger;
14
13
  /**
15
- * 创建一个 DataManager 实例。
16
14
  * @param ctx - Koishi 上下文,用于数据库操作。
17
- * @param config - 插件配置。
15
+ * @param config - 插件配置对象。
18
16
  * @param fileManager - 文件管理器实例,用于读写导入/导出文件。
19
17
  * @param logger - 日志记录器实例。
20
18
  */
21
19
  constructor(ctx: Context, config: Config, fileManager: FileManager, logger: Logger);
22
20
  /**
23
21
  * 注册与数据导入导出相关的 `.export` 和 `.import` 子命令。
24
- * @param cave - 主 `cave` 命令的实例,用于挂载子命令。
22
+ * @param cave - 主 `cave` 命令实例,用于挂载子命令。
25
23
  */
26
24
  registerCommands(cave: any): void;
27
25
  /**
28
- * 导出所有状态为 'active' 的回声洞数据。
29
- * 数据将被序列化为 JSON 并保存到 `cave_export.json` 文件中。
26
+ * 导出所有状态为 'active' 的回声洞数据到 `cave_export.json` 文件。
30
27
  * @returns 一个描述导出结果的字符串消息。
31
28
  */
32
29
  exportData(): Promise<string>;
@@ -0,0 +1,59 @@
1
+ import { Logger } from 'koishi';
2
+ import { Config } from './index';
3
+ /**
4
+ * @class FileManager
5
+ * @description 对文件资源的存储、读取和删除操作。
6
+ * 内置了基于 Promise 的文件锁,以防止对本地文件的并发写入冲突。
7
+ */
8
+ export declare class FileManager {
9
+ private logger;
10
+ private resourceDir;
11
+ private locks;
12
+ private s3Client?;
13
+ private s3Bucket?;
14
+ /**
15
+ * @param baseDir - Koishi 应用的基础数据目录 (ctx.baseDir)。
16
+ * @param config - 插件的配置对象。
17
+ * @param logger - 日志记录器实例。
18
+ */
19
+ constructor(baseDir: string, config: Config, logger: Logger);
20
+ /**
21
+ * 确保本地资源目录存在,若不存在则以递归方式创建。
22
+ * @private
23
+ */
24
+ private ensureDirectory;
25
+ /**
26
+ * 获取给定文件名的完整本地路径。
27
+ * @param fileName - 文件名。
28
+ * @returns 文件的绝对路径。
29
+ * @private
30
+ */
31
+ private getFullPath;
32
+ /**
33
+ * 使用文件锁安全地执行一个异步文件操作,防止对同一文件的并发访问。
34
+ * @template T - 异步操作的返回类型。
35
+ * @param fileName - 需要加锁的文件名。
36
+ * @param operation - 要执行的异步函数。
37
+ * @returns 返回异步操作的结果。
38
+ * @private
39
+ */
40
+ private withLock;
41
+ /**
42
+ * 保存文件,自动路由到 S3 或本地存储。
43
+ * @param fileName - 文件名,将用作 S3 Key 或本地文件名。
44
+ * @param data - 要写入的 Buffer 数据。
45
+ * @returns 返回保存时使用的文件名。
46
+ */
47
+ saveFile(fileName: string, data: Buffer): Promise<string>;
48
+ /**
49
+ * 读取文件,自动从 S3 或本地存储获取。
50
+ * @param fileName - 要读取的文件名。
51
+ * @returns 文件的 Buffer 数据。
52
+ */
53
+ readFile(fileName: string): Promise<Buffer>;
54
+ /**
55
+ * 删除文件,自动从 S3 或本地存储删除。
56
+ * @param fileName - 要删除的文件名。
57
+ */
58
+ deleteFile(fileName: string): Promise<void>;
59
+ }
@@ -0,0 +1,50 @@
1
+ import { Context } from 'koishi';
2
+ /**
3
+ * @interface UserProfile
4
+ * @description 数据库中 `cave_user` 表的记录结构。
5
+ * @property {string} userId - 用户的唯一 ID,作为主键。
6
+ * @property {string} nickname - 用户在回声洞中显示的自定义昵称。
7
+ */
8
+ export interface UserProfile {
9
+ userId: string;
10
+ nickname: string;
11
+ }
12
+ declare module 'koishi' {
13
+ interface Tables {
14
+ cave_user: UserProfile;
15
+ }
16
+ }
17
+ /**
18
+ * @class ProfileManager
19
+ * @description 负责管理用户在回声洞插件中的自定义昵称。
20
+ * 提供设置、获取和清除昵称的数据库操作和相关命令。
21
+ */
22
+ export declare class ProfileManager {
23
+ private ctx;
24
+ /**
25
+ * @param ctx - Koishi 上下文,用于数据库交互。
26
+ */
27
+ constructor(ctx: Context);
28
+ /**
29
+ * 注册与用户昵称相关的 `.profile` 子命令。
30
+ * @param cave - 主 `cave` 命令实例,用于挂载子命令。
31
+ */
32
+ registerCommands(cave: any): void;
33
+ /**
34
+ * 设置或更新指定用户的昵称。
35
+ * @param userId - 目标用户的 ID。
36
+ * @param nickname - 要设置的新昵称。
37
+ */
38
+ setNickname(userId: string, nickname: string): Promise<void>;
39
+ /**
40
+ * 获取指定用户的昵称。
41
+ * @param userId - 目标用户的 ID。
42
+ * @returns 返回用户的昵称字符串,如果用户未设置则返回 null。
43
+ */
44
+ getNickname(userId: string): Promise<string | null>;
45
+ /**
46
+ * 清除指定用户的昵称设置。
47
+ * @param userId - 目标用户的 ID。
48
+ */
49
+ clearNickname(userId: string): Promise<void>;
50
+ }
@@ -0,0 +1,52 @@
1
+ import { Context, h, Logger } from 'koishi';
2
+ import { CaveObject, Config } from './index';
3
+ import { FileManager } from './FileManager';
4
+ /**
5
+ * @class ReviewManager
6
+ * @description 负责处理回声洞的审核流程。
7
+ * 此管理器将处理新回声洞的提交、向管理员发送审核通知,并响应管理员的审核操作。
8
+ */
9
+ export declare class ReviewManager {
10
+ private ctx;
11
+ private config;
12
+ private fileManager;
13
+ private logger;
14
+ /**
15
+ * @param ctx - Koishi 上下文。
16
+ * @param config - 插件配置对象。
17
+ * @param fileManager - 文件管理器实例。
18
+ * @param logger - 日志记录器实例。
19
+ */
20
+ constructor(ctx: Context, config: Config, fileManager: FileManager, logger: Logger);
21
+ /**
22
+ * 注册与审核相关的 `.review` 子命令。
23
+ * @param cave - 主 `cave` 命令实例,用于挂载子命令。
24
+ */
25
+ registerCommands(cave: any): void;
26
+ /**
27
+ * 列出所有待审核的回声洞。
28
+ * @private
29
+ */
30
+ private listPendingCaves;
31
+ /**
32
+ * 将一条新回声洞提交给管理员进行审核。
33
+ * 如果没有配置管理员,将自动通过审核。
34
+ * @param cave - 新创建的、状态为 'pending' 的回声洞对象。
35
+ */
36
+ sendForReview(cave: CaveObject): Promise<void>;
37
+ /**
38
+ * 构建用于发送给管理员的审核消息。
39
+ * @param cave - 待审核的回声洞对象。
40
+ * @returns 一个可直接发送的消息数组。
41
+ * @private
42
+ */
43
+ private buildReviewMessage;
44
+ /**
45
+ * 处理管理员的审核决定(通过或拒绝)。
46
+ * @param action - 'approve' (通过) 或 'reject' (拒绝)。
47
+ * @param cave - 被审核的回声洞对象。
48
+ * @param adminUserName - 执行操作的管理员昵称。
49
+ * @returns 返回给操作者的确认消息。
50
+ */
51
+ processReview(action: 'approve' | 'reject', cave: CaveObject, adminUserName: string): Promise<string | (string | h)[]>;
52
+ }
package/lib/Utils.d.ts CHANGED
@@ -2,93 +2,72 @@ import { Context, h, Logger, Session } from 'koishi';
2
2
  import { CaveObject, Config, StoredElement } from './index';
3
3
  import { FileManager } from './FileManager';
4
4
  /**
5
- * 将数据库中存储的 StoredElement[] 数组转换为 Koishi h() 元素数组。
6
- * @param elements - 从数据库读取的元素对象数组。
7
- * @returns 转换后的 h() 元素数组,用于消息发送。
5
+ * 将数据库存储格式的元素数组转换为 Koishi 消息元素 (h-element) 数组。
6
+ * @param elements - StoredElement 对象数组。
7
+ * @returns 可用于发送的 h-element 数组。
8
8
  */
9
9
  export declare function storedFormatToHElements(elements: StoredElement[]): h[];
10
10
  /**
11
- * 将指向本地媒体文件的 h() 元素转换为内联 Base64 格式。
12
- * @param element - 包含本地文件路径的 h() 媒体元素。
11
+ * 将指向本地媒体文件的 h-element 转换为内联 Base64 数据。
12
+ * @param element - 包含本地文件路径的媒体 h-element。
13
13
  * @param fileManager - FileManager 实例,用于读取文件。
14
- * @param logger - Logger 实例,用于记录错误。
15
- * @returns 转换后的 h() 元素,其 src 属性为 Base64 数据 URI
14
+ * @param logger - Logger 实例,用于记录日志。
15
+ * @returns 转换后的 h-element,其 src Base64 数据 URI,或在失败时返回一个文本提示。
16
16
  */
17
17
  export declare function mediaElementToBase64(element: h, fileManager: FileManager, logger: Logger): Promise<h>;
18
18
  /**
19
- * 构建一条包含回声洞内容的完整消息,准备发送。
20
- * 此函数会处理 S3 URL、文件映射路径或本地文件到 Base64 的转换。
19
+ * 根据配置构建一条包含回声洞内容的完整可发送消息。
21
20
  * @param cave - 要展示的回声洞对象。
22
- * @param config - 插件配置。
21
+ * @param config - 插件配置对象。
23
22
  * @param fileManager - FileManager 实例。
24
23
  * @param logger - Logger 实例。
25
- * @returns 一个包含 h() 元素和字符串的消息数组。
24
+ * @returns 一个包含字符串和 h-element 的消息数组。
26
25
  */
27
26
  export declare function buildCaveMessage(cave: CaveObject, config: Config, fileManager: FileManager, logger: Logger): Promise<(string | h)[]>;
28
27
  /**
29
- * 遍历消息元素,将其转换为可存储的格式,并识别需要下载的媒体文件。
30
- * @param sourceElements - 源消息中的 h() 元素数组。
28
+ * 遍历并转换消息元素为可存储格式,同时识别待下载的媒体文件。
29
+ * @param sourceElements - 源消息中的 h-element 数组。
31
30
  * @param newId - 新回声洞的 ID。
32
31
  * @param channelId - 频道 ID。
33
32
  * @param userId - 用户 ID。
34
- * @returns 包含待存储元素和待下载媒体列表的对象。
33
+ * @returns 一个包含待存储元素和待下载媒体列表的对象。
35
34
  */
36
- export declare function prepareElementsForStorage(sourceElements: h[], newId: number, channelId: string, userId: string): Promise<{
35
+ export declare function prepareElementsForStorage(sourceElements: h[], newId: number, channelId: string, userId: string): {
37
36
  finalElementsForDb: StoredElement[];
38
37
  mediaToDownload: {
39
38
  url: string;
40
39
  fileName: string;
41
40
  }[];
42
- }>;
41
+ };
43
42
  /**
44
- * 清理数据库中所有被标记为 'delete' 状态的回声洞及其关联的文件。
43
+ * 清理数据库中所有标记为 'delete' 状态的回声洞及其关联的文件。
45
44
  * @param ctx - Koishi 上下文。
46
45
  * @param fileManager - FileManager 实例,用于删除文件。
47
46
  * @param logger - Logger 实例。
48
47
  */
49
48
  export declare function cleanupPendingDeletions(ctx: Context, fileManager: FileManager, logger: Logger): Promise<void>;
50
49
  /**
51
- * 根据插件配置(是否分群)和当前会话,生成数据库查询所需的范围条件。
52
- * @param session - 当前会话对象。
53
- * @param config - 插件配置。
54
- * @returns 一个用于数据库查询的条件对象。
50
+ * 根据插件配置和会话上下文,生成数据库查询的作用域条件。
51
+ * @param session - 当前 Koishi 会话对象。
52
+ * @param config - 插件配置对象。
53
+ * @returns 一个用于数据库查询的条件对象,其类型已精确定义。
55
54
  */
56
- export declare function getScopeQuery(session: Session, config: Config): object;
55
+ export declare function getScopeQuery(session: Session, config: Config): {
56
+ status: 'active';
57
+ channelId?: string;
58
+ };
57
59
  /**
58
- * 获取下一个可用的回声洞 ID。
59
- * 策略是找到当前已存在的 ID 中最小的未使用正整数。
60
- * @param ctx - Koishi 上下文。
61
- * @param query - 查询回声洞的范围条件,用于分群模式。
62
- * @returns 一个可用的新 ID。
63
- * @performance 对于非常大的数据集,此函数可能会有性能瓶颈,因为它需要获取所有现有 ID。
64
- */
65
- export declare function getNextCaveId(ctx: Context, query?: object): Promise<number>;
66
- /**
67
- * 下载网络媒体资源并保存到文件存储中(本地或 S3)。
68
- * @param ctx - Koishi 上下文。
69
- * @param fileManager - FileManager 实例。
70
- * @param url - 媒体资源的 URL。
71
- * @param originalName - 原始文件名,用于获取扩展名。
72
- * @param type - 媒体类型 ('img', 'video', 'audio', 'file')。
73
- * @param caveId - 新建回声洞的 ID。
74
- * @param index - 媒体在消息中的索引。
75
- * @param channelId - 频道 ID。
76
- * @param userId - 用户 ID。
77
- * @returns 保存后的文件名/标识符。
78
- */
79
- export declare function downloadMedia(ctx: Context, fileManager: FileManager, url: string, originalName: string, type: string, caveId: number, index: number, channelId: string, userId: string): Promise<string>;
80
- /**
81
- * 检查用户在当前频道是否处于指令冷却状态。
82
- * @param session - 当前会话对象。
83
- * @param config - 插件配置。
60
+ * 检查用户在当前频道是否处于指令冷却中。
61
+ * @param session - 当前 Koishi 会话对象。
62
+ * @param config - 插件配置对象。
84
63
  * @param lastUsed - 存储各频道最后使用时间的 Map。
85
- * @returns 如果处于冷却中,返回提示信息字符串;否则返回 null。
64
+ * @returns 若处于冷却中,返回提示信息字符串;否则返回 null。
86
65
  */
87
66
  export declare function checkCooldown(session: Session, config: Config, lastUsed: Map<string, number>): string | null;
88
67
  /**
89
68
  * 更新指定频道的指令使用时间戳。
90
- * @param session - 当前会话对象。
91
- * @param config - 插件配置。
69
+ * @param session - 当前 Koishi 会话对象。
70
+ * @param config - 插件配置对象。
92
71
  * @param lastUsed - 存储各频道最后使用时间的 Map。
93
72
  */
94
73
  export declare function updateCooldownTimestamp(session: Session, config: Config, lastUsed: Map<string, number>): void;
package/lib/index.d.ts CHANGED
@@ -3,10 +3,11 @@ export declare const name = "best-cave";
3
3
  export declare const inject: string[];
4
4
  export declare const usage = "\n<div style=\"border-radius: 10px; border: 1px solid #ddd; padding: 16px; margin-bottom: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);\">\n <h2 style=\"margin-top: 0; color: #4a6ee0;\">\uD83D\uDCCC \u63D2\u4EF6\u8BF4\u660E</h2>\n <p>\uD83D\uDCD6 <strong>\u4F7F\u7528\u6587\u6863</strong>\uFF1A\u8BF7\u70B9\u51FB\u5DE6\u4E0A\u89D2\u7684 <strong>\u63D2\u4EF6\u4E3B\u9875</strong> \u67E5\u770B\u63D2\u4EF6\u4F7F\u7528\u6587\u6863</p>\n <p>\uD83D\uDD0D <strong>\u66F4\u591A\u63D2\u4EF6</strong>\uFF1A\u53EF\u8BBF\u95EE <a href=\"https://github.com/YisRime\" style=\"color:#4a6ee0;text-decoration:none;\">\u82E1\u6DDE\u7684 GitHub</a> \u67E5\u770B\u672C\u4EBA\u7684\u6240\u6709\u63D2\u4EF6</p>\n</div>\n\n<div style=\"border-radius: 10px; border: 1px solid #ddd; padding: 16px; margin-bottom: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);\">\n <h2 style=\"margin-top: 0; color: #e0574a;\">\u2764\uFE0F \u652F\u6301\u4E0E\u53CD\u9988</h2>\n <p>\uD83C\uDF1F \u559C\u6B22\u8FD9\u4E2A\u63D2\u4EF6\uFF1F\u8BF7\u5728 <a href=\"https://github.com/YisRime\" style=\"color:#e0574a;text-decoration:none;\">GitHub</a> \u4E0A\u7ED9\u6211\u4E00\u4E2A Star\uFF01</p>\n <p>\uD83D\uDC1B \u9047\u5230\u95EE\u9898\uFF1F\u8BF7\u901A\u8FC7 <strong>Issues</strong> \u63D0\u4EA4\u53CD\u9988\uFF0C\u6216\u52A0\u5165 QQ \u7FA4 <a href=\"https://qm.qq.com/q/PdLMx9Jowq\" style=\"color:#e0574a;text-decoration:none;\"><strong>855571375</strong></a> \u8FDB\u884C\u4EA4\u6D41</p>\n</div>\n";
5
5
  /**
6
- * 存储在数据库中的单个消息元素。
7
- * @property type - 元素类型。
6
+ * @interface StoredElement
7
+ * @description 单个消息元素的数据库存储格式。
8
+ * @property type - 元素类型: 'text', 'image', 'video', 'audio', 'file'。
8
9
  * @property content - 文本内容,仅用于 'text' 类型。
9
- * @property file - 文件标识符(本地文件名或 S3 Key),用于媒体类型。
10
+ * @property file - 文件标识符 (本地文件名或 S3 Key),用于媒体类型。
10
11
  */
11
12
  export interface StoredElement {
12
13
  type: 'text' | 'image' | 'video' | 'audio' | 'file';
@@ -14,14 +15,15 @@ export interface StoredElement {
14
15
  file?: string;
15
16
  }
16
17
  /**
17
- * 数据库中 `cave` 表的完整对象模型。
18
- * @property id - 回声洞的唯一数字 ID。
19
- * @property elements - 构成回声洞内容的元素数组。
18
+ * @interface CaveObject
19
+ * @description `cave` 数据表的完整对象模型。
20
+ * @property id - 回声洞的唯一数字 ID (主键)。
21
+ * @property elements - 构成回声洞内容的 StoredElement 数组。
20
22
  * @property channelId - 提交回声洞的频道 ID,若为私聊则为 null。
21
23
  * @property userId - 提交用户的 ID。
22
24
  * @property userName - 提交用户的昵称。
23
25
  * @property status - 回声洞状态: 'active' (活跃), 'delete' (待删除), 'pending' (待审核)。
24
- * @property time - 提交时间。
26
+ * @property time - 提交的时间戳。
25
27
  */
26
28
  export interface CaveObject {
27
29
  id: number;
@@ -37,9 +39,6 @@ declare module 'koishi' {
37
39
  cave: CaveObject;
38
40
  }
39
41
  }
40
- /**
41
- * 插件的配置接口。
42
- */
43
42
  export interface Config {
44
43
  coolDown: number;
45
44
  perChannel: boolean;
@@ -50,19 +49,16 @@ export interface Config {
50
49
  caveFormat: string;
51
50
  localPath?: string;
52
51
  enableS3: boolean;
52
+ publicUrl?: string;
53
53
  endpoint?: string;
54
+ bucket?: string;
54
55
  region?: string;
55
56
  accessKeyId?: string;
56
57
  secretAccessKey?: string;
57
- bucket?: string;
58
- publicUrl?: string;
59
58
  }
60
- /**
61
- * 使用 Koishi Schema 定义插件的配置项,用于生成配置界面。
62
- */
63
59
  export declare const Config: Schema<Config>;
64
60
  /**
65
- * 插件的入口函数。
61
+ * 插件主逻辑入口。
66
62
  * @param ctx - Koishi 上下文。
67
63
  * @param config - 用户提供的插件配置。
68
64
  */
package/lib/index.js CHANGED
@@ -45,9 +45,8 @@ var fs = __toESM(require("fs/promises"));
45
45
  var path = __toESM(require("path"));
46
46
  var FileManager = class {
47
47
  /**
48
- * 创建一个 FileManager 实例。
49
48
  * @param baseDir - Koishi 应用的基础数据目录 (ctx.baseDir)。
50
- * @param config - 插件的完整配置对象。
49
+ * @param config - 插件的配置对象。
51
50
  * @param logger - 日志记录器实例。
52
51
  */
53
52
  constructor(baseDir, config, logger2) {
@@ -68,24 +67,19 @@ var FileManager = class {
68
67
  static {
69
68
  __name(this, "FileManager");
70
69
  }
71
- // 本地资源存储目录的绝对路径。
72
70
  resourceDir;
73
- // 本地文件锁,键为文件绝对路径,值为一个 Promise,用于防止对同一文件的并发访问。
74
71
  locks = /* @__PURE__ */ new Map();
75
- // S3 客户端实例,仅在启用 S3 时初始化。
76
72
  s3Client;
77
- // S3 存储桶名称。
78
73
  s3Bucket;
79
74
  /**
80
- * 确保本地资源目录存在。如果目录不存在,则会递归创建。
81
- * 这是一个幂等操作。
75
+ * 确保本地资源目录存在,若不存在则以递归方式创建。
82
76
  * @private
83
77
  */
84
78
  async ensureDirectory() {
85
79
  try {
86
80
  await fs.mkdir(this.resourceDir, { recursive: true });
87
81
  } catch (error) {
88
- this.logger.error(`创建资源目录失败 ${this.resourceDir}:`, error);
82
+ this.logger.error(`Failed to create resource directory ${this.resourceDir}:`, error);
89
83
  throw error;
90
84
  }
91
85
  }
@@ -99,8 +93,7 @@ var FileManager = class {
99
93
  return path.join(this.resourceDir, fileName);
100
94
  }
101
95
  /**
102
- * 使用文件锁来安全地执行一个异步文件操作。
103
- * 这可以防止对同一文件的并发读写造成数据损坏。
96
+ * 使用文件锁安全地执行一个异步文件操作,防止对同一文件的并发访问。
104
97
  * @template T - 异步操作的返回类型。
105
98
  * @param fileName - 需要加锁的文件名。
106
99
  * @param operation - 要执行的异步函数。
@@ -110,19 +103,24 @@ var FileManager = class {
110
103
  async withLock(fileName, operation) {
111
104
  const fullPath = this.getFullPath(fileName);
112
105
  while (this.locks.has(fullPath)) {
113
- await this.locks.get(fullPath);
106
+ await this.locks.get(fullPath).catch(() => {
107
+ });
114
108
  }
115
- const promise = operation().finally(() => {
116
- this.locks.delete(fullPath);
117
- });
109
+ const promise = operation();
118
110
  this.locks.set(fullPath, promise);
119
- return promise;
111
+ try {
112
+ return await promise;
113
+ } finally {
114
+ if (this.locks.get(fullPath) === promise) {
115
+ this.locks.delete(fullPath);
116
+ }
117
+ }
120
118
  }
121
119
  /**
122
- * 保存文件,自动选择 S3 或本地存储。
123
- * @param fileName - 文件名,将用作 S3 中的 Key 或本地文件名。
120
+ * 保存文件,自动路由到 S3 或本地存储。
121
+ * @param fileName - 文件名,将用作 S3 Key 或本地文件名。
124
122
  * @param data - 要写入的 Buffer 数据。
125
- * @returns 返回保存时使用的文件名/标识符。
123
+ * @returns 返回保存时使用的文件名。
126
124
  */
127
125
  async saveFile(fileName, data) {
128
126
  if (this.s3Client) {
@@ -131,20 +129,18 @@ var FileManager = class {
131
129
  Key: fileName,
132
130
  Body: data,
133
131
  ACL: "public-read"
134
- // 默认将文件权限设置为公开可读,方便通过 URL 访问。
135
132
  });
136
133
  await this.s3Client.send(command);
137
- return fileName;
138
134
  } else {
139
135
  await this.ensureDirectory();
140
136
  const filePath = this.getFullPath(fileName);
141
137
  await this.withLock(fileName, () => fs.writeFile(filePath, data));
142
- return fileName;
143
138
  }
139
+ return fileName;
144
140
  }
145
141
  /**
146
- * 读取文件,自动从 S3 或本地存储读取。
147
- * @param fileName - 要读取的文件名/标识符。
142
+ * 读取文件,自动从 S3 或本地存储获取。
143
+ * @param fileName - 要读取的文件名。
148
144
  * @returns 文件的 Buffer 数据。
149
145
  */
150
146
  async readFile(fileName) {
@@ -162,26 +158,26 @@ var FileManager = class {
162
158
  }
163
159
  }
164
160
  /**
165
- * 删除文件,自动从 S3 或本地删除。
166
- * @param fileIdentifier - 要删除的文件名/标识符。
161
+ * 删除文件,自动从 S3 或本地存储删除。
162
+ * @param fileName - 要删除的文件名。
167
163
  */
168
- async deleteFile(fileIdentifier) {
164
+ async deleteFile(fileName) {
169
165
  if (this.s3Client) {
170
166
  const command = new import_client_s3.DeleteObjectCommand({
171
167
  Bucket: this.s3Bucket,
172
- Key: fileIdentifier
168
+ Key: fileName
173
169
  });
174
170
  await this.s3Client.send(command).catch((err) => {
175
- this.logger.warn(`删除文件 ${fileIdentifier} 失败:`, err);
171
+ this.logger.warn(`删除文件 ${fileName} 失败:`, err);
176
172
  });
177
173
  } else {
178
- const filePath = this.getFullPath(fileIdentifier);
179
- await this.withLock(fileIdentifier, async () => {
174
+ const filePath = this.getFullPath(fileName);
175
+ await this.withLock(fileName, async () => {
180
176
  try {
181
177
  await fs.unlink(filePath);
182
178
  } catch (error) {
183
179
  if (error.code !== "ENOENT") {
184
- this.logger.warn(`删除文件 ${filePath} 失败:`, error);
180
+ this.logger.warn(`删除文件 ${fileName} 失败:`, error);
185
181
  }
186
182
  }
187
183
  });
@@ -192,19 +188,15 @@ var FileManager = class {
192
188
  // src/ProfileManager.ts
193
189
  var ProfileManager = class {
194
190
  /**
195
- * 创建一个 ProfileManager 实例。
196
- * @param ctx - Koishi 上下文,用于初始化数据库模型。
191
+ * @param ctx - Koishi 上下文,用于数据库交互。
197
192
  */
198
193
  constructor(ctx) {
199
194
  this.ctx = ctx;
200
195
  this.ctx.model.extend("cave_user", {
201
196
  userId: "string",
202
- // 用户 ID
203
197
  nickname: "string"
204
- // 用户自定义昵称
205
198
  }, {
206
199
  primary: "userId"
207
- // 使用 userId 作为主键,确保每个用户只有一条昵称记录。
208
200
  });
209
201
  }
210
202
  static {
@@ -212,7 +204,7 @@ var ProfileManager = class {
212
204
  }
213
205
  /**
214
206
  * 注册与用户昵称相关的 `.profile` 子命令。
215
- * @param cave - 主 `cave` 命令的实例,用于挂载子命令。
207
+ * @param cave - 主 `cave` 命令实例,用于挂载子命令。
216
208
  */
217
209
  registerCommands(cave) {
218
210
  cave.subcommand(".profile [nickname:text]", "设置显示昵称").usage("设置你在回声洞中显示的昵称。不提供昵称则清除记录。").action(async ({ session }, nickname) => {
@@ -231,19 +223,16 @@ var ProfileManager = class {
231
223
  * @param nickname - 要设置的新昵称。
232
224
  */
233
225
  async setNickname(userId, nickname) {
234
- await this.ctx.database.upsert("cave_user", [{
235
- userId,
236
- nickname
237
- }]);
226
+ await this.ctx.database.upsert("cave_user", [{ userId, nickname }]);
238
227
  }
239
228
  /**
240
229
  * 获取指定用户的昵称。
241
230
  * @param userId - 目标用户的 ID。
242
- * @returns 返回用户的昵称字符串。如果用户未设置昵称,则返回 null。
231
+ * @returns 返回用户的昵称字符串,如果用户未设置则返回 null。
243
232
  */
244
233
  async getNickname(userId) {
245
- const profiles = await this.ctx.database.get("cave_user", { userId });
246
- return profiles[0]?.nickname || null;
234
+ const [profile] = await this.ctx.database.get("cave_user", { userId });
235
+ return profile?.nickname || null;
247
236
  }
248
237
  /**
249
238
  * 清除指定用户的昵称设置。
@@ -254,6 +243,102 @@ var ProfileManager = class {
254
243
  }
255
244
  };
256
245
 
246
+ // src/DataManager.ts
247
+ var DataManager = class {
248
+ /**
249
+ * @param ctx - Koishi 上下文,用于数据库操作。
250
+ * @param config - 插件配置对象。
251
+ * @param fileManager - 文件管理器实例,用于读写导入/导出文件。
252
+ * @param logger - 日志记录器实例。
253
+ */
254
+ constructor(ctx, config, fileManager, logger2) {
255
+ this.ctx = ctx;
256
+ this.config = config;
257
+ this.fileManager = fileManager;
258
+ this.logger = logger2;
259
+ }
260
+ static {
261
+ __name(this, "DataManager");
262
+ }
263
+ /**
264
+ * 注册与数据导入导出相关的 `.export` 和 `.import` 子命令。
265
+ * @param cave - 主 `cave` 命令实例,用于挂载子命令。
266
+ */
267
+ registerCommands(cave) {
268
+ cave.subcommand(".export", "导出回声洞数据").usage("将所有回声洞数据导出到 cave_export.json。").action(async ({ session }) => {
269
+ if (!this.config.adminUsers.includes(session.userId)) return "抱歉,你没有权限导出数据";
270
+ try {
271
+ await session.send("正在导出数据,请稍候...");
272
+ return await this.exportData();
273
+ } catch (error) {
274
+ this.logger.error("导出数据时发生错误:", error);
275
+ return `导出失败: ${error.message}`;
276
+ }
277
+ });
278
+ cave.subcommand(".import", "导入回声洞数据").usage("从 cave_import.json 中导入回声洞数据。").action(async ({ session }) => {
279
+ if (!this.config.adminUsers.includes(session.userId)) return "抱歉,你没有权限导入数据";
280
+ try {
281
+ await session.send("正在导入数据,请稍候...");
282
+ return await this.importData();
283
+ } catch (error) {
284
+ this.logger.error("导入数据时发生错误:", error);
285
+ return `导入失败: ${error.message}`;
286
+ }
287
+ });
288
+ }
289
+ /**
290
+ * 导出所有状态为 'active' 的回声洞数据到 `cave_export.json` 文件。
291
+ * @returns 一个描述导出结果的字符串消息。
292
+ */
293
+ async exportData() {
294
+ const fileName = "cave_export.json";
295
+ const cavesToExport = await this.ctx.database.get("cave", { status: "active" });
296
+ const portableCaves = cavesToExport.map(({ id, ...rest }) => rest);
297
+ const data = JSON.stringify(portableCaves, null, 2);
298
+ await this.fileManager.saveFile(fileName, Buffer.from(data));
299
+ return `成功导出 ${portableCaves.length} 条数据`;
300
+ }
301
+ /**
302
+ * 从 `cave_import.json` 文件导入回声洞数据。
303
+ * @returns 一个描述导入结果的字符串消息。
304
+ */
305
+ async importData() {
306
+ const fileName = "cave_import.json";
307
+ let importedData;
308
+ try {
309
+ const fileContent = await this.fileManager.readFile(fileName);
310
+ importedData = JSON.parse(fileContent.toString("utf-8"));
311
+ if (!Array.isArray(importedData)) {
312
+ throw new Error("导入文件格式非 JSON 数组");
313
+ }
314
+ } catch (error) {
315
+ return `读取导入文件失败: ${error.message}`;
316
+ }
317
+ const allCaves = await this.ctx.database.get("cave", {}, { fields: ["id"] });
318
+ const existingIds = new Set(allCaves.map((c) => c.id));
319
+ let nextId = 1;
320
+ const cavesToCreate = [];
321
+ for (const caveData of importedData) {
322
+ while (existingIds.has(nextId)) {
323
+ nextId++;
324
+ }
325
+ const newId = nextId;
326
+ const newCave = {
327
+ ...caveData,
328
+ id: newId,
329
+ channelId: caveData.channelId,
330
+ status: "active"
331
+ };
332
+ cavesToCreate.push(newCave);
333
+ existingIds.add(newId);
334
+ }
335
+ if (cavesToCreate.length > 0) {
336
+ await this.ctx.database.upsert("cave", cavesToCreate);
337
+ }
338
+ return `成功导入 ${cavesToCreate.length} 条回声洞数据`;
339
+ }
340
+ };
341
+
257
342
  // src/Utils.ts
258
343
  var import_koishi = require("koishi");
259
344
  var path2 = __toESM(require("path"));
@@ -301,49 +386,33 @@ async function buildCaveMessage(cave, config, fileManager, logger2) {
301
386
  const isMedia = ["image", "video", "audio", "file"].includes(element.type);
302
387
  const fileName = element.attrs.src;
303
388
  if (!isMedia || !fileName) {
304
- return Promise.resolve(element);
389
+ return element;
305
390
  }
306
391
  if (config.enableS3 && config.publicUrl) {
307
- const fullUrl = config.publicUrl.endsWith("/") ? `${config.publicUrl}${fileName}` : `${config.publicUrl}/${fileName}`;
308
- return Promise.resolve((0, import_koishi.h)(element.type, { ...element.attrs, src: fullUrl }));
392
+ const fullUrl = new URL(fileName, config.publicUrl.endsWith("/") ? config.publicUrl : `${config.publicUrl}/`).href;
393
+ return (0, import_koishi.h)(element.type, { ...element.attrs, src: fullUrl });
309
394
  }
310
395
  if (config.localPath) {
311
- const mappedPath = path2.join(config.localPath, fileName);
312
- const fileUri = `file://${mappedPath}`;
313
- return Promise.resolve((0, import_koishi.h)(element.type, { ...element.attrs, src: fileUri }));
396
+ const fileUri = `file://${path2.join(config.localPath, fileName)}`;
397
+ return (0, import_koishi.h)(element.type, { ...element.attrs, src: fileUri });
314
398
  }
315
399
  return mediaElementToBase64(element, fileManager, logger2);
316
400
  }));
317
401
  const finalMessage = [];
318
- const formatString = config.caveFormat;
319
- const separatorIndex = formatString.indexOf("|");
320
- let headerFormat, footerFormat;
321
- if (separatorIndex === -1) {
322
- headerFormat = formatString;
323
- footerFormat = "";
324
- } else {
325
- headerFormat = formatString.substring(0, separatorIndex);
326
- footerFormat = formatString.substring(separatorIndex + 1);
327
- }
328
- const headerText = headerFormat.replace("{id}", cave.id.toString()).replace("{name}", cave.userName);
329
- if (headerText.trim()) finalMessage.push(headerText);
402
+ const [headerFormat, footerFormat] = config.caveFormat.split("|");
403
+ const replacer = /* @__PURE__ */ __name((str) => str.replace("{id}", cave.id.toString()).replace("{name}", cave.userName), "replacer");
404
+ if (headerFormat?.trim()) finalMessage.push(replacer(headerFormat));
330
405
  finalMessage.push(...processedElements);
331
- const footerText = footerFormat.replace("{id}", cave.id.toString()).replace("{name}", cave.userName);
332
- if (footerText.trim()) finalMessage.push(footerText);
406
+ if (footerFormat?.trim()) finalMessage.push(replacer(footerFormat));
333
407
  return finalMessage;
334
408
  }
335
409
  __name(buildCaveMessage, "buildCaveMessage");
336
- async function prepareElementsForStorage(sourceElements, newId, channelId, userId) {
410
+ function prepareElementsForStorage(sourceElements, newId, channelId, userId) {
337
411
  const finalElementsForDb = [];
338
412
  const mediaToDownload = [];
339
413
  let mediaIndex = 0;
340
- const stack = [...sourceElements].reverse();
341
- while (stack.length > 0) {
342
- const el = stack.pop();
414
+ const processElement = /* @__PURE__ */ __name((el) => {
343
415
  const elementType = el.type;
344
- if (el.children) {
345
- stack.push(...[...el.children].reverse());
346
- }
347
416
  if (["image", "video", "audio", "file"].includes(elementType) && el.attrs.src) {
348
417
  const fileIdentifier = el.attrs.src;
349
418
  if (fileIdentifier.startsWith("http")) {
@@ -361,7 +430,11 @@ async function prepareElementsForStorage(sourceElements, newId, channelId, userI
361
430
  } else if (elementType === "text" && el.attrs.content?.trim()) {
362
431
  finalElementsForDb.push({ type: "text", content: el.attrs.content.trim() });
363
432
  }
364
- }
433
+ if (el.children) {
434
+ el.children.forEach(processElement);
435
+ }
436
+ }, "processElement");
437
+ sourceElements.forEach(processElement);
365
438
  return { finalElementsForDb, mediaToDownload };
366
439
  }
367
440
  __name(prepareElementsForStorage, "prepareElementsForStorage");
@@ -369,11 +442,12 @@ async function cleanupPendingDeletions(ctx, fileManager, logger2) {
369
442
  try {
370
443
  const cavesToDelete = await ctx.database.get("cave", { status: "delete" });
371
444
  if (cavesToDelete.length === 0) return;
372
- for (const cave of cavesToDelete) {
373
- const deletePromises = cave.elements.filter((el) => el.file).map((el) => fileManager.deleteFile(el.file));
374
- await Promise.all(deletePromises);
375
- await ctx.database.remove("cave", { id: cave.id });
376
- }
445
+ const filesToDelete = cavesToDelete.flatMap(
446
+ (cave) => cave.elements.filter((el) => el.file).map((el) => el.file)
447
+ );
448
+ await Promise.all(filesToDelete.map((file) => fileManager.deleteFile(file)));
449
+ const idsToRemove = cavesToDelete.map((cave) => cave.id);
450
+ await ctx.database.remove("cave", { id: { $in: idsToRemove } });
377
451
  } catch (error) {
378
452
  logger2.error("清理回声洞时发生错误:", error);
379
453
  }
@@ -387,16 +461,6 @@ function getScopeQuery(session, config) {
387
461
  return baseQuery;
388
462
  }
389
463
  __name(getScopeQuery, "getScopeQuery");
390
- async function getNextCaveId(ctx, query = {}) {
391
- const allCaves = await ctx.database.get("cave", query, { fields: ["id"] });
392
- const existingIds = new Set(allCaves.map((c) => c.id));
393
- let newId = 1;
394
- while (existingIds.has(newId)) {
395
- newId++;
396
- }
397
- return newId;
398
- }
399
- __name(getNextCaveId, "getNextCaveId");
400
464
  function checkCooldown(session, config, lastUsed) {
401
465
  if (config.coolDown <= 0 || !session.channelId || config.adminUsers.includes(session.userId)) {
402
466
  return null;
@@ -417,106 +481,13 @@ function updateCooldownTimestamp(session, config, lastUsed) {
417
481
  }
418
482
  __name(updateCooldownTimestamp, "updateCooldownTimestamp");
419
483
 
420
- // src/DataManager.ts
421
- var DataManager = class {
422
- /**
423
- * 创建一个 DataManager 实例。
424
- * @param ctx - Koishi 上下文,用于数据库操作。
425
- * @param config - 插件配置。
426
- * @param fileManager - 文件管理器实例,用于读写导入/导出文件。
427
- * @param logger - 日志记录器实例。
428
- */
429
- constructor(ctx, config, fileManager, logger2) {
430
- this.ctx = ctx;
431
- this.config = config;
432
- this.fileManager = fileManager;
433
- this.logger = logger2;
434
- }
435
- static {
436
- __name(this, "DataManager");
437
- }
438
- /**
439
- * 注册与数据导入导出相关的 `.export` 和 `.import` 子命令。
440
- * @param cave - 主 `cave` 命令的实例,用于挂载子命令。
441
- */
442
- registerCommands(cave) {
443
- cave.subcommand(".export", "导出回声洞数据").usage("将所有回声洞数据导出到 cave_export.json。").action(async ({ session }) => {
444
- if (!this.config.adminUsers.includes(session.userId)) return "抱歉,你没有权限导出数据";
445
- try {
446
- await session.send("正在导出数据,请稍候...");
447
- const resultMessage = await this.exportData();
448
- return resultMessage;
449
- } catch (error) {
450
- this.logger.error("导出数据时发生错误:", error);
451
- return `导出失败: ${error.message}`;
452
- }
453
- });
454
- cave.subcommand(".import", "导入回声洞数据").usage("从 cave_import.json 中导入回声洞数据。").action(async ({ session }) => {
455
- if (!this.config.adminUsers.includes(session.userId)) return "抱歉,你没有权限导入数据";
456
- try {
457
- await session.send("正在导入数据,请稍候...");
458
- const resultMessage = await this.importData();
459
- return resultMessage;
460
- } catch (error) {
461
- this.logger.error("导入数据时发生错误:", error);
462
- return `导入失败: ${error.message}`;
463
- }
464
- });
465
- }
466
- /**
467
- * 导出所有状态为 'active' 的回声洞数据。
468
- * 数据将被序列化为 JSON 并保存到 `cave_export.json` 文件中。
469
- * @returns 一个描述导出结果的字符串消息。
470
- */
471
- async exportData() {
472
- const fileName = "cave_export.json";
473
- const cavesToExport = await this.ctx.database.get("cave", { status: "active" });
474
- const portableCaves = cavesToExport.map(({ id, ...rest }) => rest);
475
- const data = JSON.stringify(portableCaves, null, 2);
476
- await this.fileManager.saveFile(fileName, Buffer.from(data));
477
- return `成功导出 ${portableCaves.length} 条数据`;
478
- }
479
- /**
480
- * 从 `cave_import.json` 文件导入回声洞数据。
481
- * @returns 一个描述导入结果的字符串消息。
482
- */
483
- async importData() {
484
- const fileName = "cave_import.json";
485
- let importedCaves;
486
- try {
487
- const fileContent = await this.fileManager.readFile(fileName);
488
- importedCaves = JSON.parse(fileContent.toString("utf-8"));
489
- if (!Array.isArray(importedCaves)) {
490
- throw new Error("导入文件格式无效");
491
- }
492
- } catch (error) {
493
- this.logger.error(`读取导入文件失败:`, error);
494
- return `读取导入文件失败: ${error.message || "未知错误"}`;
495
- }
496
- let successCount = 0;
497
- for (const cave of importedCaves) {
498
- const newId = await getNextCaveId(this.ctx, {});
499
- const newCave = {
500
- ...cave,
501
- id: newId,
502
- channelId: cave.channelId || null,
503
- // 确保 channelId 存在,若无则为 null。
504
- status: "active"
505
- // 导入的数据直接设为 active 状态。
506
- };
507
- await this.ctx.database.create("cave", newCave);
508
- successCount++;
509
- }
510
- return `成功导入 ${successCount} 条回声洞数据`;
511
- }
512
- };
513
-
514
484
  // src/ReviewManager.ts
485
+ var APPROVE_ACTIONS = /* @__PURE__ */ new Set(["y", "yes", "pass", "approve"]);
486
+ var REJECT_ACTIONS = /* @__PURE__ */ new Set(["n", "no", "deny", "reject"]);
515
487
  var ReviewManager = class {
516
488
  /**
517
- * 创建一个 ReviewManager 实例。
518
489
  * @param ctx - Koishi 上下文。
519
- * @param config - 插件配置。
490
+ * @param config - 插件配置对象。
520
491
  * @param fileManager - 文件管理器实例。
521
492
  * @param logger - 日志记录器实例。
522
493
  */
@@ -531,7 +502,7 @@ var ReviewManager = class {
531
502
  }
532
503
  /**
533
504
  * 注册与审核相关的 `.review` 子命令。
534
- * @param cave - 主 `cave` 命令的实例,用于挂载子命令。
505
+ * @param cave - 主 `cave` 命令实例,用于挂载子命令。
535
506
  */
536
507
  registerCommands(cave) {
537
508
  cave.subcommand(".review [id:posint] [action:string]", "审核回声洞").usage("查看或审核回声洞,使用 <Y/N> 进行审核。").action(async ({ session }, id, action) => {
@@ -539,44 +510,46 @@ var ReviewManager = class {
539
510
  return "抱歉,你没有权限执行审核";
540
511
  }
541
512
  if (!id) {
542
- const pendingCaves = await this.ctx.database.get("cave", { status: "pending" });
543
- if (pendingCaves.length === 0) {
544
- return "当前没有需要审核的回声洞";
545
- }
546
- const pendingIds = pendingCaves.map((c) => c.id).join(", ");
547
- return `当前共有 ${pendingCaves.length} 条待审核回声洞,序号为:
548
- ${pendingIds}`;
513
+ return this.listPendingCaves();
549
514
  }
550
515
  const [targetCave] = await this.ctx.database.get("cave", { id });
551
- if (!targetCave) {
552
- return `回声洞(${id})不存在`;
553
- }
554
- if (targetCave.status !== "pending") {
555
- return `回声洞(${id})无需审核`;
556
- }
557
- if (id && !action) {
516
+ if (!targetCave) return `回声洞(${id})不存在`;
517
+ if (targetCave.status !== "pending") return `回声洞(${id})无需审核`;
518
+ if (!action) {
558
519
  return this.buildReviewMessage(targetCave);
559
520
  }
560
521
  const normalizedAction = action.toLowerCase();
561
- let reviewAction;
562
- if (["y", "yes", "ok", "pass", "approve"].includes(normalizedAction)) {
563
- reviewAction = "approve";
564
- } else if (["n", "no", "deny", "reject"].includes(normalizedAction)) {
565
- reviewAction = "reject";
566
- } else {
567
- return `无效操作: "${action}"
568
- 请使用 "Y" (通过) 或 "N" (拒绝)`;
522
+ if (APPROVE_ACTIONS.has(normalizedAction)) {
523
+ return this.processReview("approve", targetCave, session.username);
524
+ }
525
+ if (REJECT_ACTIONS.has(normalizedAction)) {
526
+ return this.processReview("reject", targetCave, session.username);
569
527
  }
570
- return this.processReview(reviewAction, id, session.username);
528
+ return `无效操作: "${action}"
529
+ 请使用 "Y" (通过) 或 "N" (拒绝)`;
571
530
  });
572
531
  }
573
532
  /**
574
- * 将一条新的回声洞提交给所有管理员进行审核。
533
+ * 列出所有待审核的回声洞。
534
+ * @private
535
+ */
536
+ async listPendingCaves() {
537
+ const pendingCaves = await this.ctx.database.get("cave", { status: "pending" });
538
+ if (pendingCaves.length === 0) {
539
+ return "当前没有需要审核的回声洞";
540
+ }
541
+ const pendingIds = pendingCaves.map((c) => c.id).join(", ");
542
+ return `当前共有 ${pendingCaves.length} 条待审核回声洞,序号为:
543
+ ${pendingIds}`;
544
+ }
545
+ /**
546
+ * 将一条新回声洞提交给管理员进行审核。
547
+ * 如果没有配置管理员,将自动通过审核。
575
548
  * @param cave - 新创建的、状态为 'pending' 的回声洞对象。
576
549
  */
577
550
  async sendForReview(cave) {
578
551
  if (!this.config.adminUsers?.length) {
579
- this.logger.warn(`未配置管理员,回声洞(${cave.id})已自动通过审核`);
552
+ this.logger.warn(`No admin users configured. Cave ${cave.id} has been auto-approved.`);
580
553
  await this.ctx.database.upsert("cave", [{ id: cave.id, status: "active" }]);
581
554
  return;
582
555
  }
@@ -584,11 +557,11 @@ ${pendingIds}`;
584
557
  try {
585
558
  await this.ctx.broadcast(this.config.adminUsers, reviewMessage);
586
559
  } catch (error) {
587
- this.logger.error(`广播回声洞(${cave.id})审核请求失败:`, error);
560
+ this.logger.error(`Failed to broadcast review request for cave ${cave.id}:`, error);
588
561
  }
589
562
  }
590
563
  /**
591
- * 构建一条用于发送给管理员的、包含审核信息的消息。
564
+ * 构建用于发送给管理员的审核消息。
592
565
  * @param cave - 待审核的回声洞对象。
593
566
  * @returns 一个可直接发送的消息数组。
594
567
  * @private
@@ -600,32 +573,29 @@ ${pendingIds}`;
600
573
  /**
601
574
  * 处理管理员的审核决定(通过或拒绝)。
602
575
  * @param action - 'approve' (通过) 或 'reject' (拒绝)。
603
- * @param caveId - 被审核的回声洞 ID。
604
- * @param adminUserName - 执行操作的管理员的昵称。
576
+ * @param cave - 被审核的回声洞对象。
577
+ * @param adminUserName - 执行操作的管理员昵称。
605
578
  * @returns 返回给操作者的确认消息。
606
579
  */
607
- async processReview(action, caveId, adminUserName) {
608
- const [cave] = await this.ctx.database.get("cave", { id: caveId });
609
- if (!cave) return `回声洞(${caveId})不存在`;
610
- if (cave.status !== "pending") return `回声洞(${caveId})无需审核`;
580
+ async processReview(action, cave, adminUserName) {
611
581
  let resultMessage;
612
582
  let broadcastMessage;
613
583
  if (action === "approve") {
614
- await this.ctx.database.upsert("cave", [{ id: caveId, status: "active" }]);
615
- resultMessage = `回声洞(${caveId})已通过`;
616
- broadcastMessage = `回声洞(${caveId})已由管理员 "${adminUserName}" 通过`;
584
+ await this.ctx.database.upsert("cave", [{ id: cave.id, status: "active" }]);
585
+ resultMessage = `回声洞(${cave.id})已通过`;
586
+ broadcastMessage = `回声洞(${cave.id})已由管理员 "${adminUserName}" 通过`;
617
587
  } else {
618
- await this.ctx.database.upsert("cave", [{ id: caveId, status: "delete" }]);
619
- resultMessage = `回声洞(${caveId})已拒绝`;
588
+ await this.ctx.database.upsert("cave", [{ id: cave.id, status: "delete" }]);
589
+ resultMessage = `回声洞(${cave.id})已拒绝`;
620
590
  const caveContent = await buildCaveMessage(cave, this.config, this.fileManager, this.logger);
621
- broadcastMessage = [
622
- `回声洞(${caveId})已由管理员 "${adminUserName}" 拒绝`,
623
- ...caveContent
624
- ];
591
+ broadcastMessage = [`回声洞(${cave.id})已由管理员 "${adminUserName}" 拒绝`, ...caveContent];
592
+ cleanupPendingDeletions(this.ctx, this.fileManager, this.logger).catch((err) => {
593
+ this.logger.error(`Background cleanup failed for rejected cave ${cave.id}:`, err);
594
+ });
625
595
  }
626
- if (broadcastMessage && this.config.adminUsers?.length) {
627
- await this.ctx.broadcast(this.config.adminUsers, broadcastMessage).catch((err) => {
628
- this.logger.error(`广播回声洞(${cave.id})审核结果失败:`, err);
596
+ if (this.config.adminUsers?.length) {
597
+ this.ctx.broadcast(this.config.adminUsers, broadcastMessage).catch((err) => {
598
+ this.logger.error(`Failed to broadcast review result for cave ${cave.id}:`, err);
629
599
  });
630
600
  }
631
601
  return resultMessage;
@@ -675,22 +645,14 @@ var Config = import_koishi2.Schema.intersect([
675
645
  function apply(ctx, config) {
676
646
  ctx.model.extend("cave", {
677
647
  id: "unsigned",
678
- // 无符号整数,作为主键。
679
648
  elements: "json",
680
- // 存储为 JSON 字符串的元素数组。
681
649
  channelId: "string",
682
- // 频道 ID。
683
650
  userId: "string",
684
- // 用户 ID。
685
651
  userName: "string",
686
- // 用户昵称。
687
652
  status: "string",
688
- // 回声洞状态。
689
653
  time: "timestamp"
690
- // 提交时间。
691
654
  }, {
692
655
  primary: "id"
693
- // 将 'id' 字段设置为主键。
694
656
  });
695
657
  const fileManager = new FileManager(ctx.baseDir, config, logger);
696
658
  const lastUsed = /* @__PURE__ */ new Map();
@@ -712,32 +674,40 @@ function apply(ctx, config) {
712
674
  }
713
675
  const randomId = candidates[Math.floor(Math.random() * candidates.length)].id;
714
676
  const [randomCave] = await ctx.database.get("cave", { ...query, id: randomId });
715
- updateCooldownTimestamp(session, config, lastUsed);
716
- return buildCaveMessage(randomCave, config, fileManager, logger);
677
+ if (randomCave) {
678
+ updateCooldownTimestamp(session, config, lastUsed);
679
+ return buildCaveMessage(randomCave, config, fileManager, logger);
680
+ }
681
+ return "未能获取到回声洞";
717
682
  } catch (error) {
718
683
  logger.error("随机获取回声洞失败:", error);
719
- return "随机获取回声洞失败";
720
684
  }
721
685
  });
722
686
  cave.subcommand(".add [content:text]", "添加回声洞").usage("添加一条回声洞。可以直接发送内容,也可以回复或引用一条消息。").action(async ({ session }, content) => {
723
687
  try {
724
- let sourceElements;
725
- if (session.quote?.elements) {
726
- sourceElements = session.quote.elements;
727
- } else if (content?.trim()) {
728
- sourceElements = import_koishi2.h.parse(content);
729
- } else {
730
- await session.send("请在一分钟内发送你要添加的内容");
731
- const reply = await session.prompt(6e4);
732
- if (!reply) return "操作超时,已取消添加";
733
- sourceElements = import_koishi2.h.parse(reply);
688
+ let sourceElements = session.quote?.elements;
689
+ if (!sourceElements) {
690
+ const sourceText = content?.trim();
691
+ if (sourceText) {
692
+ sourceElements = import_koishi2.h.parse(sourceText);
693
+ } else {
694
+ await session.send("请在一分钟内发送你要添加的内容");
695
+ const reply = await session.prompt(6e4);
696
+ if (!reply) return "操作超时,已取消添加";
697
+ sourceElements = import_koishi2.h.parse(reply);
698
+ }
734
699
  }
735
700
  const scopeQuery = getScopeQuery(session, config);
736
- const newId = await getNextCaveId(ctx, scopeQuery);
737
- const { finalElementsForDb, mediaToDownload } = await prepareElementsForStorage(sourceElements, newId, session.channelId, session.userId);
701
+ const allCaves = await ctx.database.get("cave", scopeQuery, { fields: ["id"] });
702
+ const existingIds = new Set(allCaves.map((c) => c.id));
703
+ let newId = 1;
704
+ while (existingIds.has(newId)) {
705
+ newId++;
706
+ }
707
+ const { finalElementsForDb, mediaToDownload } = prepareElementsForStorage(sourceElements, newId, session.channelId, session.userId);
738
708
  if (finalElementsForDb.length === 0) return "内容为空,已取消添加";
739
709
  let userName = session.username;
740
- if (config.enableProfile) {
710
+ if (config.enableProfile && profileManager) {
741
711
  userName = await profileManager.getNickname(session.userId) || userName;
742
712
  }
743
713
  const newCave = {
@@ -758,9 +728,10 @@ function apply(ctx, config) {
758
728
  await Promise.all(downloadPromises);
759
729
  } catch (fileError) {
760
730
  await ctx.database.remove("cave", { id: newId });
761
- throw fileError;
731
+ logger.error("媒体文件存储失败:", fileError);
732
+ return "添加失败:媒体文件存储失败";
762
733
  }
763
- if (newCave.status === "pending") {
734
+ if (newCave.status === "pending" && reviewManager) {
764
735
  reviewManager.sendForReview(newCave);
765
736
  return `提交成功,序号为(${newCave.id})`;
766
737
  }
@@ -777,9 +748,7 @@ function apply(ctx, config) {
777
748
  try {
778
749
  const query = { ...getScopeQuery(session, config), id };
779
750
  const [targetCave] = await ctx.database.get("cave", query);
780
- if (!targetCave) {
781
- return `回声洞(${id})不存在`;
782
- }
751
+ if (!targetCave) return `回声洞(${id})不存在`;
783
752
  updateCooldownTimestamp(session, config, lastUsed);
784
753
  return buildCaveMessage(targetCave, config, fileManager, logger);
785
754
  } catch (error) {
@@ -800,9 +769,11 @@ function apply(ctx, config) {
800
769
  const caveMessage = await buildCaveMessage(targetCave, config, fileManager, logger);
801
770
  await ctx.database.upsert("cave", [{ id, status: "delete" }]);
802
771
  session.send([`已删除`, ...caveMessage]);
803
- cleanupPendingDeletions(ctx, fileManager, logger);
772
+ cleanupPendingDeletions(ctx, fileManager, logger).catch((err) => {
773
+ logger.error(`删除回声洞(${id})失败:`, err);
774
+ });
804
775
  } catch (error) {
805
- logger.error(`标记回声洞(${id})失败:`, error);
776
+ logger.error(`标记删除回声洞(${id})失败:`, error);
806
777
  return "删除失败,请稍后再试";
807
778
  }
808
779
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-best-cave",
3
3
  "description": "最强大的回声洞现已重构完成啦!注意数据格式需要使用脚本转换哦~",
4
- "version": "2.0.4",
4
+ "version": "2.0.5",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],