koishi-plugin-chat-analyse 0.0.2 → 0.0.3

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.
@@ -1,7 +1,7 @@
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 {
@@ -24,12 +24,9 @@ declare module 'koishi' {
24
24
  }
25
25
  /**
26
26
  * @class Collector
27
- * @description 监听所有消息和命令,将消息内容存入 'analyse_origin_msg' 表,将命令存入 'analyse_origin_cmd' 表。
27
+ * @description 使用双监听器,精确捕获并存储消息和命令。
28
28
  */
29
29
  export declare class Collector {
30
30
  private ctx;
31
- /**
32
- * @param ctx Koishi 的上下文对象,用于访问模型和事件系统
33
- */
34
31
  constructor(ctx: Context);
35
32
  }
package/lib/index.js CHANGED
@@ -30,9 +30,6 @@ var import_koishi = require("koishi");
30
30
 
31
31
  // src/collector.ts
32
32
  var Collector = class {
33
- /**
34
- * @param ctx Koishi 的上下文对象,用于访问模型和事件系统
35
- */
36
33
  constructor(ctx) {
37
34
  this.ctx = ctx;
38
35
  ctx.model.extend("analyse_origin_msg", {
@@ -41,9 +38,7 @@ var Collector = class {
41
38
  userId: "string",
42
39
  content: "text",
43
40
  timestamp: "timestamp"
44
- }, {
45
- autoInc: true
46
- });
41
+ }, { autoInc: true });
47
42
  ctx.model.extend("analyse_origin_cmd", {
48
43
  id: "unsigned",
49
44
  channelId: "string",
@@ -51,11 +46,8 @@ var Collector = class {
51
46
  command: "string",
52
47
  content: "text",
53
48
  timestamp: "timestamp"
54
- }, {
55
- autoInc: true
56
- });
49
+ }, { autoInc: true });
57
50
  ctx.on("command/before-execute", async (argv) => {
58
- if (!argv.command) return;
59
51
  await ctx.database.create("analyse_origin_cmd", {
60
52
  channelId: argv.session.channelId,
61
53
  userId: argv.session.userId,
@@ -64,28 +56,26 @@ var Collector = class {
64
56
  timestamp: new Date(argv.session.timestamp)
65
57
  });
66
58
  });
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);
59
+ ctx.on("message", async (session) => {
60
+ if (session.argv?.command) return;
61
+ const content = session.elements.map((element) => {
62
+ switch (element.type) {
63
+ case "text":
64
+ return element.attrs.content;
65
+ case "img":
66
+ return element.attrs.summary === "[动画表情]" ? "[gif]" : `[${element.type}]`;
67
+ default:
68
+ return `[${element.type}]`;
69
+ }
70
+ }).join("");
71
+ const sanitizedContent = content.trim();
72
+ if (!sanitizedContent) return;
73
+ await ctx.database.create("analyse_origin_msg", {
74
+ channelId: session.channelId,
75
+ userId: session.userId,
76
+ content: sanitizedContent,
77
+ timestamp: new Date(session.timestamp)
78
+ });
89
79
  });
90
80
  }
91
81
  static {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chat-analyse",
3
3
  "description": "聊天记录分析",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],