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