koishi-plugin-chat-analyse 0.2.1 → 0.2.2

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,6 +2,7 @@ import { Context } from 'koishi';
2
2
  declare module 'koishi' {
3
3
  interface Tables {
4
4
  analyse_msg: {
5
+ id: number;
5
6
  channelId: string;
6
7
  userId: string;
7
8
  type: string;
package/lib/index.js CHANGED
@@ -38,14 +38,16 @@ var Collector = class _Collector {
38
38
  constructor(ctx) {
39
39
  this.ctx = ctx;
40
40
  this.ctx.model.extend("analyse_msg", {
41
+ id: "unsigned",
41
42
  channelId: "string",
42
43
  userId: "string",
43
44
  type: "string",
44
45
  content: "text",
45
46
  timestamp: "timestamp"
46
47
  }, {
47
- primary: ["channelId", "userId", "timestamp"],
48
- indexes: ["type"]
48
+ primary: "id",
49
+ autoInc: true,
50
+ indexes: ["timestamp", "channelId", "userId", "type"]
49
51
  });
50
52
  this.ctx.model.extend("analyse_name", {
51
53
  channelId: "string",
@@ -67,15 +69,10 @@ var Collector = class _Collector {
67
69
  static {
68
70
  __name(this, "Collector");
69
71
  }
70
- // 每隔 1 分钟将缓冲区数据写入数据库
71
72
  static FLUSH_INTERVAL = 60 * 1e3;
72
- // 当缓冲区数据达到 100 条时,立即写入数据库
73
73
  static BUFFER_THRESHOLD = 100;
74
- // 消息数据写入缓冲区
75
74
  msgBuffer = [];
76
- // 定时器,用于周期性地清空缓冲区
77
75
  flushInterval;
78
- // 名称缓存,减少不必要的 API 请求 (key: 'channelId:userId')
79
76
  nameCache = /* @__PURE__ */ new Map();
80
77
  /**
81
78
  * 核心消息处理函数。
@@ -84,7 +81,7 @@ var Collector = class _Collector {
84
81
  async handleMessage(session) {
85
82
  const { userId, channelId, guildId, content, timestamp, argv, elements } = session;
86
83
  const effectiveId = channelId || guildId;
87
- if (!effectiveId || !userId) return;
84
+ if (!effectiveId || !userId || !timestamp) return;
88
85
  this.updateNameIfNeeded(session, effectiveId);
89
86
  const isCommand = !!argv?.command;
90
87
  const type = isCommand ? argv.command.name : this.summarizeElementTypes(elements);
@@ -135,9 +132,9 @@ var Collector = class _Collector {
135
132
  const bufferToFlush = this.msgBuffer;
136
133
  this.msgBuffer = [];
137
134
  try {
138
- await this.ctx.database.create("analyse_msg", bufferToFlush);
135
+ await this.ctx.database.upsert("analyse_msg", bufferToFlush);
139
136
  } catch (error) {
140
- this.ctx.logger.error("数据库写入失败:", error);
137
+ this.ctx.logger.error("数据写入失败:", error);
141
138
  this.msgBuffer.unshift(...bufferToFlush);
142
139
  }
143
140
  }
@@ -159,7 +156,7 @@ var Collector = class _Collector {
159
156
  const channelName = guild?.name;
160
157
  const userName = member?.nick || member?.name;
161
158
  if (!channelName || !userName) return;
162
- await this.ctx.database.upsert("analyse_name", [{
159
+ await this.ctx.database.upsert("analyse_name", () => [{
163
160
  channelId: effectiveId,
164
161
  channelName,
165
162
  userId,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chat-analyse",
3
3
  "description": "聊天记录分析",
4
- "version": "0.2.1",
4
+ "version": "0.2.2",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],