koishi-plugin-chat-analyse 0.4.10 → 0.5.1

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.
@@ -59,7 +59,8 @@ export declare class Collector {
59
59
  private oriCacheBuffer;
60
60
  private whoAtBuffer;
61
61
  private userCache;
62
- private pendingUserRequests;
62
+ private channelCache;
63
+ private pendingRequests;
63
64
  private flushInterval;
64
65
  /**
65
66
  * @param ctx - Koishi 的插件上下文。
package/lib/index.js CHANGED
@@ -68,7 +68,7 @@ var Collector = class _Collector {
68
68
  /** @property FLUSH_INTERVAL - 内存缓存区定时刷入数据库的间隔(毫秒)。 */
69
69
  static FLUSH_INTERVAL = import_koishi.Time.minute;
70
70
  /** @property BUFFER_THRESHOLD - 内存缓存区触发刷新的消息数量阈值。 */
71
- static BUFFER_THRESHOLD = 100;
71
+ static BUFFER_THRESHOLD = 60;
72
72
  // 统一的数据缓冲区
73
73
  msgStatBuffer = /* @__PURE__ */ new Map();
74
74
  rankStatBuffer = /* @__PURE__ */ new Map();
@@ -76,7 +76,8 @@ var Collector = class _Collector {
76
76
  oriCacheBuffer = [];
77
77
  whoAtBuffer = [];
78
78
  userCache = /* @__PURE__ */ new Map();
79
- pendingUserRequests = /* @__PURE__ */ new Map();
79
+ channelCache = /* @__PURE__ */ new Map();
80
+ pendingRequests = /* @__PURE__ */ new Map();
80
81
  flushInterval;
81
82
  /**
82
83
  * @private @method onMessage
@@ -90,35 +91,41 @@ var Collector = class _Collector {
90
91
  let user;
91
92
  if (this.userCache.has(cacheKey)) {
92
93
  user = this.userCache.get(cacheKey);
93
- } else if (this.pendingUserRequests.has(cacheKey)) {
94
- user = await this.pendingUserRequests.get(cacheKey);
94
+ } else if (this.pendingRequests.has(cacheKey)) {
95
+ user = await this.pendingRequests.get(cacheKey);
95
96
  } else {
96
97
  const promise = (async () => {
97
98
  try {
98
99
  const [dbUser] = await this.ctx.database.get("analyse_user", { channelId, userId });
99
100
  const currentUserName = session.username ?? "";
100
- const guild = await bot.getGuild(channelId).catch(() => null);
101
- const currentChannelName = guild?.name ?? "";
101
+ let currentChannelName = this.channelCache.get(channelId);
102
+ if (currentChannelName === void 0) {
103
+ const guild = await bot.getGuild(channelId).catch(() => null);
104
+ currentChannelName = guild?.name ?? "";
105
+ if (currentChannelName) this.channelCache.set(channelId, currentChannelName);
106
+ }
102
107
  if (dbUser) {
103
108
  if (currentUserName && dbUser.userName !== currentUserName || currentChannelName && dbUser.channelName !== currentChannelName) {
104
109
  await this.ctx.database.set("analyse_user", { uid: dbUser.uid }, { userName: currentUserName, channelName: currentChannelName });
105
110
  dbUser.userName = currentUserName;
106
111
  dbUser.channelName = currentChannelName;
107
112
  }
108
- this.userCache.set(cacheKey, dbUser);
109
- return dbUser;
113
+ const cacheData2 = { uid: dbUser.uid, userName: dbUser.userName };
114
+ this.userCache.set(cacheKey, cacheData2);
115
+ return cacheData2;
110
116
  }
111
117
  const createdUser = await this.ctx.database.create("analyse_user", { channelId, userId, userName: currentUserName, channelName: currentChannelName });
112
- this.userCache.set(cacheKey, createdUser);
113
- return createdUser;
118
+ const cacheData = { uid: createdUser.uid, userName: createdUser.userName };
119
+ this.userCache.set(cacheKey, cacheData);
120
+ return cacheData;
114
121
  } catch (error) {
115
122
  this.ctx.logger.error(`创建或获取用户(${cacheKey})失败:`, error);
116
123
  return null;
117
124
  } finally {
118
- this.pendingUserRequests.delete(cacheKey);
125
+ this.pendingRequests.delete(cacheKey);
119
126
  }
120
127
  })();
121
- this.pendingUserRequests.set(cacheKey, promise);
128
+ this.pendingRequests.set(cacheKey, promise);
122
129
  user = await promise;
123
130
  }
124
131
  if (!user) return;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chat-analyse",
3
3
  "description": "聊天记录分析",
4
- "version": "0.4.10",
4
+ "version": "0.5.1",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],
package/lib/Data.d.ts DELETED
@@ -1,17 +0,0 @@
1
- import { Context, Command } from 'koishi';
2
- /**
3
- * @class Data
4
- * @description 提供数据备份、恢复和清理等高级管理功能。恢复逻辑采用分批处理以优化性能。
5
- */
6
- export declare class Data {
7
- private ctx;
8
- private dataDir;
9
- constructor(ctx: Context);
10
- /**
11
- * @public
12
- * @method registerCommands
13
- * @description 在 `analyse` 命令下注册所有数据管理相关的子命令 (`.backup`, `.restore`, `.clear`, `.list`)。
14
- * @param analyse - 主 `analyse` 命令实例。
15
- */
16
- registerCommands(analyse: Command): void;
17
- }
package/lib/Debug.d.ts DELETED
@@ -1,22 +0,0 @@
1
- import { Context, Command } from 'koishi';
2
- /**
3
- * @class Debug
4
- * @description
5
- * 提供一系列调试工具,用于数据维护和状态检查。
6
- * 包括手动补全用户信息、列出数据库中的频道和命令等功能。
7
- */
8
- export declare class Debug {
9
- private ctx;
10
- /**
11
- * @constructor
12
- * @param {Context} ctx - Koishi 的插件上下文。
13
- */
14
- constructor(ctx: Context);
15
- /**
16
- * @public
17
- * @method registerCommands
18
- * @description 在 'analyse' 命令下注册所有调试相关的子命令。
19
- * @param {Command} analyse - 主 'analyse' 命令实例。
20
- */
21
- registerCommands(analyse: Command): void;
22
- }
package/lib/Manager.d.ts DELETED
@@ -1,56 +0,0 @@
1
- import { Context, Command } from 'koishi';
2
- import { Config } from './index';
3
- /**
4
- * @class Manager
5
- * @description
6
- * 提供数据管理功能,包括导入、导出和清除插件产生的所有数据。
7
- * 这些命令通常需要较高的权限才能执行。
8
- */
9
- export declare class Manager {
10
- private ctx;
11
- private config;
12
- private readonly tableNames;
13
- private readonly userRelatableTables;
14
- /**
15
- * Manager 类的构造函数。
16
- * @param {Context} ctx - Koishi 的插件上下文。
17
- * @param {Config} config - 插件的配置对象。
18
- */
19
- constructor(ctx: Context, config: Config);
20
- /**
21
- * @public
22
- * @method registerCommands
23
- * @description 在主 `analyse` 命令下注册 `.import`, `.export`, 和 `.clear` 子命令。
24
- * @param {Command} analyse - 主 `analyse` 命令实例。
25
- */
26
- registerCommands(analyse: Command): void;
27
- /**
28
- * @private
29
- * @async
30
- * @method handleClearAll
31
- * @description 处理清空所有数据的逻辑,包括用户确认。
32
- */
33
- private handleClearAll;
34
- /**
35
- * @private
36
- * @async
37
- * @method clearSingleTable
38
- * @description 清空单个指定表的数据。
39
- */
40
- private clearSingleTable;
41
- /**
42
- * @private
43
- * @async
44
- * @method clearScopedData
45
- * @description 清除指定范围(用户/群组)的数据。
46
- */
47
- private clearScopedData;
48
- /**
49
- * @private
50
- * @async
51
- * @method clearAllData
52
- * @description 清空所有由本插件创建的表中的数据。
53
- * @returns {Promise<Record<string, number>>} 返回一个对象,键为表名,值为被删除的行数。
54
- */
55
- private clearAllData;
56
- }
package/lib/Renderer.d.ts DELETED
@@ -1,49 +0,0 @@
1
- import { Context } from 'koishi';
2
- /** 定义了渲染列表中单行数据的格式,是一个由字符串、数字或 `Date` 对象构成的数组。 */
3
- export type RenderListItem = (string | number | Date)[];
4
- /**
5
- * @interface ListRenderData
6
- * @description 定义了调用 `renderList` 方法所需的数据结构,包含了渲染一张完整列表图片所必需的所有信息。
7
- */
8
- export interface ListRenderData {
9
- title: string;
10
- time: Date;
11
- total?: string | number;
12
- list: RenderListItem[];
13
- }
14
- /**
15
- * @class Renderer
16
- * @description 负责将结构化的列表数据渲染为设计精美的 PNG 图片。其核心特性是能够动态计算内容尺寸,生成布局紧凑、自适应的图片。
17
- */
18
- export declare class Renderer {
19
- private ctx;
20
- /**
21
- * @param ctx - Koishi 的插件上下文,用于访问 `puppeteer` 等核心服务。
22
- */
23
- constructor(ctx: Context);
24
- /**
25
- * @private
26
- * @method htmlToImage
27
- * @description 将 HTML 字符串转换为 PNG 图片 Buffer。
28
- * @param html - 要渲染的 HTML 主体内容。
29
- * @returns 返回一个包含 PNG 图片数据的 Buffer。
30
- */
31
- private htmlToImage;
32
- /**
33
- * @private
34
- * @method formatDate
35
- * @description 将 `Date` 对象格式化为易于理解的相对时间或绝对日期字符串。
36
- * @param date - 需要格式化的日期对象。
37
- * @returns 格式化后的时间字符串。
38
- */
39
- private formatDate;
40
- /**
41
- * @public
42
- * @method renderList
43
- * @description 构建并渲染一个包含标题、统计信息和数据表格的 HTML 卡片为图片。
44
- * @param data - 包含渲染所需全部信息的对象。
45
- * @param headers - (可选) 表格的表头字符串数组。
46
- * @returns 成功时返回包含 PNG 图片的 Buffer,若列表为空则返回提示字符串。
47
- */
48
- renderList(data: ListRenderData, headers?: string[]): Promise<string | Buffer>;
49
- }
package/lib/Stat.d.ts DELETED
@@ -1,37 +0,0 @@
1
- import { Context, Command } from 'koishi';
2
- import { Renderer } from './Renderer';
3
- import { Config } from './index';
4
- /**
5
- * @class Stat
6
- * @description 提供统一的统计查询服务。负责注册查询命令,从数据库获取数据,并调用渲染器生成图表。
7
- */
8
- export declare class Stat {
9
- private ctx;
10
- private config;
11
- renderer: Renderer;
12
- /**
13
- * @param ctx - Koishi 的插件上下文。
14
- * @param config - 插件的配置对象。
15
- */
16
- constructor(ctx: Context, config: Config);
17
- /**
18
- * @public @method registerCommands
19
- * @description 根据配置,动态地将 `.cmd`, `.msg`, `.rank` 子命令注册到主 `analyse` 命令下。
20
- * @param analyse - 主 `analyse` 命令实例。
21
- */
22
- registerCommands(analyse: Command): void;
23
- /**
24
- * @private @method parseQueryScope
25
- * @description 解析命令选项,转换为包含 UIDs 和描述性信息的统一查询范围对象。
26
- * @param session - 当前会话对象。
27
- * @param options - 命令选项。
28
- * @returns 包含 uids、错误或范围描述的查询范围对象。
29
- */
30
- private parseQueryScope;
31
- /**
32
- * @private @method generateTitle
33
- * @description 根据查询范围和类型动态生成易于理解的图片标题。
34
- * @returns 生成的标题字符串。
35
- */
36
- private generateTitle;
37
- }
package/lib/WhoAt.d.ts DELETED
@@ -1,21 +0,0 @@
1
- import { Context, Command } from 'koishi';
2
- import { Config } from './index';
3
- /**
4
- * @class WhoAt
5
- * @description 负责处理“谁@我”相关功能,包括查询和定时清理。
6
- */
7
- export declare class WhoAt {
8
- private ctx;
9
- private config;
10
- /**
11
- * @param ctx - Koishi 的插件上下文。
12
- * @param config - 插件的配置对象。
13
- */
14
- constructor(ctx: Context, config: Config);
15
- /**
16
- * @public @method registerCommand
17
- * @description 在主 `analyse` 命令下注册 `whoatme` 子命令。
18
- * @param analyse - 主 `analyse` 命令实例。
19
- */
20
- registerCommand(analyse: Command): void;
21
- }
package/lib/index.d.ts DELETED
@@ -1,29 +0,0 @@
1
- import { Context, Schema } from 'koishi';
2
- /** @name 插件使用说明 */
3
- 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<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";
4
- export declare const name = "chat-analyse";
5
- export declare const using: string[];
6
- /**
7
- * @interface Config
8
- * @description 定义插件的配置项结构。
9
- */
10
- export interface Config {
11
- enableListener: boolean;
12
- enableCmdStat: boolean;
13
- enableMsgStat: boolean;
14
- enableRankStat: boolean;
15
- enableOriRecord: boolean;
16
- enableWhoAt: boolean;
17
- enableData: boolean;
18
- atRetentionDays: number;
19
- rankRetentionDays: number;
20
- }
21
- /** @description 插件的配置项定义,使用 Koishi Schema 构建。 */
22
- export declare const Config: Schema<Config>;
23
- /**
24
- * @function apply
25
- * @description Koishi 插件的主入口函数,负责初始化和注册所有功能模块。
26
- * @param ctx - Koishi 的插件上下文。
27
- * @param config - 用户配置对象。
28
- */
29
- export declare function apply(ctx: Context, config: Config): void;