koishi-plugin-chat-analyse 0.4.7 → 0.4.9

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/index.d.ts CHANGED
@@ -18,7 +18,6 @@ export interface Config {
18
18
  enableOriRecord: boolean;
19
19
  enableWhoAt: boolean;
20
20
  enableData: boolean;
21
- enableDebug: boolean;
22
21
  atRetentionDays: number;
23
22
  rankRetentionDays: number;
24
23
  }
package/lib/index.js CHANGED
@@ -202,46 +202,43 @@ var Collector = class _Collector {
202
202
  * @returns {Promise<UserCache | null>} 返回用户的缓存对象,如果操作失败则返回 `null`。
203
203
  */
204
204
  async getOrCreateCachedUser(session, channelId) {
205
- const { userId, bot, guildId } = session;
206
- const cacheKey = `${channelId}:${userId}`;
205
+ const { userId, guildId, bot } = session;
206
+ const effectiveId = guildId || channelId;
207
+ const cacheKey = `${effectiveId}:${userId}`;
207
208
  if (this.userCache.has(cacheKey)) return this.userCache.get(cacheKey);
208
209
  if (this.pendingUserRequests.has(cacheKey)) return this.pendingUserRequests.get(cacheKey);
209
210
  const promise = (async () => {
210
211
  try {
211
- const [dbUser] = await this.ctx.database.get("analyse_user", { channelId, userId });
212
- if (dbUser && dbUser.userName && dbUser.channelName) {
213
- this.userCache.set(cacheKey, dbUser);
214
- return dbUser;
212
+ const dbUsers = await this.ctx.database.get("analyse_user", { channelId: effectiveId, userId });
213
+ const dbUser = dbUsers[0];
214
+ const currentUserName = session.username || "";
215
+ let currentChannelName = "";
216
+ if (effectiveId && bot.getGuild) {
217
+ const guild = await bot.getGuild(effectiveId);
218
+ currentChannelName = guild?.name || "";
215
219
  }
216
- const [guild, member] = await Promise.all([
217
- guildId ? bot.getGuild(guildId).catch(() => null) : Promise.resolve(null),
218
- guildId ? bot.getGuildMember(guildId, userId).catch(() => null) : Promise.resolve(null)
219
- ]);
220
- const user = !member ? await bot.getUser(userId).catch(() => null) : null;
221
- const fetchedUserName = member?.nick || member?.name || user?.name || "";
222
- const fetchedChannelName = guild?.name || "";
223
220
  if (dbUser) {
224
- const needsUpdate = !dbUser.userName && fetchedUserName || !dbUser.channelName && fetchedChannelName;
225
- if (needsUpdate) {
226
- dbUser.userName = dbUser.userName || fetchedUserName;
227
- dbUser.channelName = dbUser.channelName || fetchedChannelName;
221
+ const nameChanged = currentUserName && dbUser.userName !== currentUserName;
222
+ const channelNameChanged = currentChannelName && dbUser.channelName !== currentChannelName;
223
+ if (nameChanged || channelNameChanged) {
224
+ dbUser.userName = nameChanged ? currentUserName : dbUser.userName;
225
+ dbUser.channelName = channelNameChanged ? currentChannelName : dbUser.channelName;
228
226
  await this.ctx.database.set("analyse_user", { uid: dbUser.uid }, {
229
227
  userName: dbUser.userName,
230
228
  channelName: dbUser.channelName
231
229
  });
232
230
  }
233
- if (dbUser.userName && dbUser.channelName) this.userCache.set(cacheKey, dbUser);
231
+ this.userCache.set(cacheKey, dbUser);
234
232
  return dbUser;
235
- } else {
236
- const createdUser = await this.ctx.database.create("analyse_user", {
237
- channelId,
238
- userId,
239
- userName: fetchedUserName,
240
- channelName: fetchedChannelName
241
- });
242
- if (createdUser.userName && createdUser.channelName) this.userCache.set(cacheKey, createdUser);
243
- return createdUser;
244
233
  }
234
+ const createdUser = await this.ctx.database.create("analyse_user", {
235
+ channelId: effectiveId,
236
+ userId,
237
+ userName: currentUserName,
238
+ channelName: currentChannelName
239
+ });
240
+ this.userCache.set(cacheKey, createdUser);
241
+ return createdUser;
245
242
  } catch (error) {
246
243
  this.ctx.logger.error(`创建或获取用户(${cacheKey})失败:`, error);
247
244
  return null;
@@ -1078,72 +1075,6 @@ var Data = class {
1078
1075
  return "数据清理失败";
1079
1076
  }
1080
1077
  });
1081
- }
1082
- };
1083
-
1084
- // src/Debug.ts
1085
- var Debug = class {
1086
- /**
1087
- * @constructor
1088
- * @param {Context} ctx - Koishi 的插件上下文。
1089
- */
1090
- constructor(ctx) {
1091
- this.ctx = ctx;
1092
- }
1093
- static {
1094
- __name(this, "Debug");
1095
- }
1096
- /**
1097
- * @public
1098
- * @method registerCommands
1099
- * @description 在 'analyse' 命令下注册所有调试相关的子命令。
1100
- * @param {Command} analyse - 主 'analyse' 命令实例。
1101
- */
1102
- registerCommands(analyse) {
1103
- analyse.subcommand(".fill", "手动补全用户信息", { authority: 4 }).action(async ({ session }) => {
1104
- const bots = this.ctx.bots;
1105
- if (bots.length === 0) return "暂无可用机器人";
1106
- const usersToUpdate = await this.ctx.database.get("analyse_user", {
1107
- $or: [{ userName: "" }, { channelName: "" }]
1108
- });
1109
- if (usersToUpdate.length === 0) return "暂无用户信息需要补全";
1110
- const usersByChannel = usersToUpdate.reduce((acc, user) => {
1111
- (acc[user.channelId] = acc[user.channelId] || []).push(user);
1112
- return acc;
1113
- }, {});
1114
- let updatedCount = 0;
1115
- const bot = bots.find((b) => b.platform === session.platform) || bots[0];
1116
- for (const channelId in usersByChannel) {
1117
- const usersInChannel = usersByChannel[channelId];
1118
- let channelName = usersInChannel.find((u) => u.channelName)?.channelName || "";
1119
- if (!channelName && channelId) {
1120
- try {
1121
- channelName = (await bot.getGuild(channelId))?.name || "";
1122
- } catch (e) {
1123
- this.ctx.logger.warn(`获取频道 ${channelId} 信息失败:`, e);
1124
- }
1125
- }
1126
- for (const user of usersInChannel) {
1127
- if (user.userName && user.channelName) continue;
1128
- let userName = user.userName;
1129
- if (!userName && user.userId && channelId) {
1130
- try {
1131
- const member = await bot.getGuildMember(channelId, user.userId);
1132
- userName = member?.nick || member?.name || "";
1133
- if (!userName) userName = (await bot.getUser(user.userId))?.name || "";
1134
- } catch (e) {
1135
- this.ctx.logger.warn(`获取频道 ${channelId} 的用户 ${user.userId} 信息失败:`, e);
1136
- }
1137
- }
1138
- await this.ctx.database.set("analyse_user", { uid: user.uid }, {
1139
- userName: userName || user.userName,
1140
- channelName: channelName || user.channelName
1141
- });
1142
- updatedCount++;
1143
- }
1144
- }
1145
- return `已补全 ${updatedCount} 条用户信息`;
1146
- });
1147
1078
  analyse.subcommand(".list", "列出频道及命令", { authority: 4 }).action(async () => {
1148
1079
  const allChannelInfo = await this.ctx.database.get("analyse_user", {}, ["channelId", "channelName"]);
1149
1080
  const uniqueChannels = [...new Map(allChannelInfo.map((item) => [item.channelId, item])).values()];
@@ -1174,11 +1105,12 @@ var using = ["database", "puppeteer", "cron"];
1174
1105
  var Config = import_koishi6.Schema.intersect([
1175
1106
  import_koishi6.Schema.object({
1176
1107
  enableListener: import_koishi6.Schema.boolean().default(true).description("启用消息监听"),
1177
- enableOriRecord: import_koishi6.Schema.boolean().default(true).description("启用原始记录")
1108
+ enableData: import_koishi6.Schema.boolean().default(false).description("启用数据管理")
1178
1109
  }).description("监听配置"),
1179
1110
  import_koishi6.Schema.object({
1180
1111
  enableCmdStat: import_koishi6.Schema.boolean().default(true).description("启用命令统计"),
1181
- enableMsgStat: import_koishi6.Schema.boolean().default(true).description("启用消息统计")
1112
+ enableMsgStat: import_koishi6.Schema.boolean().default(true).description("启用消息统计"),
1113
+ enableOriRecord: import_koishi6.Schema.boolean().default(true).description("启用原始记录")
1182
1114
  }).description("功能配置"),
1183
1115
  import_koishi6.Schema.object({
1184
1116
  enableRankStat: import_koishi6.Schema.boolean().default(true).description("启用发言排行"),
@@ -1187,11 +1119,7 @@ var Config = import_koishi6.Schema.intersect([
1187
1119
  import_koishi6.Schema.object({
1188
1120
  enableWhoAt: import_koishi6.Schema.boolean().default(true).description("启用 @ 记录"),
1189
1121
  atRetentionDays: import_koishi6.Schema.number().min(0).default(7).description("记录保留天数")
1190
- }).description("@ 记录配置"),
1191
- import_koishi6.Schema.object({
1192
- enableData: import_koishi6.Schema.boolean().default(false).description("启用数据管理"),
1193
- enableDebug: import_koishi6.Schema.boolean().default(false).description("启用调试工具")
1194
- }).description("高级功能")
1122
+ }).description("@ 记录配置")
1195
1123
  ]);
1196
1124
  function apply(ctx, config) {
1197
1125
  if (config.enableListener) new Collector(ctx, config);
@@ -1199,7 +1127,6 @@ function apply(ctx, config) {
1199
1127
  new Stat(ctx, config).registerCommands(analyse);
1200
1128
  if (config.enableWhoAt) new WhoAt(ctx, config).registerCommand(analyse);
1201
1129
  if (config.enableData) new Data(ctx).registerCommands(analyse);
1202
- if (config.enableDebug) new Debug(ctx).registerCommands(analyse);
1203
1130
  }
1204
1131
  __name(apply, "apply");
1205
1132
  // 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.4.7",
4
+ "version": "0.4.9",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],