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.
@@ -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
- chat_origin: {
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 MessageCollector
19
- * @description 监听所有消息,将其内容序列化后存入名为 'chat_origin' 的数据库表中。
26
+ * @class Collector
27
+ * @description 监听所有消息和命令,将消息内容存入 'analyse_origin_msg' 表,将命令存入 'analyse_origin_cmd' 表。
20
28
  */
21
- export declare class MessageCollector {
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 MessageCollector = class {
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("chat_origin", {
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.on("message", async (session) => {
48
- const messageParts = session.elements.map((element) => {
49
- switch (element.type) {
50
- case "text":
51
- return element.attrs.content;
52
- case "at":
53
- return `[at=${element.attrs.id}]`;
54
- default:
55
- return "";
56
- }
57
- });
58
- const sanitizedContent = messageParts.join("").trim();
59
- if (!sanitizedContent) return;
60
- await ctx.database.create("chat_origin", {
61
- channelId: session.channelId,
62
- userId: session.userId,
63
- content: sanitizedContent,
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, "MessageCollector");
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 MessageCollector(ctx);
101
+ new Collector(ctx);
79
102
  }
80
103
  __name(apply, "apply");
81
104
  // Annotate the CommonJS export names for ESM import in node:
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chat-analyse",
3
3
  "description": "聊天记录分析",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],