koishi-plugin-chat-analyse 0.0.3 → 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.
@@ -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.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Context, Schema } from 'koishi';
2
+ export declare const usage = "\n<div style=\"border-radius: 10px; border: 1px solid #ddd; padding: 16px; margin-bottom: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);\">\n <h2 style=\"margin-top: 0; color: #4a6ee0;\">\uD83D\uDCCC \u63D2\u4EF6\u8BF4\u660E</h2>\n <p>\uD83D\uDCD6 <strong>\u4F7F\u7528\u6587\u6863</strong>\uFF1A\u8BF7\u70B9\u51FB\u5DE6\u4E0A\u89D2\u7684 <strong>\u63D2\u4EF6\u4E3B\u9875</strong> \u67E5\u770B\u63D2\u4EF6\u4F7F\u7528\u6587\u6863</p>\n <p>\uD83D\uDD0D <strong>\u66F4\u591A\u63D2\u4EF6</strong>\uFF1A\u53EF\u8BBF\u95EE <a href=\"https://github.com/YisRime\" style=\"color:#4a6ee0;text-decoration:none;\">\u82E1\u6DDE\u7684 GitHub</a> \u67E5\u770B\u672C\u4EBA\u7684\u6240\u6709\u63D2\u4EF6</p>\n</div>\n<div style=\"border-radius: 10px; border: 1px solid #ddd; padding: 16px; margin-bottom: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);\">\n <h2 style=\"margin-top: 0; color: #e0574a;\">\u2764\uFE0F \u652F\u6301\u4E0E\u53CD\u9988</h2>\n <p>\uD83C\uDF1F \u559C\u6B22\u8FD9\u4E2A\u63D2\u4EF6\uFF1F\u8BF7\u5728 <a href=\"https://github.com/YisRime\" style=\"color:#e0574a;text-decoration:none;\">GitHub</a> \u4E0A\u7ED9\u6211\u4E00\u4E2A Star\uFF01</p>\n <p>\uD83D\uDC1B \u9047\u5230\u95EE\u9898\uFF1F\u8BF7\u901A\u8FC7 <strong>Issues</strong> \u63D0\u4EA4\u53CD\u9988\uFF0C\u6216\u52A0\u5165 QQ \u7FA4 <a href=\"https://qm.qq.com/q/PdLMx9Jowq\" style=\"color:#e0574a;text-decoration:none;\"><strong>855571375</strong></a> \u8FDB\u884C\u4EA4\u6D41</p>\n</div>\n";
2
3
  export declare const name = "chat-analyse";
3
4
  export interface Config {
4
5
  }
package/lib/index.js CHANGED
@@ -23,6 +23,7 @@ __export(src_exports, {
23
23
  Config: () => Config,
24
24
  apply: () => apply,
25
25
  name: () => name,
26
+ usage: () => usage,
26
27
  using: () => using
27
28
  });
28
29
  module.exports = __toCommonJS(src_exports);
@@ -38,7 +39,10 @@ var Collector = class {
38
39
  userId: "string",
39
40
  content: "text",
40
41
  timestamp: "timestamp"
41
- }, { autoInc: true });
42
+ }, {
43
+ autoInc: true,
44
+ indexes: ["channelId", "userId", "timestamp"]
45
+ });
42
46
  ctx.model.extend("analyse_origin_cmd", {
43
47
  id: "unsigned",
44
48
  channelId: "string",
@@ -46,10 +50,26 @@ var Collector = class {
46
50
  command: "string",
47
51
  content: "text",
48
52
  timestamp: "timestamp"
49
- }, { autoInc: true });
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
+ });
50
68
  ctx.on("command/before-execute", async (argv) => {
69
+ const effectiveId = argv.session.channelId || argv.session.guildId;
70
+ if (!effectiveId) return;
51
71
  await ctx.database.create("analyse_origin_cmd", {
52
- channelId: argv.session.channelId,
72
+ channelId: effectiveId,
53
73
  userId: argv.session.userId,
54
74
  command: argv.command.name,
55
75
  content: argv.session.content,
@@ -57,13 +77,35 @@ var Collector = class {
57
77
  });
58
78
  });
59
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
+ }
60
99
  if (session.argv?.command) return;
100
+ if (!effectiveId) return;
61
101
  const content = session.elements.map((element) => {
62
102
  switch (element.type) {
63
103
  case "text":
64
104
  return element.attrs.content;
65
105
  case "img":
66
106
  return element.attrs.summary === "[动画表情]" ? "[gif]" : `[${element.type}]`;
107
+ case "at":
108
+ return `[at:${element.attrs.id}]`;
67
109
  default:
68
110
  return `[${element.type}]`;
69
111
  }
@@ -71,7 +113,7 @@ var Collector = class {
71
113
  const sanitizedContent = content.trim();
72
114
  if (!sanitizedContent) return;
73
115
  await ctx.database.create("analyse_origin_msg", {
74
- channelId: session.channelId,
116
+ channelId: effectiveId,
75
117
  userId: session.userId,
76
118
  content: sanitizedContent,
77
119
  timestamp: new Date(session.timestamp)
@@ -81,9 +123,32 @@ var Collector = class {
81
123
  static {
82
124
  __name(this, "Collector");
83
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
+ }
84
137
  };
85
138
 
86
139
  // src/index.ts
140
+ var usage = `
141
+ <div style="border-radius: 10px; border: 1px solid #ddd; padding: 16px; margin-bottom: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
142
+ <h2 style="margin-top: 0; color: #4a6ee0;">📌 插件说明</h2>
143
+ <p>📖 <strong>使用文档</strong>:请点击左上角的 <strong>插件主页</strong> 查看插件使用文档</p>
144
+ <p>🔍 <strong>更多插件</strong>:可访问 <a href="https://github.com/YisRime" style="color:#4a6ee0;text-decoration:none;">苡淞的 GitHub</a> 查看本人的所有插件</p>
145
+ </div>
146
+ <div style="border-radius: 10px; border: 1px solid #ddd; padding: 16px; margin-bottom: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
147
+ <h2 style="margin-top: 0; color: #e0574a;">❤️ 支持与反馈</h2>
148
+ <p>🌟 喜欢这个插件?请在 <a href="https://github.com/YisRime" style="color:#e0574a;text-decoration:none;">GitHub</a> 上给我一个 Star!</p>
149
+ <p>🐛 遇到问题?请通过 <strong>Issues</strong> 提交反馈,或加入 QQ 群 <a href="https://qm.qq.com/q/PdLMx9Jowq" style="color:#e0574a;text-decoration:none;"><strong>855571375</strong></a> 进行交流</p>
150
+ </div>
151
+ `;
87
152
  var name = "chat-analyse";
88
153
  var Config = import_koishi.Schema.object({});
89
154
  var using = ["database"];
@@ -96,5 +161,6 @@ __name(apply, "apply");
96
161
  Config,
97
162
  apply,
98
163
  name,
164
+ usage,
99
165
  using
100
166
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chat-analyse",
3
3
  "description": "聊天记录分析",
4
- "version": "0.0.3",
4
+ "version": "0.1.1",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],