@nestim/koishi-plugin-qq-group-manager 0.1.8 → 0.1.10

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.js CHANGED
@@ -72,7 +72,7 @@ exports.Config = koishi_1.Schema.intersect([
72
72
  blacklistFileName: koishi_1.Schema.string().default('banMember.md').description('黑名单文件名(保存在 Koishi 数据目录,可用控制台左侧「探索器 Explorer」查看/编辑)。'),
73
73
  banCommand: koishi_1.Schema.string().default('[ban]').description('(兼容字段)黑名单指令前缀,等同 cmdBan。'),
74
74
  banKickOnJoin: koishi_1.Schema.boolean().default(true).description('有人入群时是否自动比对黑名单,命中则立即踢出并提示。'),
75
- banRecordOnKick: koishi_1.Schema.boolean().default(true).description('发生踢人时(bot 踢人、群主/管理员踢人)是否自动把被踢者加入黑名单。'),
75
+ banRecordOnKick: koishi_1.Schema.boolean().default(true).description('有人被移出群聊时是否自动把被踢者加入黑名单(仅处理明确判定为被踢的事件 subtype=passive,主动退群不记;bot 自己永不记入。不想让群主/管理员踢人也入黑名单时设为 false)。'),
76
76
  }).description('黑名单'),
77
77
  koishi_1.Schema.object({
78
78
  enableSelfGag: koishi_1.Schema.boolean().default(true).description('启用口球娱乐:普通成员对自己使用 mute/gag 时随机 1~60 分钟禁言。'),
package/lib/service.js CHANGED
@@ -46,6 +46,46 @@ class QQGroupManagerService extends koishi_1.Service {
46
46
  eventOperatorId(session) {
47
47
  return String((session && (session.operatorId || session.userId)) || '');
48
48
  }
49
+ /**
50
+ * 把任意形态的 ID 归一成纯数字 QQ 号,便于比较。
51
+ * 兼容 'onebot:12345'、12345(number)、'12345'。
52
+ * selfId 取自 session.bot.selfId(session.selfId 在 Koishi 里通常不存在)。
53
+ */
54
+ normalizeQqForCompare(value) {
55
+ const raw = String(value ?? '').trim();
56
+ if (!raw)
57
+ return '';
58
+ const tail = raw.includes(':') ? raw.slice(raw.lastIndexOf(':') + 1) : raw;
59
+ const digits = tail.replace(/\D/g, '');
60
+ return digits || '';
61
+ }
62
+ /** bot 自身 QQ */
63
+ selfQqForCompare(session) {
64
+ return this.normalizeQqForCompare(session?.bot?.selfId ?? session?.selfId);
65
+ }
66
+ /**
67
+ * 求「被移出群聊的人」的 QQ,并标记身份是否可确证。
68
+ *
69
+ * 字段语义(Koishi onebot):
70
+ * - `session.targetId` 存在时,就是被移出的人,最可信;
71
+ * - `session.userId` 在移出事件里通常是**操作者**(bot 自己踢人时就是 bot),
72
+ * 只有没有 targetId 时才退而用它,且仅当它与操作者不同(=当时是被踢者)才可采信;
73
+ * - 永远排除 bot 自己。
74
+ *
75
+ * 返回 `{ id, certain }`。`certain=false` 表示只是猜测,宁可漏记也不误拉黑。
76
+ */
77
+ resolveRemovedMemberId(session) {
78
+ const rawTarget = this.normalizeQqForCompare(session?.targetId);
79
+ if (rawTarget)
80
+ return { id: rawTarget, certain: true };
81
+ const id = this.normalizeQqForCompare(session?.userId);
82
+ if (!id)
83
+ return { id: '', certain: false };
84
+ const operator = this.normalizeQqForCompare(this.eventOperatorId(session));
85
+ if (id === operator && operator !== this.selfQqForCompare(session))
86
+ return { id: '', certain: false };
87
+ return { id, certain: false };
88
+ }
49
89
  /** 踢人后记入黑名单(供自动踢人/手动踢人复用) */
50
90
  async recordKickToBlacklist(session, qq) {
51
91
  if (!this.cfgBool('banRecordOnKick', true))
@@ -65,21 +105,39 @@ class QQGroupManagerService extends koishi_1.Service {
65
105
  }
66
106
  }
67
107
  /**
68
- * 群成员被移出(自己退群 or 被踢)→ 记入本群黑名单。
69
- * 仅处理 subtype='passive'(被踢),主动退群不记。
108
+ * 群成员被移出 → 记入本群黑名单。
109
+ *
110
+ * 只在「明确判定为被踢」时记录。历史 bug:原先写的是
111
+ * `if (session.subtype && session.subtype !== 'passive') return;`,
112
+ * subtype 缺失(undefined)时会继续往下走,把**主动退群**的人也当被踢记进黑名单,
113
+ * 并且会误用 session.userId(可能是踢人者/事件消费者)。
114
+ * 现在改为白名单式判定:只有 subtype === 'passive' 才处理。
70
115
  */
71
116
  async handleMemberRemoved(session) {
72
117
  if (!this.isPlatformAllowed(session.platform))
73
118
  return;
74
119
  if (!session.guildId)
75
120
  return;
76
- // 只记"被踢",自己退群不记
77
- if (session.subtype && session.subtype !== 'passive')
121
+ // 白名单式判定:只认「被踢」,主动退群/未知一律不记
122
+ if (session.subtype !== 'passive') {
123
+ this._logger.info(`[ban] 退群事件 subtype=${session.subtype ?? 'unknown'},非被动踢出,不记黑名单 qq=${this.normalizeQqForCompare(session.targetId || session.userId) || '未知'} | ${this.sessionTag(session)}`);
78
124
  return;
79
- const qq = String(session.targetId || session.userId || '').trim();
80
- if (qq === String(session.selfId || ''))
125
+ }
126
+ const self = this.selfQqForCompare(session);
127
+ const operator = this.normalizeQqForCompare(this.eventOperatorId(session));
128
+ const resolved = this.resolveRemovedMemberId(session);
129
+ const qq = resolved.id;
130
+ // bot 自己被踢:不记录(原判断用 session.selfId,取不到值导致 bot 可能把自己拉黑)
131
+ if (qq && self && qq === self) {
132
+ this._logger.info(`[ban] bot 自身被移出群聊,不记黑名单 | ${this.sessionTag(session)}`);
133
+ return;
134
+ }
135
+ // 没能确证被踢者身份时,只记日志不写黑名单,避免误拉黑
136
+ if (!qq || (!resolved.certain && operator && operator !== self)) {
137
+ this._logger.warn(`[ban] 无法确证被踢者(targetId 缺失且 userId 落在操作者上),已跳过记录以免误拉黑 | ${this.sessionTag(session)}`);
81
138
  return;
82
- this._logger.info(`[ban] 检测到踢人事件 qq=${qq} operator=${this.eventOperatorId(session)} | ${this.sessionTag(session)}`);
139
+ }
140
+ this._logger.info(`[ban] 检测到踢人事件 被踢者=${qq} 操作者=${operator || '未知'} 来源=${resolved.certain ? 'targetId' : 'userId'} | ${this.sessionTag(session)}`);
83
141
  await this.recordKickToBlacklist(session, qq);
84
142
  }
85
143
  /**
@@ -3215,24 +3273,24 @@ class QQGroupManagerService extends koishi_1.Service {
3215
3273
  return false;
3216
3274
  }
3217
3275
  /**
3218
- * 指令菜单:仅英文与数字([记忆] 新规),记忆查询紧跟在记忆保存之后。
3219
- * 冒号后留一个空行,避免标题与条目挤在一起。
3276
+ * 指令菜单:行首用中文介绍功能名称,指令本身保持英文([记忆] 新规)。
3277
+ * 记忆查询紧跟在记忆保存之后;冒号后留一个空行,避免标题与条目挤在一起。
3220
3278
  */
3221
3279
  async sendCommandMenu(session) {
3222
3280
  const P = (k, d) => this.cmdPrefix(k, d);
3223
3281
  const lines = [
3224
- 'mute ' + P('cmdMute', '[mute]') + ' qq time reason',
3225
- 'unmute ' + P('cmdUnmute', '[unmute]') + ' qq reason',
3226
- 'memory ' + P('cmdMemory', '[memory]') + ' content',
3227
- 'memory search ' + P('cmdMemory', '[memory]') + ' search keyword',
3228
- 'ban ' + P('cmdBan', '[ban]') + ' qq reason',
3229
- 'unban ' + P('cmdUnban', '[unban]') + ' qq reason',
3230
- 'banlist ' + P('cmdBanList', '[banlist]'),
3231
- 'custom ' + P('cmdCustom', '[custom]'),
3232
- 'kick ' + P('cmdKick', '[kick]') + ' qq reason',
3282
+ '禁言 ' + P('cmdMute', '[mute]') + ' qq time reason',
3283
+ '解除禁言 ' + P('cmdUnmute', '[unmute]') + ' qq reason',
3284
+ '记忆保存 ' + P('cmdMemory', '[memory]') + ' content',
3285
+ '记忆查询 ' + P('cmdMemory', '[memory]') + ' search keyword',
3286
+ '拉黑 ' + P('cmdBan', '[ban]') + ' qq reason',
3287
+ '解除拉黑 ' + P('cmdUnban', '[unban]') + ' qq reason',
3288
+ '黑名单查询 ' + P('cmdBanList', '[banlist]'),
3289
+ '自定义指令集 ' + P('cmdCustom', '[custom]'),
3290
+ '踢人 ' + P('cmdKick', '[kick]') + ' qq reason',
3233
3291
  ];
3234
3292
  await session.send(this.styleText(
3235
- 'available commands:\n\n' + lines.join('\n')
3293
+ '可用指令:\n\n' + lines.join('\n')
3236
3294
  ));
3237
3295
  }
3238
3296
  /** 自定义指令集:列出当前生效的自定义命令;没有配置时明确提示暂无指令 */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nestim/koishi-plugin-qq-group-manager",
3
3
  "description": "Koishi QQ群管理插件(OneBot/LLOneBot):群管命令、权限校验、文本菜单、复读、AI群聊回复、记忆库、识图与自动管控(禁言/踢人)。",
4
- "version": "0.1.8",
4
+ "version": "0.1.10",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/readme.md CHANGED
@@ -4,6 +4,29 @@
4
4
 
5
5
  QQ群管理插件(OneBot/LLOneBot):群管命令、权限校验、文本菜单、复读、AI群聊回复、记忆库、识图与自动管控(禁言/踢人)。
6
6
 
7
+ ## v0.1.10 重点更新
8
+
9
+ ### 修复:没发过消息的成员被误加入黑名单
10
+
11
+ `handleMemberRemoved` 原先写成 `if (session.subtype && session.subtype !== 'passive') return;`,
12
+ **subtype 缺失时不会 return**,导致「主动退群」的人也被当成「被踢」记进黑名单
13
+ (很多适配器不给 `subtype`,所以触发面不小);同时 `session.selfId` 取不到值,
14
+ 「别把 bot 自己记进去」的保护也失效。
15
+
16
+ 已改为:只有 `subtype === 'passive'` 才处理;被踢者优先取 `targetId`,拿不到就退用 `userId`
17
+ 但必须与操作者不同;bot 自己永不入黑名单;无法确证时只记日志、不写黑名单。
18
+
19
+ **历史误封不会自动清除**,请打开 `banMember_<群号>.md` 删掉误封条目,或用 `[unban] QQ号` 解除。
20
+ 若不想让「群主/管理员踢人」也自动入黑名单,把 `banRecordOnKick` 设为 `false`。
21
+
22
+ ## v0.1.9 重点更新
23
+
24
+ ### 菜单显示:中文功能名 + 英文指令
25
+
26
+ 需求是「**指令**用英文,指令**前面**用中文介绍名称」。v0.1.8 曾把整行改成英文,现已修正为
27
+ 行首中文功能名、方括号指令保持英文(见下方「菜单功能」一节的完整示例)。
28
+ 指令触发词仍只有英文与数字,中文只用于行首的名称。
29
+
7
30
  ## v0.1.8 重点更新
8
31
 
9
32
  ### 1. 旧指令菜单已删除,一次 `[bot]` 只出一份菜单
@@ -250,20 +273,21 @@ src/
250
273
  ## 菜单功能
251
274
 
252
275
  - 唯一入口:群里单独发送 `command`(默认 `[bot]`),返回插件自己的**纯文本指令菜单**。
253
- - 菜单内容只有英文与数字,冒号后留一个空行,条目如下(`memory search` 紧跟在 `memory` 下面):
276
+ - 菜单每行 = **中文功能名称 + 英文方括号指令**(指令本身只有英文与数字),冒号后留一个空行,
277
+ `记忆查询` 紧跟在 `记忆保存` 下面:
254
278
 
255
279
  ```txt
256
- available commands:
257
-
258
- mute [mute] qq time reason
259
- unmute [unmute] qq reason
260
- memory [memory] content
261
- memory search [memory] search keyword
262
- ban [ban] qq reason
263
- unban [unban] qq reason
264
- banlist [banlist]
265
- custom [custom]
266
- kick [kick] qq reason
280
+ 可用指令:
281
+
282
+ 禁言 [mute] qq time reason
283
+ 解除禁言 [unmute] qq reason
284
+ 记忆保存 [memory] content
285
+ 记忆查询 [memory] search keyword
286
+ 拉黑 [ban] qq reason
287
+ 解除拉黑 [unban] qq reason
288
+ 黑名单查询 [banlist]
289
+ 自定义指令集 [custom]
290
+ 踢人 [kick] qq reason
267
291
  ```
268
292
 
269
293
  - 旧的图片菜单(`菜单` / `help` 接管 / `status` 状态图片 / 分级菜单 / 30 秒关键词配对)已**整体移除**,