koishi-plugin-chat-analyse 0.0.1 → 0.0.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 +13 -5
- package/lib/index.js +45 -22
- package/package.json +1 -1
package/lib/collector.d.ts
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
import { Context } from 'koishi';
|
|
2
2
|
/**
|
|
3
3
|
* @file collector.ts
|
|
4
|
-
* @description
|
|
4
|
+
* @description 用于将收到的消息和命令分别持久化存储到数据库中。
|
|
5
5
|
*/
|
|
6
6
|
declare module 'koishi' {
|
|
7
7
|
interface Tables {
|
|
8
|
-
|
|
8
|
+
analyse_origin_msg: {
|
|
9
9
|
id: number;
|
|
10
10
|
channelId: string;
|
|
11
11
|
userId: string;
|
|
12
12
|
content: string;
|
|
13
13
|
timestamp: Date;
|
|
14
14
|
};
|
|
15
|
+
analyse_origin_cmd: {
|
|
16
|
+
id: number;
|
|
17
|
+
channelId: string;
|
|
18
|
+
userId: string;
|
|
19
|
+
command: string;
|
|
20
|
+
content: string;
|
|
21
|
+
timestamp: Date;
|
|
22
|
+
};
|
|
15
23
|
}
|
|
16
24
|
}
|
|
17
25
|
/**
|
|
18
|
-
* @class
|
|
19
|
-
* @description
|
|
26
|
+
* @class Collector
|
|
27
|
+
* @description 监听所有消息和命令,将消息内容存入 'analyse_origin_msg' 表,将命令存入 'analyse_origin_cmd' 表。
|
|
20
28
|
*/
|
|
21
|
-
export declare class
|
|
29
|
+
export declare class Collector {
|
|
22
30
|
private ctx;
|
|
23
31
|
/**
|
|
24
32
|
* @param ctx Koishi 的上下文对象,用于访问模型和事件系统
|
package/lib/index.js
CHANGED
|
@@ -29,13 +29,13 @@ module.exports = __toCommonJS(src_exports);
|
|
|
29
29
|
var import_koishi = require("koishi");
|
|
30
30
|
|
|
31
31
|
// src/collector.ts
|
|
32
|
-
var
|
|
32
|
+
var Collector = class {
|
|
33
33
|
/**
|
|
34
34
|
* @param ctx Koishi 的上下文对象,用于访问模型和事件系统
|
|
35
35
|
*/
|
|
36
36
|
constructor(ctx) {
|
|
37
37
|
this.ctx = ctx;
|
|
38
|
-
ctx.model.extend("
|
|
38
|
+
ctx.model.extend("analyse_origin_msg", {
|
|
39
39
|
id: "unsigned",
|
|
40
40
|
channelId: "string",
|
|
41
41
|
userId: "string",
|
|
@@ -44,29 +44,52 @@ var MessageCollector = class {
|
|
|
44
44
|
}, {
|
|
45
45
|
autoInc: true
|
|
46
46
|
});
|
|
47
|
-
ctx.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
content:
|
|
64
|
-
timestamp: new Date(session.timestamp)
|
|
47
|
+
ctx.model.extend("analyse_origin_cmd", {
|
|
48
|
+
id: "unsigned",
|
|
49
|
+
channelId: "string",
|
|
50
|
+
userId: "string",
|
|
51
|
+
command: "string",
|
|
52
|
+
content: "text",
|
|
53
|
+
timestamp: "timestamp"
|
|
54
|
+
}, {
|
|
55
|
+
autoInc: true
|
|
56
|
+
});
|
|
57
|
+
ctx.on("command/before-execute", async (argv) => {
|
|
58
|
+
if (!argv.command) return;
|
|
59
|
+
await ctx.database.create("analyse_origin_cmd", {
|
|
60
|
+
channelId: argv.session.channelId,
|
|
61
|
+
userId: argv.session.userId,
|
|
62
|
+
command: argv.command.name,
|
|
63
|
+
content: argv.session.content,
|
|
64
|
+
timestamp: new Date(argv.session.timestamp)
|
|
65
65
|
});
|
|
66
66
|
});
|
|
67
|
+
ctx.on("message", (session) => {
|
|
68
|
+
setTimeout(async () => {
|
|
69
|
+
if (session.argv) return;
|
|
70
|
+
const messageParts = session.elements.map((element) => {
|
|
71
|
+
switch (element.type) {
|
|
72
|
+
case "text":
|
|
73
|
+
return element.attrs.content;
|
|
74
|
+
case "at":
|
|
75
|
+
return `[at=${element.attrs.id}]`;
|
|
76
|
+
default:
|
|
77
|
+
return "";
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
const sanitizedContent = messageParts.join("").trim();
|
|
81
|
+
if (!sanitizedContent) return;
|
|
82
|
+
await ctx.database.create("analyse_origin_msg", {
|
|
83
|
+
channelId: session.channelId,
|
|
84
|
+
userId: session.userId,
|
|
85
|
+
content: sanitizedContent,
|
|
86
|
+
timestamp: new Date(session.timestamp)
|
|
87
|
+
});
|
|
88
|
+
}, 0);
|
|
89
|
+
});
|
|
67
90
|
}
|
|
68
91
|
static {
|
|
69
|
-
__name(this, "
|
|
92
|
+
__name(this, "Collector");
|
|
70
93
|
}
|
|
71
94
|
};
|
|
72
95
|
|
|
@@ -75,7 +98,7 @@ var name = "chat-analyse";
|
|
|
75
98
|
var Config = import_koishi.Schema.object({});
|
|
76
99
|
var using = ["database"];
|
|
77
100
|
function apply(ctx) {
|
|
78
|
-
new
|
|
101
|
+
new Collector(ctx);
|
|
79
102
|
}
|
|
80
103
|
__name(apply, "apply");
|
|
81
104
|
// Annotate the CommonJS export names for ESM import in node:
|