koishi-plugin-chat-analyse 0.1.0 → 0.1.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.
- package/lib/collector.d.ts +12 -2
- package/lib/index.js +54 -4
- package/package.json +1 -1
package/lib/collector.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Context } from 'koishi';
|
|
2
2
|
/**
|
|
3
3
|
* @file collector.ts
|
|
4
|
-
* @description
|
|
4
|
+
* @description 通过独立的事件监听器,分别持久化存储收到的普通消息和已确认的命令,并高效地维护ID与名称的映射。
|
|
5
5
|
*/
|
|
6
6
|
declare module 'koishi' {
|
|
7
7
|
interface Tables {
|
|
@@ -20,13 +20,23 @@ declare module 'koishi' {
|
|
|
20
20
|
content: string;
|
|
21
21
|
timestamp: Date;
|
|
22
22
|
};
|
|
23
|
+
analyse_name_map: {
|
|
24
|
+
id: number;
|
|
25
|
+
channelId: string;
|
|
26
|
+
channelName: string;
|
|
27
|
+
userId: string;
|
|
28
|
+
userName: string;
|
|
29
|
+
timestamp: Date;
|
|
30
|
+
};
|
|
23
31
|
}
|
|
24
32
|
}
|
|
25
33
|
/**
|
|
26
34
|
* @class Collector
|
|
27
|
-
* @description
|
|
35
|
+
* @description 使用双监听器,精确捕获并存储消息和命令,并维护一个ID到名称的映射表。
|
|
28
36
|
*/
|
|
29
37
|
export declare class Collector {
|
|
30
38
|
private ctx;
|
|
39
|
+
private nameCache;
|
|
31
40
|
constructor(ctx: Context);
|
|
41
|
+
private updateName;
|
|
32
42
|
}
|
package/lib/index.js
CHANGED
|
@@ -39,7 +39,10 @@ var Collector = class {
|
|
|
39
39
|
userId: "string",
|
|
40
40
|
content: "text",
|
|
41
41
|
timestamp: "timestamp"
|
|
42
|
-
}, {
|
|
42
|
+
}, {
|
|
43
|
+
autoInc: true,
|
|
44
|
+
indexes: ["channelId", "userId", "timestamp"]
|
|
45
|
+
});
|
|
43
46
|
ctx.model.extend("analyse_origin_cmd", {
|
|
44
47
|
id: "unsigned",
|
|
45
48
|
channelId: "string",
|
|
@@ -47,10 +50,26 @@ var Collector = class {
|
|
|
47
50
|
command: "string",
|
|
48
51
|
content: "text",
|
|
49
52
|
timestamp: "timestamp"
|
|
50
|
-
}, {
|
|
53
|
+
}, {
|
|
54
|
+
autoInc: true,
|
|
55
|
+
indexes: ["channelId", "userId", "command", "timestamp"]
|
|
56
|
+
});
|
|
57
|
+
ctx.model.extend("analyse_name_map", {
|
|
58
|
+
id: "unsigned",
|
|
59
|
+
channelId: "string",
|
|
60
|
+
channelName: "string",
|
|
61
|
+
userId: "string",
|
|
62
|
+
userName: "string",
|
|
63
|
+
timestamp: "timestamp"
|
|
64
|
+
}, {
|
|
65
|
+
autoInc: true,
|
|
66
|
+
unique: [["channelId", "userId"]]
|
|
67
|
+
});
|
|
51
68
|
ctx.on("command/before-execute", async (argv) => {
|
|
69
|
+
const effectiveId = argv.session.channelId || argv.session.guildId;
|
|
70
|
+
if (!effectiveId) return;
|
|
52
71
|
await ctx.database.create("analyse_origin_cmd", {
|
|
53
|
-
channelId:
|
|
72
|
+
channelId: effectiveId,
|
|
54
73
|
userId: argv.session.userId,
|
|
55
74
|
command: argv.command.name,
|
|
56
75
|
content: argv.session.content,
|
|
@@ -58,7 +77,27 @@ var Collector = class {
|
|
|
58
77
|
});
|
|
59
78
|
});
|
|
60
79
|
ctx.on("message", async (session) => {
|
|
80
|
+
const { userId, author } = session;
|
|
81
|
+
const effectiveId = session.channelId || session.guildId;
|
|
82
|
+
const { channelName, guildName } = session;
|
|
83
|
+
const effectiveName = channelName || guildName;
|
|
84
|
+
const currentName = author?.name || author?.nick;
|
|
85
|
+
if (currentName && effectiveName && effectiveId && userId) {
|
|
86
|
+
const cacheKey = `${effectiveId}:${userId}`;
|
|
87
|
+
const cachedName = this.nameCache.get(cacheKey);
|
|
88
|
+
if (cachedName && currentName !== cachedName) await this.updateName(effectiveId, effectiveName, userId, currentName);
|
|
89
|
+
else if (!cachedName) {
|
|
90
|
+
const dbRecord = await ctx.database.get("analyse_name_map", {
|
|
91
|
+
channelId: effectiveId,
|
|
92
|
+
userId
|
|
93
|
+
});
|
|
94
|
+
const dbName = dbRecord[0]?.userName;
|
|
95
|
+
if (!dbName || currentName !== dbName) await this.updateName(effectiveId, effectiveName, userId, currentName);
|
|
96
|
+
else this.nameCache.set(cacheKey, currentName);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
61
99
|
if (session.argv?.command) return;
|
|
100
|
+
if (!effectiveId) return;
|
|
62
101
|
const content = session.elements.map((element) => {
|
|
63
102
|
switch (element.type) {
|
|
64
103
|
case "text":
|
|
@@ -74,7 +113,7 @@ var Collector = class {
|
|
|
74
113
|
const sanitizedContent = content.trim();
|
|
75
114
|
if (!sanitizedContent) return;
|
|
76
115
|
await ctx.database.create("analyse_origin_msg", {
|
|
77
|
-
channelId:
|
|
116
|
+
channelId: effectiveId,
|
|
78
117
|
userId: session.userId,
|
|
79
118
|
content: sanitizedContent,
|
|
80
119
|
timestamp: new Date(session.timestamp)
|
|
@@ -84,6 +123,17 @@ var Collector = class {
|
|
|
84
123
|
static {
|
|
85
124
|
__name(this, "Collector");
|
|
86
125
|
}
|
|
126
|
+
nameCache = /* @__PURE__ */ new Map();
|
|
127
|
+
async updateName(channelId, channelName, userId, userName) {
|
|
128
|
+
await this.ctx.database.upsert("analyse_name_map", [{
|
|
129
|
+
channelId,
|
|
130
|
+
channelName,
|
|
131
|
+
userId,
|
|
132
|
+
userName,
|
|
133
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
134
|
+
}]);
|
|
135
|
+
this.nameCache.set(`${channelId}:${userId}`, userName);
|
|
136
|
+
}
|
|
87
137
|
};
|
|
88
138
|
|
|
89
139
|
// src/index.ts
|