koishi-plugin-group-analysis-mx 1.2.4 → 1.2.5
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/service/analysis.js +59 -15
- package/package.json +1 -1
package/lib/service/analysis.js
CHANGED
|
@@ -15,6 +15,8 @@ class AnalysisService extends koishi_1.Service {
|
|
|
15
15
|
personaCache = new Map();
|
|
16
16
|
personaProcessing = new Set();
|
|
17
17
|
groupAnalysisCache = new Map();
|
|
18
|
+
// 群昵称缓存:官方 bot 消息作者名是个人昵称,群名片需通过 getGuildMember 获取
|
|
19
|
+
_groupNicknameCache = new Map();
|
|
18
20
|
constructor(ctx, config) {
|
|
19
21
|
super(ctx, 'group_analysis', true);
|
|
20
22
|
this.config = config;
|
|
@@ -702,6 +704,31 @@ class AnalysisService extends koishi_1.Service {
|
|
|
702
704
|
}
|
|
703
705
|
await session.send(message);
|
|
704
706
|
}
|
|
707
|
+
async resolveGroupNickname(bot, guildId, userId) {
|
|
708
|
+
if (!bot?.getGuildMember || !guildId || !userId)
|
|
709
|
+
return null;
|
|
710
|
+
const key = `${bot.selfId}:${guildId}:${userId}`;
|
|
711
|
+
const cached = this._groupNicknameCache.get(key);
|
|
712
|
+
if (cached) {
|
|
713
|
+
if (cached.expireAt > Date.now())
|
|
714
|
+
return cached.name;
|
|
715
|
+
this._groupNicknameCache.delete(key);
|
|
716
|
+
}
|
|
717
|
+
try {
|
|
718
|
+
const info = await bot.getGuildMember(guildId, userId);
|
|
719
|
+
const name = (info?.name || info?.user?.name || '').trim();
|
|
720
|
+
this._groupNicknameCache.set(key, {
|
|
721
|
+
name: name || null,
|
|
722
|
+
expireAt: Date.now() + (name ? 3600_000 : 60_000)
|
|
723
|
+
});
|
|
724
|
+
return name || null;
|
|
725
|
+
}
|
|
726
|
+
catch (error) {
|
|
727
|
+
// 无权限或调用失败,缓存失败状态 60 秒,避免反复请求
|
|
728
|
+
this._groupNicknameCache.set(key, { name: null, expireAt: Date.now() + 60_000 });
|
|
729
|
+
return null;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
705
732
|
async lookupUserProfileFromMessages(platform, selfId, userId) {
|
|
706
733
|
try {
|
|
707
734
|
const records = await this.ctx.database
|
|
@@ -806,9 +833,23 @@ class AnalysisService extends koishi_1.Service {
|
|
|
806
833
|
});
|
|
807
834
|
// Final statistics
|
|
808
835
|
const sortedUsers = users.sort((a, b) => b.messageCount - a.messageCount);
|
|
836
|
+
// 群名片解析:官方 QQ bot 消息作者名是"个人昵称",群名片需通过 getGuildMember 获取。
|
|
837
|
+
// 只对活跃的 top 用户调用,且带缓存避免重复请求。
|
|
838
|
+
const botForAvatar = this._getBot(selfId);
|
|
839
|
+
const guildIdForNick = target?.guildId;
|
|
840
|
+
const groupNicknameMap = new Map();
|
|
841
|
+
if (botForAvatar?.getGuildMember && guildIdForNick) {
|
|
842
|
+
const topUsers = sortedUsers.slice(0, Math.max(this.config.maxUsersInReport * 2, 20));
|
|
843
|
+
await Promise.all(topUsers.map(async (u) => {
|
|
844
|
+
const gname = await this.resolveGroupNickname(botForAvatar, guildIdForNick, String(u.userId));
|
|
845
|
+
if (gname)
|
|
846
|
+
groupNicknameMap.set(String(u.userId), gname);
|
|
847
|
+
}));
|
|
848
|
+
if (groupNicknameMap.size)
|
|
849
|
+
this.ctx.logger.info(`已获取 ${groupNicknameMap.size} 个群成员的群名片。`);
|
|
850
|
+
}
|
|
809
851
|
// 为用户补全真实头像:优先消息记录中的头像,其次按平台生成头像 URL。
|
|
810
852
|
// 官方 QQ 机器人等平台下 userId 为 openid,不能直接使用 QQ 号头像接口。
|
|
811
|
-
const botForAvatar = this._getBot(selfId);
|
|
812
853
|
const avatarMap = new Map();
|
|
813
854
|
for (const user of sortedUsers) {
|
|
814
855
|
if (!user.avatar) {
|
|
@@ -816,23 +857,26 @@ class AnalysisService extends koishi_1.Service {
|
|
|
816
857
|
}
|
|
817
858
|
avatarMap.set(String(user.userId), user.avatar || '');
|
|
818
859
|
}
|
|
819
|
-
//
|
|
820
|
-
//
|
|
860
|
+
// 昵称映射:统一用群名片作为显示名。LLM 生成的 sender/name 很可能是"个人昵称",
|
|
861
|
+
// 这里把个人昵称(含/不含 emoji)归一化后映射到群名片。
|
|
821
862
|
const nicknameAvatarMap = new Map();
|
|
822
863
|
const nicknameMap = new Map();
|
|
823
864
|
for (const user of sortedUsers) {
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
865
|
+
const rawName = user.nickname;
|
|
866
|
+
const displayName = groupNicknameMap.get(String(user.userId)) || rawName;
|
|
867
|
+
if (rawName) {
|
|
868
|
+
const normRaw = (0, utils_1.normalizeNickname)(rawName);
|
|
869
|
+
nicknameMap.set(normRaw || rawName, displayName);
|
|
870
|
+
nicknameMap.set((0, utils_1.normalizeNickname)(displayName) || displayName, displayName);
|
|
871
|
+
}
|
|
872
|
+
if (user.avatar) {
|
|
873
|
+
nicknameAvatarMap.set(rawName, user.avatar);
|
|
874
|
+
const normRaw = (0, utils_1.normalizeNickname)(rawName);
|
|
875
|
+
if (normRaw)
|
|
876
|
+
nicknameAvatarMap.set(normRaw, user.avatar);
|
|
877
|
+
}
|
|
878
|
+
// 排行榜显示名使用群名片
|
|
879
|
+
user.nickname = displayName;
|
|
836
880
|
}
|
|
837
881
|
// 用户称号:用真实昵称替换 LLM 生成的 name(保留 emoji),并补全头像
|
|
838
882
|
if (Array.isArray(userTitles)) {
|
package/package.json
CHANGED