koishi-plugin-group-analysis-mx 1.8.1 → 1.9.0
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/config.js +1 -1
- package/lib/plugins/command.js +0 -6
- package/lib/service/analysis.js +23 -6
- package/lib/service/message.js +41 -10
- package/lib/utils.js +33 -0
- package/package.json +3 -1
package/lib/config.js
CHANGED
|
@@ -214,7 +214,7 @@ exports.Config = koishi_1.Schema.intersect([
|
|
|
214
214
|
.default([]),
|
|
215
215
|
personaBlacklist: koishi_1.Schema.array(String)
|
|
216
216
|
.role('table')
|
|
217
|
-
.description('
|
|
217
|
+
.description('用户画像黑名单。只影响用户画像(不采集、不分析、不可查询),不影响群分析的统计与报告。')
|
|
218
218
|
.default([])
|
|
219
219
|
}).description('用户画像设置'),
|
|
220
220
|
koishi_1.Schema.object({
|
package/lib/plugins/command.js
CHANGED
|
@@ -36,9 +36,6 @@ function apply(ctx, config) {
|
|
|
36
36
|
authority: 3
|
|
37
37
|
})
|
|
38
38
|
.action(async ({ session, options }, query) => {
|
|
39
|
-
if (config.personaBlacklist.includes(session.userId)) {
|
|
40
|
-
return '您无权使用本插件。';
|
|
41
|
-
}
|
|
42
39
|
if (session.isDirect && !options.group && !options.channel) {
|
|
43
40
|
return '私聊中请使用 -g 或 -c 指定目标群或频道。';
|
|
44
41
|
}
|
|
@@ -220,9 +217,6 @@ function apply(ctx, config) {
|
|
|
220
217
|
authority: 3
|
|
221
218
|
})
|
|
222
219
|
.action(async ({ session, options }, days) => {
|
|
223
|
-
if (config.personaBlacklist.includes(session.userId)) {
|
|
224
|
-
return '您无权使用本插件。';
|
|
225
|
-
}
|
|
226
220
|
if (session.isDirect && !options.group && !options.channel) {
|
|
227
221
|
return '私聊中请使用 -g 或 -c 指定目标群或频道。';
|
|
228
222
|
}
|
package/lib/service/analysis.js
CHANGED
|
@@ -624,6 +624,9 @@ class AnalysisService extends koishi_1.Service {
|
|
|
624
624
|
const recordId = (0, utils_1.buildPersonaRecordId)(session.platform, session.selfId, userId);
|
|
625
625
|
let avatar;
|
|
626
626
|
let user;
|
|
627
|
+
// 优先使用消息事件缓存下来的群昵称/头像(QQ 官方新事件已提供)
|
|
628
|
+
const memberProfile = this.ctx.group_analysis_message?.getMemberProfile?.(
|
|
629
|
+
session.guildId || session.channelId, userId);
|
|
627
630
|
try {
|
|
628
631
|
user = await bot.getGuildMember(session.channelId || session.guildId, userId);
|
|
629
632
|
}
|
|
@@ -635,7 +638,8 @@ class AnalysisService extends koishi_1.Service {
|
|
|
635
638
|
platform: session.platform,
|
|
636
639
|
selfId: session.selfId,
|
|
637
640
|
userId,
|
|
638
|
-
username:
|
|
641
|
+
username: memberProfile?.nickname ||
|
|
642
|
+
user?.nick || user?.name || user?.user?.name || userId
|
|
639
643
|
});
|
|
640
644
|
let displayName = cache.record.username;
|
|
641
645
|
const cacheExpired = (0, utils_1.isCacheExpiredByDays)(cache.record.lastAnalysisAt, this.config.personaCacheLifetimeDays);
|
|
@@ -662,6 +666,13 @@ class AnalysisService extends koishi_1.Service {
|
|
|
662
666
|
cache.pendingMessages = 0;
|
|
663
667
|
}
|
|
664
668
|
const profile = cache.parsedPersona;
|
|
669
|
+
if (memberProfile?.nickname) {
|
|
670
|
+
displayName = memberProfile.nickname;
|
|
671
|
+
cache.record.username = memberProfile.nickname;
|
|
672
|
+
}
|
|
673
|
+
if (memberProfile?.avatar) {
|
|
674
|
+
avatar = memberProfile.avatar;
|
|
675
|
+
}
|
|
665
676
|
if (user) {
|
|
666
677
|
// Koishi Universal.GuildMember 结构:{ user: { id, name, avatar }, name, ... }
|
|
667
678
|
avatar = user.avatar || user.user?.avatar;
|
|
@@ -708,7 +719,13 @@ class AnalysisService extends koishi_1.Service {
|
|
|
708
719
|
await session.send(message);
|
|
709
720
|
}
|
|
710
721
|
async resolveGroupNickname(bot, guildId, userId) {
|
|
711
|
-
if (!
|
|
722
|
+
if (!guildId || !userId)
|
|
723
|
+
return null;
|
|
724
|
+
// 优先用消息事件里带的群昵称(QQ 官方新事件已提供 member_nickname)
|
|
725
|
+
const cachedProfile = this.ctx.group_analysis_message?.getMemberProfile?.(guildId, String(userId));
|
|
726
|
+
if (cachedProfile?.nickname)
|
|
727
|
+
return cachedProfile.nickname;
|
|
728
|
+
if (!bot?.getGuildMember)
|
|
712
729
|
return null;
|
|
713
730
|
const key = `${bot.selfId}:${guildId}:${userId}`;
|
|
714
731
|
const cached = this._groupNicknameCache.get(key);
|
|
@@ -719,7 +736,8 @@ class AnalysisService extends koishi_1.Service {
|
|
|
719
736
|
}
|
|
720
737
|
try {
|
|
721
738
|
const info = await bot.getGuildMember(guildId, userId);
|
|
722
|
-
|
|
739
|
+
// 新接口/适配器可能提供群内昵称字段,优先于个人昵称
|
|
740
|
+
const name = (info?.member_nickname || info?.memberNickname || info?.name || info?.user?.name || '').trim();
|
|
723
741
|
this._groupNicknameCache.set(key, {
|
|
724
742
|
name: name || null,
|
|
725
743
|
expireAt: Date.now() + (name ? 3600_000 : 60_000)
|
|
@@ -866,12 +884,11 @@ class AnalysisService extends koishi_1.Service {
|
|
|
866
884
|
this.ctx.logger.error('金句分析失败:', quotesResult.reason);
|
|
867
885
|
// Final statistics
|
|
868
886
|
const sortedUsers = users.sort((a, b) => b.messageCount - a.messageCount);
|
|
869
|
-
// 群名片解析:官方 QQ bot 消息作者名是"个人昵称",群名片需通过 getGuildMember 获取。
|
|
870
|
-
// 只对活跃的 top 用户调用,且带缓存避免重复请求。
|
|
871
887
|
const botForAvatar = this._getBot(selfId);
|
|
872
888
|
const guildIdForNick = target?.guildId;
|
|
873
889
|
const groupNicknameMap = new Map();
|
|
874
|
-
|
|
890
|
+
// 群名片解析:优先用消息事件自带的群昵称,其次回退 getGuildMember
|
|
891
|
+
if (guildIdForNick) {
|
|
875
892
|
const topUsers = sortedUsers.slice(0, Math.max(this.config.maxUsersInReport * 2, 20));
|
|
876
893
|
await Promise.all(topUsers.map(async (u) => {
|
|
877
894
|
const gname = await this.resolveGroupNickname(botForAvatar, guildIdForNick, String(u.userId));
|
package/lib/service/message.js
CHANGED
|
@@ -7,6 +7,8 @@ const cqcode_1 = require("../onebot/cqcode");
|
|
|
7
7
|
class MessageService extends koishi_1.Service {
|
|
8
8
|
config;
|
|
9
9
|
messageCache = new Map();
|
|
10
|
+
/** QQ 官方事件带来的成员资料缓存(群昵称/头像),key = guildId:userId */
|
|
11
|
+
memberProfiles = new Map();
|
|
10
12
|
cacheSize = 1000;
|
|
11
13
|
cacheExpiration = 1000 * 60 * 24; // 1 days
|
|
12
14
|
messageHandlers = [];
|
|
@@ -28,6 +30,19 @@ class MessageService extends koishi_1.Service {
|
|
|
28
30
|
onUserMessage(handler) {
|
|
29
31
|
this.messageHandlers.push(handler);
|
|
30
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* 读取事件中缓存的群成员资料(群昵称/头像)。
|
|
35
|
+
* QQ 官方 bot 获取群成员信息的接口常受权限限制,这里优先用消息事件自带的字段。
|
|
36
|
+
*/
|
|
37
|
+
getMemberProfile(guildId, userId) {
|
|
38
|
+
if (!guildId || !userId)
|
|
39
|
+
return null;
|
|
40
|
+
const profile = this.memberProfiles.get(`${guildId}:${userId}`);
|
|
41
|
+
if (!profile)
|
|
42
|
+
return null;
|
|
43
|
+
// 缓存 6 小时,过期后仍可作为弱兜底返回,但标记 stale
|
|
44
|
+
return { ...profile, stale: Date.now() - profile.updatedAt > 6 * 60 * 60 * 1000 };
|
|
45
|
+
}
|
|
31
46
|
setupDatabase() {
|
|
32
47
|
this.ctx.database.extend('group_analysis_messages', {
|
|
33
48
|
id: 'string',
|
|
@@ -65,9 +80,16 @@ class MessageService extends koishi_1.Service {
|
|
|
65
80
|
return result;
|
|
66
81
|
}
|
|
67
82
|
async handleMessage(session) {
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
// 黑名单只限制用户画像(见 handleIncomingMessageForPersona),消息本身仍需入库供群分析统计
|
|
84
|
+
// QQ 官方机器人新事件直接带群昵称和头像,先缓存(即使该条消息因无文字被跳过,资料也应留存)
|
|
85
|
+
const qqMember = (0, utils_1.extractQQGroupMember)(session);
|
|
86
|
+
if (qqMember && (qqMember.nickname || qqMember.avatar)) {
|
|
87
|
+
const scope = session.guildId || session.channelId || '0';
|
|
88
|
+
this.memberProfiles.set(`${scope}:${qqMember.userId || session.userId}`, {
|
|
89
|
+
nickname: qqMember.nickname || '',
|
|
90
|
+
avatar: qqMember.avatar || '',
|
|
91
|
+
updatedAt: Date.now()
|
|
92
|
+
});
|
|
71
93
|
}
|
|
72
94
|
// 清洗掉官方 bot 的原始表情/协议标签,只保留正常文字
|
|
73
95
|
const cleanContent = (0, utils_1.cleanContent)(session.content);
|
|
@@ -87,9 +109,13 @@ class MessageService extends koishi_1.Service {
|
|
|
87
109
|
channelId: session.channelId || '0',
|
|
88
110
|
guildId: session.guildId || '0',
|
|
89
111
|
userId: session.userId,
|
|
90
|
-
avatarUrl: session.event.user?.avatar || '',
|
|
112
|
+
avatarUrl: (qqMember && qqMember.avatar) || session.event.user?.avatar || '',
|
|
91
113
|
// 用户昵称可能存在事件缺失(如官方 QQ bot 作者未返回 username),做多级兜底
|
|
92
|
-
username:
|
|
114
|
+
username: (qqMember && qqMember.nickname) ||
|
|
115
|
+
session.username ||
|
|
116
|
+
session.event?.user?.name ||
|
|
117
|
+
session.userId ||
|
|
118
|
+
'未知用户',
|
|
93
119
|
content: cleanContent,
|
|
94
120
|
timestamp: new Date(session.timestamp),
|
|
95
121
|
messageId: session.messageId
|
|
@@ -211,8 +237,8 @@ class MessageService extends koishi_1.Service {
|
|
|
211
237
|
// Filter out users in personaUserFilter for user persona
|
|
212
238
|
purposeFilter = !this.config.personaUserFilter.includes(msg.userId);
|
|
213
239
|
}
|
|
214
|
-
//
|
|
215
|
-
if (this.config.personaBlacklist?.includes(msg.userId)) {
|
|
240
|
+
// 黑名单仅作用于用户画像,不影响群分析统计
|
|
241
|
+
if (filter.purpose === 'user-persona' && this.config.personaBlacklist?.includes(msg.userId)) {
|
|
216
242
|
purposeFilter = false;
|
|
217
243
|
}
|
|
218
244
|
return (withinTimeRange &&
|
|
@@ -346,8 +372,8 @@ class MessageService extends koishi_1.Service {
|
|
|
346
372
|
purposeFilter =
|
|
347
373
|
!this.config.personaUserFilter.includes(userId);
|
|
348
374
|
}
|
|
349
|
-
//
|
|
350
|
-
if (this.config.personaBlacklist?.includes(userId)) {
|
|
375
|
+
// 黑名单仅作用于用户画像,不影响群分析统计
|
|
376
|
+
if (filter.purpose === 'user-persona' && this.config.personaBlacklist?.includes(userId)) {
|
|
351
377
|
purposeFilter = false;
|
|
352
378
|
}
|
|
353
379
|
return (withinTimeRange &&
|
|
@@ -412,7 +438,12 @@ class MessageService extends koishi_1.Service {
|
|
|
412
438
|
query.timestamp.$lte = filter.endTime;
|
|
413
439
|
}
|
|
414
440
|
// 用户条件:显式指定的用户 ∩ 排除名单。所有排除项合并,而不是互相覆盖。
|
|
415
|
-
|
|
441
|
+
// 黑名单只影响用户画像,群分析不排除这些用户
|
|
442
|
+
const excluded = new Set();
|
|
443
|
+
if (filter.purpose === 'user-persona') {
|
|
444
|
+
for (const id of (this.config.personaBlacklist || []))
|
|
445
|
+
excluded.add(String(id));
|
|
446
|
+
}
|
|
416
447
|
if (filter.purpose === 'group-analysis') {
|
|
417
448
|
for (const id of (this.config.userFilter || []))
|
|
418
449
|
excluded.add(String(id));
|
package/lib/utils.js
CHANGED
|
@@ -27,6 +27,7 @@ exports.isCacheExpiredByMinutes = isCacheExpiredByMinutes;
|
|
|
27
27
|
exports.buildGroupAnalysisCacheKey = buildGroupAnalysisCacheKey;
|
|
28
28
|
exports.buildMessagePersistenceKey = buildMessagePersistenceKey;
|
|
29
29
|
exports.isNapCatBot = isNapCatBot;
|
|
30
|
+
exports.extractQQGroupMember = extractQQGroupMember;
|
|
30
31
|
exports.isJunkMessage = isJunkMessage;
|
|
31
32
|
exports.isForwardedChatRecord = isForwardedChatRecord;
|
|
32
33
|
exports.cleanContent = cleanContent;
|
|
@@ -373,6 +374,38 @@ async function isNapCatBot(bot) {
|
|
|
373
374
|
const name = versionInfo.data['app_name'].toLowerCase();
|
|
374
375
|
return { isRunningNapCat: name.includes('napcat'), botAppName: name };
|
|
375
376
|
}
|
|
377
|
+
/**
|
|
378
|
+
* 从 QQ 官方机器人原始事件中提取发送者资料。
|
|
379
|
+
* 2026-10 起官方在群消息事件里新增 author.member_nickname(群内昵称)与 author.avatar,
|
|
380
|
+
* 这两项比 username(个人昵称)和 openid 头像模板更准确,优先使用。
|
|
381
|
+
* 原始 payload 由 Koishi QQ 适配器保存在 session.event._data。
|
|
382
|
+
*/
|
|
383
|
+
function extractQQGroupMember(session) {
|
|
384
|
+
if (!session || session.platform !== 'qq')
|
|
385
|
+
return null;
|
|
386
|
+
const raw = session.event?._data;
|
|
387
|
+
// 适配器有两种形态:内部事件 _data 直接是事件体,消息会话 _data 是 { op, t, d } 派发载荷
|
|
388
|
+
const payload = raw && typeof raw === 'object' && raw.d && typeof raw.d === 'object' ? raw.d : raw;
|
|
389
|
+
const author = payload?.author;
|
|
390
|
+
if (!author || typeof author !== 'object')
|
|
391
|
+
return null;
|
|
392
|
+
const memberNickname = String(author.member_nickname || '').trim();
|
|
393
|
+
const username = String(author.username || '').trim();
|
|
394
|
+
const avatar = String(author.avatar || '').trim();
|
|
395
|
+
const memberOpenid = String(author.member_openid || author.id || '').trim();
|
|
396
|
+
const unionGroupOpenid = String(payload?.union_group_openid || '').trim();
|
|
397
|
+
if (!memberNickname && !username && !avatar)
|
|
398
|
+
return null;
|
|
399
|
+
return {
|
|
400
|
+
userId: memberOpenid,
|
|
401
|
+
// 群内昵称优先,其次个人昵称
|
|
402
|
+
nickname: memberNickname || username || '',
|
|
403
|
+
username,
|
|
404
|
+
memberNickname,
|
|
405
|
+
avatar,
|
|
406
|
+
unionGroupOpenid
|
|
407
|
+
};
|
|
408
|
+
}
|
|
376
409
|
/**
|
|
377
410
|
* 判断消息是否为 QQ“合并转发/聊天记录”卡片。
|
|
378
411
|
* 官方适配器没有解析 102 类型的聊天记录,只把卡片预览文字当成普通文本,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-group-analysis-mx",
|
|
3
3
|
"description": "群聊分析插件 - 生成精美群聊统计报告,支持 AI 话题总结(独立版,不依赖 ChatLuna)",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.9.0",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "lib/index.mjs",
|
|
7
7
|
"typings": "lib/index.d.ts",
|
|
@@ -65,4 +65,6 @@
|
|
|
65
65
|
|
|
66
66
|
|
|
67
67
|
|
|
68
|
+
|
|
69
|
+
|
|
68
70
|
|