qq-bot-roost 1.1.0 → 1.1.2
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/README.md +4 -2
- package/docs//345/256/230/346/226/271/346/216/245/345/217/243/351/231/220/345/210/266.md +32 -0
- package/package.json +3 -3
- package/src/api.js +22 -0
- package/src/bot.js +149 -8
- package/src/dev/platform.js +140 -9
- package/src/dev/url-guard.js +209 -0
- package/src/endpoints.js +2 -1
- package/src/groups.js +17 -1
- package/src/host/methods.js +191 -1
- package/src/host/serialize.js +15 -0
- package/src/ledger.js +202 -5
- package/src/message.js +66 -4
- package/src/onebot/actions.js +131 -17
- package/src/onebot/capability.js +1 -1
- package/src/onebot/translate.js +7 -3
- package/src/people-search.js +88 -0
- package/src/search-query.js +87 -0
- package/src/web/server.js +348 -7
- package/web/app.js +371 -15
- package/web/index.html +65 -1
- package/web/style.css +63 -2
package/README.md
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
|
|
9
9
|
<img src="https://img.shields.io/badge/Node.js-22.13%2B-339933?logo=node.js&logoColor=white" alt="Node.js 22.13+">
|
|
10
10
|
<img src="https://img.shields.io/badge/dependencies-0-brightgreen" alt="零依赖">
|
|
11
|
-
<img src="https://img.shields.io/badge/tests-
|
|
11
|
+
<img src="https://img.shields.io/badge/tests-1273%20passed-brightgreen" alt="测试 1273 项">
|
|
12
12
|
<img src="https://img.shields.io/badge/plugins-JS%20%7C%20Python%20%7C%20Go%20%7C%20any-blue" alt="多语言插件">
|
|
13
|
-
<img src="https://img.shields.io/badge/version-1.1.
|
|
13
|
+
<img src="https://img.shields.io/badge/version-1.1.1-blueviolet" alt="版本 1.1.1">
|
|
14
14
|
<img src="https://img.shields.io/badge/license-AGPL--3.0--only-blue" alt="AGPL-3.0-only">
|
|
15
15
|
<a href="https://github.com/tdcq9594/qq-bot-roost/actions"><img src="https://img.shields.io/github/actions/workflow/status/tdcq9594/qq-bot-roost/ci.yml?branch=master" alt="CI"></a>
|
|
16
16
|
|
|
@@ -288,6 +288,8 @@ docker compose up -d
|
|
|
288
288
|
| 群组 | 全局主人增删、群档案(邀请人 / 群主 / 状态)、手动登记群 |
|
|
289
289
|
| 权限 | 等级与作用域说明、按作用域增删管理员、已授权列表 |
|
|
290
290
|
| 日志 | SSE 实时流,按级别和来源过滤,支持关键词搜索 |
|
|
291
|
+
| 账本 | 每个会话的历史消息:发言人缩略头像 / 群名片 / QQ 昵称、会话显示群名,支持导出与按条件删除 |
|
|
292
|
+
| 查询 | 跨会话检索:关键词(多词默认全中,无结果自动放宽)+ 发言人(昵称 / openid / QQ号)+ 时间;另有**人物反查**:给昵称 / QQ号 / openid 任一个,查出这个人的其它字段,openid 一键复制 |
|
|
291
293
|
| 通知 | SMTP 服务器 / 端口 / 用户名 / 授权码 / 发件人 / 主人邮箱、测试邮件通知、普通成员自助绑定说明(位于顶栏「身份」与「关于」之间) |
|
|
292
294
|
|
|
293
295
|
事件页把官方 47 个事件全列出来,灰掉的是当前 intents 没订阅的:
|
|
@@ -265,6 +265,38 @@ const st = await ctx.api.canManageGroup(msg.groupId);
|
|
|
265
265
|
|
|
266
266
|
---
|
|
267
267
|
|
|
268
|
+
## 二·补二、用户资料与群档案(2026-11-16 官方新增)
|
|
269
|
+
|
|
270
|
+
| 接口 | 方法 | 路径 | 频率限制 |
|
|
271
|
+
|------|------|------|----------|
|
|
272
|
+
| 获取用户基本信息 | GET | `/v2/users/{user_openid}/info` | 10 QPS |
|
|
273
|
+
|
|
274
|
+
返回 `username`、`avatar`(原图,qlogo 地址)、`allow_proactive_msg`、
|
|
275
|
+
`union_openid`(绑了 3 方应用才有)。
|
|
276
|
+
|
|
277
|
+
**它要求好友关系。** 2026-11-16 用同一只机器人、两个**同群成员**实测:
|
|
278
|
+
没私聊过的那个回 `40184001 无好友关系`,私聊过(好友)的正常返回。
|
|
279
|
+
所以「查群成员资料」不能只靠它——宿主那边还有 `people.list`(最近说过话的
|
|
280
|
+
人,带昵称/群名片/头像)兜底,见 `src/host/methods.js`。
|
|
281
|
+
|
|
282
|
+
事件侧同时补了两样:
|
|
283
|
+
|
|
284
|
+
- 群聊 `author.member_nickname`(**群内昵称**,和 `username` 不是一回事)
|
|
285
|
+
与 `author.avatar`;单聊也补上了昵称。框架的 `msg.userName` 按
|
|
286
|
+
「群内昵称 → QQ 昵称」取,另给 `userNickname` / `userCard` 分开。
|
|
287
|
+
- 群信息接口多返回 `group_member_num`(人数)、`group_finger_memo`(简介)、
|
|
288
|
+
`group_class_text`(分类)、`group_tags`(标签)。**注意**:以前代码读的
|
|
289
|
+
`member_count` / `max_member_count` 两个字段都不存在,人数一直返回 0。
|
|
290
|
+
|
|
291
|
+
跨应用的 `union_openid` / `union_group_openid` **只透传,不做关联**:只有同一
|
|
292
|
+
开发者 + 绑了同一个 3 方应用时才可能相等,而且**换不出对方的 openid**。
|
|
293
|
+
跨机器人的身份/群关联仍走 `identity` 的「QQ号/群号 ↔ openid」绑定。
|
|
294
|
+
|
|
295
|
+
> 上面「用户 QQ 号 | openid 不可读」那条仍然成立:新接口给的是昵称、头像和
|
|
296
|
+
> union id,**没有把 QQ 号交出来**。
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
268
300
|
## 三、免@粘性怎么工作
|
|
269
301
|
|
|
270
302
|
用户 @ 过一次后,该会话在 `session.stickySec`(默认 180 秒)内,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qq-bot-roost",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "栖 Roost:零依赖的 QQ 官方机器人框架,填 AppID/AppSecret 即连,插件可写任意语言",
|
|
6
6
|
"license": "AGPL-3.0-only",
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"start": "node src/main.js",
|
|
30
30
|
"mock": "node src/main.js --mock",
|
|
31
31
|
"check": "node src/main.js --check",
|
|
32
|
-
"test": "node test/selftest.mjs && node test/qrlogin.mjs && node test/deps.mjs && node test/npm-registry.mjs && node test/deps-external.mjs && node --test test/trust.mjs && node --test test/manifest.mjs && node --test test/market-cache.mjs && node --test test/market-kind.mjs && node --test test/instance-proc.mjs && node --test test/proxy.mjs && node --test test/trace.mjs && node --test test/fuzz-tar.mjs && node --test test/group-name.mjs && node --test test/identity.mjs && node --test test/identity-commands.mjs && node --test test/mail.mjs && node --test test/ledger-outbound.mjs && node --test test/api-record.mjs && node --test test/reply-auto.mjs && node --test test/plugin-observability.mjs && node --test test/outbound-dry.mjs && node --test test/ext-deps-migrate.mjs && node --test test/onebot-deps.mjs && node --test test/onebot-config.mjs && node --test test/onebot-segments.mjs && node --test test/onebot-outbound-at.mjs && node --test test/onebot-translate.mjs && node --test test/onebot-voice.mjs && node --test test/onebot-loopback.mjs && node --test test/host-protocol.mjs && node --test test/host-config.mjs && node --test test/host-tool-methods.mjs && node --test test/host-ext-bridge.mjs && node --test test/web-token-echo.mjs && node --test test/group-manage.mjs && node --test test/wakeup-payload.mjs && node --test test/typing.mjs && node --test test/media-convert.mjs && node --test test/gateway-interaction-ack.mjs && node --test test/host-dev.mjs && node --test test/dev-browser.mjs && node --test test/dev-cooldown.mjs && node --test test/onebot-profile.mjs && node --test test/dev-remember.mjs && node --test test/dev-scope.mjs && node --test test/dev-renew.mjs && node --test test/dev-ticket.mjs && node --test test/nickname.mjs",
|
|
32
|
+
"test": "node test/selftest.mjs && node test/qrlogin.mjs && node test/deps.mjs && node test/npm-registry.mjs && node test/deps-external.mjs && node --test test/trust.mjs && node --test test/manifest.mjs && node --test test/market-cache.mjs && node --test test/market-kind.mjs && node --test test/instance-proc.mjs && node --test test/proxy.mjs && node --test test/trace.mjs && node --test test/fuzz-tar.mjs && node --test test/group-name.mjs && node --test test/message-fields.mjs && node --test test/people-list.mjs && node --test test/ledger-people.mjs && node --test test/ledger-search.mjs && node --test test/people-search.mjs && node --test test/host-ledger.mjs && node --test test/identity.mjs && node --test test/identity-commands.mjs && node --test test/mail.mjs && node --test test/ledger-outbound.mjs && node --test test/api-record.mjs && node --test test/reply-auto.mjs && node --test test/plugin-observability.mjs && node --test test/outbound-dry.mjs && node --test test/ext-deps-migrate.mjs && node --test test/onebot-deps.mjs && node --test test/onebot-config.mjs && node --test test/onebot-segments.mjs && node --test test/onebot-outbound-at.mjs && node --test test/onebot-translate.mjs && node --test test/onebot-voice.mjs && node --test test/onebot-loopback.mjs && node --test test/host-protocol.mjs && node --test test/host-config.mjs && node --test test/host-tool-methods.mjs && node --test test/host-ext-bridge.mjs && node --test test/web-token-echo.mjs && node --test test/group-manage.mjs && node --test test/wakeup-payload.mjs && node --test test/typing.mjs && node --test test/media-convert.mjs && node --test test/gateway-interaction-ack.mjs && node --test test/host-dev.mjs && node --test test/dev-browser.mjs && node --test test/dev-cooldown.mjs && node --test test/onebot-profile.mjs && node --test test/dev-remember.mjs && node --test test/dev-scope.mjs && node --test test/dev-renew.mjs && node --test test/dev-ticket.mjs && node --test test/nickname.mjs && node --test test/url-guard.mjs",
|
|
33
33
|
"test:e2e": "node test/e2e.mjs",
|
|
34
|
-
"test:all": "node test/selftest.mjs && node test/qrlogin.mjs && node test/deps.mjs && node test/npm-registry.mjs && node test/deps-external.mjs && node --test test/trust.mjs && node --test test/manifest.mjs && node --test test/market-cache.mjs && node --test test/market-kind.mjs && node --test test/instance-proc.mjs && node --test test/proxy.mjs && node --test test/trace.mjs && node --test test/fuzz-tar.mjs && node --test test/group-name.mjs && node --test test/identity.mjs && node --test test/identity-commands.mjs && node --test test/mail.mjs && node --test test/ledger-outbound.mjs && node --test test/api-record.mjs && node --test test/reply-auto.mjs && node test/e2e.mjs && node test/live-driver.mjs && node --test test/ext-deps-migrate.mjs && node --test test/onebot-deps.mjs && node --test test/onebot-config.mjs && node --test test/onebot-segments.mjs && node --test test/onebot-outbound-at.mjs && node --test test/onebot-translate.mjs && node --test test/onebot-voice.mjs && node --test test/onebot-loopback.mjs && node --test test/host-protocol.mjs && node --test test/host-config.mjs && node --test test/host-tool-methods.mjs && node --test test/host-ext-bridge.mjs && node --test test/web-token-echo.mjs && node --test test/group-manage.mjs && node --test test/wakeup-payload.mjs && node --test test/typing.mjs && node --test test/media-convert.mjs && node --test test/host-dev.mjs && node --test test/dev-browser.mjs && node --test test/dev-cooldown.mjs && node --test test/onebot-profile.mjs && node --test test/dev-remember.mjs && node --test test/dev-scope.mjs && node --test test/dev-renew.mjs && node --test test/dev-ticket.mjs && node --test test/nickname.mjs",
|
|
34
|
+
"test:all": "node test/selftest.mjs && node test/qrlogin.mjs && node test/deps.mjs && node test/npm-registry.mjs && node test/deps-external.mjs && node --test test/trust.mjs && node --test test/manifest.mjs && node --test test/market-cache.mjs && node --test test/market-kind.mjs && node --test test/instance-proc.mjs && node --test test/proxy.mjs && node --test test/trace.mjs && node --test test/fuzz-tar.mjs && node --test test/group-name.mjs && node --test test/message-fields.mjs && node --test test/people-list.mjs && node --test test/ledger-people.mjs && node --test test/ledger-search.mjs && node --test test/people-search.mjs && node --test test/host-ledger.mjs && node --test test/identity.mjs && node --test test/identity-commands.mjs && node --test test/mail.mjs && node --test test/ledger-outbound.mjs && node --test test/api-record.mjs && node --test test/reply-auto.mjs && node test/e2e.mjs && node test/live-driver.mjs && node --test test/ext-deps-migrate.mjs && node --test test/onebot-deps.mjs && node --test test/onebot-config.mjs && node --test test/onebot-segments.mjs && node --test test/onebot-outbound-at.mjs && node --test test/onebot-translate.mjs && node --test test/onebot-voice.mjs && node --test test/onebot-loopback.mjs && node --test test/host-protocol.mjs && node --test test/host-config.mjs && node --test test/host-tool-methods.mjs && node --test test/host-ext-bridge.mjs && node --test test/web-token-echo.mjs && node --test test/group-manage.mjs && node --test test/wakeup-payload.mjs && node --test test/typing.mjs && node --test test/media-convert.mjs && node --test test/host-dev.mjs && node --test test/dev-browser.mjs && node --test test/dev-cooldown.mjs && node --test test/onebot-profile.mjs && node --test test/dev-remember.mjs && node --test test/dev-scope.mjs && node --test test/dev-renew.mjs && node --test test/dev-ticket.mjs && node --test test/nickname.mjs && node --test test/url-guard.mjs",
|
|
35
35
|
"check:secrets": "node scripts/check-secrets.mjs",
|
|
36
36
|
"create": "node scripts/create-plugin.mjs",
|
|
37
37
|
"test:plugin": "node scripts/test-plugin.mjs",
|
package/src/api.js
CHANGED
|
@@ -436,6 +436,28 @@ export class QQApi {
|
|
|
436
436
|
return this.#request('GET', '/users/@me');
|
|
437
437
|
}
|
|
438
438
|
|
|
439
|
+
/**
|
|
440
|
+
* 获取用户基本信息(公开接口)。
|
|
441
|
+
* GET /v2/users/{user_openid}/info
|
|
442
|
+
*
|
|
443
|
+
* 2026-11-16 新增。这是**第一次**能主动问出「这个人叫什么、头像是哪张、
|
|
444
|
+
* 收不收主动推送」——以前这三件事只能靠事件慢慢攒,攒不到就永远是 openid。
|
|
445
|
+
*
|
|
446
|
+
* 返回的关键字段:
|
|
447
|
+
* · `username` / `avatar` 昵称、头像原图
|
|
448
|
+
* · `allow_proactive_msg` 是否允许机器人主动推送(false 就别发,省额度)
|
|
449
|
+
* · `union_openid` 跨应用统一 id(绑了 3 方应用才有)
|
|
450
|
+
*
|
|
451
|
+
* 限 **10 QPS**,而且**要求好友关系**——2026-11-16 用两个同群成员实测:
|
|
452
|
+
* 没私聊过的那个回 `40184001 无好友关系`,私聊过(好友)的正常返回。
|
|
453
|
+
* 所以「查群成员资料」不能只靠它,宿主那边还有 `people.list` 兜底。
|
|
454
|
+
*
|
|
455
|
+
* 别在消息热路径里每条都调——`msg.userName` / `msg.userAvatar` 事件里直接就有。
|
|
456
|
+
*/
|
|
457
|
+
getUserInfo(userOpenid) {
|
|
458
|
+
return this.#request('GET', `/v2/users/${enc(userOpenid)}/info`);
|
|
459
|
+
}
|
|
460
|
+
|
|
439
461
|
/* ---------------- 消息发送 ---------------- */
|
|
440
462
|
|
|
441
463
|
/**
|
package/src/bot.js
CHANGED
|
@@ -89,15 +89,19 @@ export class BotRuntime {
|
|
|
89
89
|
*/
|
|
90
90
|
this.recentPeople = new Map();
|
|
91
91
|
/**
|
|
92
|
-
*
|
|
92
|
+
* 群名 / 群档案缓存:groupOpenid -> { name, memberNum, fingerMemo, …, at, ttl }
|
|
93
93
|
*
|
|
94
94
|
* **官方事件里没有群名**(实测 GROUP_AT_MESSAGE_CREATE 的字段只有
|
|
95
95
|
* author/content/group_id/group_openid/id/message_scene/message_type/timestamp),
|
|
96
96
|
* 而面板上只显示 `577C4A90…` 这种截断的 openid,用户根本认不出是哪个群。
|
|
97
97
|
*
|
|
98
98
|
* 所以在入群 / 首次收到消息时异步拉一次 `getGroupInfo` 补上,
|
|
99
|
-
*
|
|
100
|
-
*
|
|
99
|
+
* 之后面板直接读缓存。
|
|
100
|
+
*
|
|
101
|
+
* 现在会往群档案里写一份(带 `infoAt`)。以前是「不落盘」,理由是
|
|
102
|
+
* 「群名会变,存下来反而错」——但那样重启后第一屏永远是一串 openid。
|
|
103
|
+
* 改成落盘 + 时间戳后,TTL 仍按 `at` 算:过期就重新拉,存的只是
|
|
104
|
+
* 上一次问到的结果,不是权威值。
|
|
101
105
|
*/
|
|
102
106
|
this.groupNames = new Map();
|
|
103
107
|
/** 同群并发补名时复用同一个请求,避免一条消息打两次接口 */
|
|
@@ -110,6 +114,41 @@ export class BotRuntime {
|
|
|
110
114
|
*/
|
|
111
115
|
this.groupNameActive = 0;
|
|
112
116
|
this.groupNameWaiters = [];
|
|
117
|
+
/**
|
|
118
|
+
* 群信息接口的节奏控制:`getGroupInfo` 是 **30 QPM**。
|
|
119
|
+
*
|
|
120
|
+
* 并发上限(4)只防「同一瞬间打太多」,防不住「一分钟打了 40 次」——
|
|
121
|
+
* 面板一次补 20 个群就能撞上去。所以另存一个「下一个请求最早什么时候发」,
|
|
122
|
+
* 默认 2.2 秒一个(27 次/分钟,留 10% 余量)。
|
|
123
|
+
*/
|
|
124
|
+
this.groupInfoNextAt = 0;
|
|
125
|
+
this.groupInfoGapMs = 2200;
|
|
126
|
+
/**
|
|
127
|
+
* 启动预载:档案里存过群名的,直接灌进缓存。
|
|
128
|
+
*
|
|
129
|
+
* 不预载的话,重启后面板第一屏必然是一串 openid,要等群里有人说句话
|
|
130
|
+
* 才会慢慢补上。`at` 存的是上次真拉到的时间,所以过期条目第一次用到
|
|
131
|
+
* 就会按正常 TTL 重新拉,不会永远吃旧名字。
|
|
132
|
+
*
|
|
133
|
+
* 不 await:这是显示用的,挡在启动路径上没意义;失败就当时没有。
|
|
134
|
+
*/
|
|
135
|
+
this.groups.list().then((list) => {
|
|
136
|
+
for (const g of list) {
|
|
137
|
+
const id = g?.groupOpenid;
|
|
138
|
+
if (!id || this.groupNames.has(id)) continue;
|
|
139
|
+
/** 一条信息都没有就别占位(有名字/人数/简介任一项就值得预载) */
|
|
140
|
+
if (!g.name && !g.memberNum && !g.fingerMemo) continue;
|
|
141
|
+
this.groupNames.set(id, {
|
|
142
|
+
name: g.name || '',
|
|
143
|
+
memberNum: Number(g.memberNum) || 0,
|
|
144
|
+
fingerMemo: g.fingerMemo || '',
|
|
145
|
+
classText: g.classText || '',
|
|
146
|
+
tags: Array.isArray(g.tags) ? g.tags : [],
|
|
147
|
+
at: Number(g.infoAt) || 0,
|
|
148
|
+
ttl: 10 * 60_000,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}).catch(() => {});
|
|
113
152
|
this.roles = new RoleStore(this.paths.roles);
|
|
114
153
|
/**
|
|
115
154
|
* 可达性:谁能私聊、哪个群能主动推。
|
|
@@ -508,6 +547,15 @@ export class BotRuntime {
|
|
|
508
547
|
openid: msg.userId,
|
|
509
548
|
// 有的场景拿不到昵称(频道事件),保留上一次的
|
|
510
549
|
name: msg.userName || prev?.name || '',
|
|
550
|
+
/** 群名片 / QQ 昵称分开存:面板上「群里的名字」和「QQ 昵称」经常不一样 */
|
|
551
|
+
card: msg.userCard || prev?.card || '',
|
|
552
|
+
nickname: msg.userNickname || prev?.nickname || '',
|
|
553
|
+
/** 头像原图;2026-11-16 起事件里直接带,面板能显示人脸比一串 openid 好认得多 */
|
|
554
|
+
avatar: msg.userAvatar || prev?.avatar || '',
|
|
555
|
+
/** 跨应用统一 id(绑 3 方应用才有),多机器人对齐同一个人靠它 */
|
|
556
|
+
unionOpenid: msg.unionUserId || prev?.unionOpenid || '',
|
|
557
|
+
/** QQ号(面板绑过才有):宿主认人时号码比 openid 可靠,换机器人也不变 */
|
|
558
|
+
uin: msg.uin || prev?.uin || '',
|
|
511
559
|
scope: msg.kind,
|
|
512
560
|
chatId: msg.chatId,
|
|
513
561
|
at: Date.now(),
|
|
@@ -534,6 +582,17 @@ export class BotRuntime {
|
|
|
534
582
|
return this.groupNames.get(groupOpenid)?.name || undefined;
|
|
535
583
|
}
|
|
536
584
|
|
|
585
|
+
/**
|
|
586
|
+
* 查群档案缓存(名字 / 人数 / 简介 / 标签 / 跨应用群标识)。
|
|
587
|
+
*
|
|
588
|
+
* 和 `groupName()` 是同一份缓存,只是把整份给出去。没有就 null——
|
|
589
|
+
* 调用方自己决定是显示 openid 还是先触发一次补拉(`fetchGroupNames`)。
|
|
590
|
+
*/
|
|
591
|
+
groupInfo(groupOpenid) {
|
|
592
|
+
if (!groupOpenid) return null;
|
|
593
|
+
return this.groupNames.get(groupOpenid) ?? null;
|
|
594
|
+
}
|
|
595
|
+
|
|
537
596
|
/**
|
|
538
597
|
* 用 6 个事件维护可达性。
|
|
539
598
|
*
|
|
@@ -636,6 +695,34 @@ export class BotRuntime {
|
|
|
636
695
|
clearTimeout(timer);
|
|
637
696
|
}
|
|
638
697
|
|
|
698
|
+
/**
|
|
699
|
+
* 批量补群名:排进后台,按 30 QPM 慢慢拉,**不等结果**。
|
|
700
|
+
*
|
|
701
|
+
* 为什么需要它:`getGroupInfo` 是 30 QPM,补 20 个群要 40 多秒。面板请求
|
|
702
|
+
* 不可能等那么久(`fetchGroupNames` 自带 2.5 秒超时,到点就不再派活,
|
|
703
|
+
* 没派出去的会丢)。所以面板那边「先显示能显示的」,剩下的交给这里:
|
|
704
|
+
* 全部丢进 `#ensureGroupName`(并发 4 + 时间闸),它自己排队、自己落盘,
|
|
705
|
+
* 下次打开面板就都是名字了。
|
|
706
|
+
*
|
|
707
|
+
* 重复调用安全:`#ensureGroupName` 有 in-flight 去重,缓存未过期时直接返回。
|
|
708
|
+
*
|
|
709
|
+
* @param {string[]} ids
|
|
710
|
+
* @returns {number} 真正排进去的数量(已缓存的不算)
|
|
711
|
+
*/
|
|
712
|
+
prefetchGroupNames(ids = []) {
|
|
713
|
+
let queued = 0;
|
|
714
|
+
for (const raw of Array.isArray(ids) ? ids : []) {
|
|
715
|
+
const id = String(raw ?? '').trim();
|
|
716
|
+
if (!id) continue;
|
|
717
|
+
const hit = this.groupNames.get(id);
|
|
718
|
+
if (hit && Date.now() - hit.at < (hit.ttl ?? 10 * 60_000)) continue;
|
|
719
|
+
if (this.groupNameInflight.has(id)) continue;
|
|
720
|
+
queued += 1;
|
|
721
|
+
this.#ensureGroupName(id).catch(() => {});
|
|
722
|
+
}
|
|
723
|
+
return queued;
|
|
724
|
+
}
|
|
725
|
+
|
|
639
726
|
/**
|
|
640
727
|
* 补一个群的名字。
|
|
641
728
|
*
|
|
@@ -667,8 +754,25 @@ export class BotRuntime {
|
|
|
667
754
|
await this.#groupNameSlot();
|
|
668
755
|
try {
|
|
669
756
|
const info = await this.api.getGroupInfo(groupOpenid);
|
|
670
|
-
const
|
|
671
|
-
this.groupNames.set(groupOpenid, {
|
|
757
|
+
const profile = groupProfileOf(info);
|
|
758
|
+
this.groupNames.set(groupOpenid, { ...profile, at: Date.now(), ttl: 10 * 60_000 });
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* 顺手落盘一份。
|
|
762
|
+
*
|
|
763
|
+
* 群名 / 人数 / 简介 / 标签以前只在内存里活 10 分钟,重启就没了,
|
|
764
|
+
* 面板第一屏永远是一串 openid。档案里存一份,重启直接读。
|
|
765
|
+
*
|
|
766
|
+
* **只写非空字段**:接口少给某个字段时不能把已有值覆盖成空串
|
|
767
|
+
* (`upsert` 是浅合并,空串会盖掉旧值)。
|
|
768
|
+
*/
|
|
769
|
+
const patch = { infoAt: Date.now() };
|
|
770
|
+
if (profile.name) patch.name = profile.name;
|
|
771
|
+
if (profile.memberNum) patch.memberNum = profile.memberNum;
|
|
772
|
+
if (profile.fingerMemo) patch.fingerMemo = profile.fingerMemo;
|
|
773
|
+
if (profile.classText) patch.classText = profile.classText;
|
|
774
|
+
if (profile.tags.length) patch.tags = profile.tags;
|
|
775
|
+
await this.groups?.upsert?.(groupOpenid, patch);
|
|
672
776
|
} catch (e) {
|
|
673
777
|
// 拉不到就算了:openid 也能认,只是没那么好认
|
|
674
778
|
/** api.js 抛的是 `qqCode`,不是 code;读错字段会让 11253 永远按 30 秒重试 */
|
|
@@ -689,13 +793,29 @@ export class BotRuntime {
|
|
|
689
793
|
return job;
|
|
690
794
|
}
|
|
691
795
|
|
|
692
|
-
/** 拿一个群名请求的名额(最多 4
|
|
796
|
+
/** 拿一个群名请求的名额(最多 4 个同时在飞),再按 30 QPM 放行时间点 */
|
|
693
797
|
async #groupNameSlot() {
|
|
694
798
|
if (this.groupNameActive < 4) {
|
|
695
799
|
this.groupNameActive += 1;
|
|
696
|
-
|
|
800
|
+
} else {
|
|
801
|
+
await new Promise((resolve) => this.groupNameWaiters.push(resolve));
|
|
697
802
|
}
|
|
698
|
-
|
|
803
|
+
/**
|
|
804
|
+
* 并发上限只管「同时在飞几个」,不管「一分钟打了多少次」。
|
|
805
|
+
*
|
|
806
|
+
* 一次刷新面板拉 20 个群,4 并发几秒钟就能打满 30 次,然后整批 429。
|
|
807
|
+
* 所以拿到名额后还要过一道**时间闸**:默认 2.2 秒一个(27 次/分钟,
|
|
808
|
+
* 留 10% 余量)。代价是 20 个群要补 40 多秒——但这是显示用的,
|
|
809
|
+
* 面板等不到就先显示 openid,后台继续补,下次打开就是名字了。
|
|
810
|
+
*
|
|
811
|
+
* `groupInfoGapMs` 可以调(测试里设小值),0 = 不限制。
|
|
812
|
+
*/
|
|
813
|
+
const gap = Math.max(0, Number(this.groupInfoGapMs) || 0);
|
|
814
|
+
if (!gap) return;
|
|
815
|
+
const now = Date.now();
|
|
816
|
+
const at = Math.max(now, this.groupInfoNextAt || 0);
|
|
817
|
+
this.groupInfoNextAt = at + gap;
|
|
818
|
+
if (at > now) await new Promise((r) => setTimeout(r, at - now));
|
|
699
819
|
}
|
|
700
820
|
|
|
701
821
|
#freeGroupNameSlot() {
|
|
@@ -1323,6 +1443,27 @@ export function pluginFilterOf(spec) {
|
|
|
1323
1443
|
return (id) => list.has(id); // include
|
|
1324
1444
|
}
|
|
1325
1445
|
|
|
1446
|
+
/**
|
|
1447
|
+
* 群基本信息接口的返回 → 框架内部形状。
|
|
1448
|
+
*
|
|
1449
|
+
* 2026-11-16 官方把 `/v2/groups/{openid}/info` 从「只给群名」扩成一份完整档案
|
|
1450
|
+
* (人数 / 简介 / 分类 / 标签)。字段都取一遍,缺的给空值——接口少给不该让
|
|
1451
|
+
* 调用方拿到 undefined。
|
|
1452
|
+
*
|
|
1453
|
+
* **`union_group_openid` 不要**:跨机器人的群关联走 `identity` 的「群号 ↔ openid」
|
|
1454
|
+
* 绑定,那个字段只在同一开发者 + 同一个 3 方应用下才有值,还换不出对方的 openid。
|
|
1455
|
+
*/
|
|
1456
|
+
function groupProfileOf(info) {
|
|
1457
|
+
const src = info && typeof info === 'object' ? info : {};
|
|
1458
|
+
return {
|
|
1459
|
+
name: String(src.group_name ?? src.name ?? '').trim(),
|
|
1460
|
+
memberNum: Number(src.group_member_num) || 0,
|
|
1461
|
+
fingerMemo: String(src.group_finger_memo ?? '').trim(),
|
|
1462
|
+
classText: String(src.group_class_text ?? '').trim(),
|
|
1463
|
+
tags: Array.isArray(src.group_tags) ? src.group_tags.map(String).filter(Boolean) : [],
|
|
1464
|
+
};
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1326
1467
|
/**
|
|
1327
1468
|
* 这个会话里能不能用某个插件(分群开关)。
|
|
1328
1469
|
*
|
package/src/dev/platform.js
CHANGED
|
@@ -37,6 +37,7 @@ import { DIRS } from '../paths.js';
|
|
|
37
37
|
import { createDevSession, parseCookies, bknOf, DEV_HOST, TICKET_KEYS } from './session.js';
|
|
38
38
|
import { DEV_ERRORS, devError } from './errors.js';
|
|
39
39
|
import { createScanLogin } from './browser.js';
|
|
40
|
+
import { fetchImageLimited } from './url-guard.js';
|
|
40
41
|
|
|
41
42
|
export { bknOf, DEV_HOST };
|
|
42
43
|
|
|
@@ -188,7 +189,7 @@ export function isAuthFailure(e) {
|
|
|
188
189
|
/**
|
|
189
190
|
* @param {{log?:(m:string)=>void, fetchImpl?:typeof fetch}} o
|
|
190
191
|
*/
|
|
191
|
-
export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
192
|
+
export function createDevPlatform({ log = () => {}, fetchImpl = fetch, lookup = null } = {}) {
|
|
192
193
|
/**
|
|
193
194
|
* 传输层:**默认走 Node 直发**。
|
|
194
195
|
*
|
|
@@ -239,6 +240,72 @@ export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
|
239
240
|
/** 本次扫码要不要把票据落到本机(面板上的「记住登录」,发起时定) */
|
|
240
241
|
let scanRemember = false;
|
|
241
242
|
|
|
243
|
+
/**
|
|
244
|
+
* 用户明确关掉了「记住登录」。
|
|
245
|
+
*
|
|
246
|
+
* 光靠「文件在不在」判断不够:取消勾选是删文件,而**正在跑的自动续期**
|
|
247
|
+
* 可能在删之前已经判断过 `hasSaved()`,换票成功后一 `save` 就把文件写回来——
|
|
248
|
+
* 表现就是「取消了没用,刷新还是已记住」。所以除了删文件,还落一个会话级
|
|
249
|
+
* 标志,续期那条路径看到它就不许写盘。
|
|
250
|
+
*/
|
|
251
|
+
let rememberOff = false;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* 「记住登录」偏好**落到后端**。
|
|
255
|
+
*
|
|
256
|
+
* 之前这个选择只存在浏览器的 localStorage 里,后端完全不知道——于是重启时
|
|
257
|
+
* `devTicket` 看到磁盘上有票(哪怕早过期了)就照旧开无头浏览器换票,
|
|
258
|
+
* 用户看到的是「我都没登录,怎么又在后台登录」。
|
|
259
|
+
*
|
|
260
|
+
* 现在偏好写盘,启动时按它决定:
|
|
261
|
+
* · `remember:false` → 连**历史残留**一起清(票 + 扫码 profile),不自动续期;
|
|
262
|
+
* · 没存过 → 按 `true`(保持老行为,不能因为升级就把大家的登录踢掉)。
|
|
263
|
+
*/
|
|
264
|
+
const prefFile = () => path.join(DIRS.data, 'dev-remember.json');
|
|
265
|
+
let prefLoaded = null;
|
|
266
|
+
|
|
267
|
+
async function loadPref() {
|
|
268
|
+
try {
|
|
269
|
+
const j = JSON.parse(await fs.readFile(prefFile(), 'utf8'));
|
|
270
|
+
rememberOff = j?.remember === false;
|
|
271
|
+
} catch {
|
|
272
|
+
/** 没存过 = 默认记住(老用户升级后行为不变) */
|
|
273
|
+
rememberOff = false;
|
|
274
|
+
}
|
|
275
|
+
if (rememberOff) {
|
|
276
|
+
/**
|
|
277
|
+
* 用户说过「别记住」:把上一版留下的东西收拾干净。
|
|
278
|
+
*
|
|
279
|
+
* 旧版本取消勾选只删票、不删 profile,所以升级后第一次启动必须补这一刀,
|
|
280
|
+
* 否则「我没登录」和「后台在换票」会同时成立。
|
|
281
|
+
*/
|
|
282
|
+
await wipe();
|
|
283
|
+
try {
|
|
284
|
+
if (!scanner.status?.().busy) await scanner.purgeProfile?.();
|
|
285
|
+
} catch (e) {
|
|
286
|
+
log(`[dev] 清理扫码 profile 失败(不影响启动):${e?.message ?? e}`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return rememberOff;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** 懒加载:构造函数是同步的,所以第一次用到时再读 */
|
|
293
|
+
function ensurePref() {
|
|
294
|
+
prefLoaded ??= loadPref();
|
|
295
|
+
return prefLoaded;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async function savePref(on) {
|
|
299
|
+
try {
|
|
300
|
+
await fs.mkdir(DIRS.data, { recursive: true });
|
|
301
|
+
await fs.writeFile(prefFile(), JSON.stringify({ remember: Boolean(on), at: Date.now() }, null, 2), {
|
|
302
|
+
mode: 0o600,
|
|
303
|
+
});
|
|
304
|
+
} catch (e) {
|
|
305
|
+
log(`[dev] 记住登录偏好没写成:${e?.message ?? e}`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
242
309
|
/** 手填的浏览器路径,空 = 自动探测;扫码器每次 start 时现读,改了立刻生效 */
|
|
243
310
|
let browserPath = '';
|
|
244
311
|
|
|
@@ -257,7 +324,8 @@ export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
|
257
324
|
session.adopt(jar);
|
|
258
325
|
try {
|
|
259
326
|
const bots = await listBots();
|
|
260
|
-
|
|
327
|
+
/** 扫码成功写盘同样要尊重「记住」开关:`scanRemember` 是发起时的选择,可能已过期 */
|
|
328
|
+
if (scanRemember && !rememberOff) await save(Object.fromEntries(session.jar()));
|
|
261
329
|
return bots.length;
|
|
262
330
|
} catch (e) {
|
|
263
331
|
/**
|
|
@@ -308,6 +376,14 @@ export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
|
308
376
|
let autoRenewBackoffUntil = 0;
|
|
309
377
|
|
|
310
378
|
async function autoRelogin() {
|
|
379
|
+
/**
|
|
380
|
+
* 偏好先读出来:`remember:false` 的人,**任何路径都不该被开浏览器**。
|
|
381
|
+
*
|
|
382
|
+
* 这就是「我都没登录,怎么又在后台登录」的根:以前只看磁盘上有没有票,
|
|
383
|
+
* 而票是上一轮留下的、早就过期了。
|
|
384
|
+
*/
|
|
385
|
+
await ensurePref();
|
|
386
|
+
if (rememberOff) return false;
|
|
311
387
|
if (autoRenewDisabled || suspendAutoRenew || autoRenewing) return false;
|
|
312
388
|
if (Date.now() < autoRenewBackoffUntil) return false;
|
|
313
389
|
|
|
@@ -422,7 +498,13 @@ export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
|
422
498
|
}
|
|
423
499
|
// 整份替换:新票和旧票混着用会 401(旧 cookie 可能已经被平台作废)
|
|
424
500
|
session.adopt(r.jar);
|
|
425
|
-
|
|
501
|
+
/**
|
|
502
|
+
* 写回票据要同时满足两条:原本就存过 + 用户没关掉「记住」。
|
|
503
|
+
*
|
|
504
|
+
* 只看 `hasSaved()` 会有竞态:取消勾选删文件的同时续期正好在写,
|
|
505
|
+
* 文件被复活,面板又显示「已记住」。`rememberOff` 是会话内的硬否决。
|
|
506
|
+
*/
|
|
507
|
+
if (!rememberOff && (await hasSaved())) await save(Object.fromEntries(session.jar())).catch(() => {});
|
|
426
508
|
// 换票之后「账号下有几只」可能变(换号了),缓存必须作废
|
|
427
509
|
botsCache = null;
|
|
428
510
|
autoRenewBackoffUntil = 0;
|
|
@@ -631,7 +713,12 @@ export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
|
631
713
|
suspendAutoRenew = true;
|
|
632
714
|
try {
|
|
633
715
|
const bots = await listBots();
|
|
716
|
+
/** 导入时勾没勾「记住」同样落一个否决位,后面自动续期才不会把文件写回来 */
|
|
717
|
+
rememberOff = !remember;
|
|
718
|
+
await savePref(remember);
|
|
634
719
|
if (remember) await save(Object.fromEntries(session.jar()));
|
|
720
|
+
/** 选了「不记住」就把旧票也清掉:留着的话下次启动又会被当成「已记住」 */
|
|
721
|
+
else await wipe();
|
|
635
722
|
// keys 只回**键名**,值不出后端:名字够用来判断「是不是漏了 httpOnly 的那个」
|
|
636
723
|
return { ok: true, ...session.describe(), botCount: bots.length, keys };
|
|
637
724
|
} catch (e) {
|
|
@@ -657,11 +744,29 @@ export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
|
657
744
|
*/
|
|
658
745
|
async function setRemember(on) {
|
|
659
746
|
const want = Boolean(on);
|
|
747
|
+
rememberOff = !want;
|
|
748
|
+
/** 偏好落到后端:重启后只有它知道该不该自动续期 */
|
|
749
|
+
await savePref(want);
|
|
660
750
|
if (want) {
|
|
661
751
|
requireReady();
|
|
662
752
|
await save(Object.fromEntries(session.jar()));
|
|
663
753
|
} else {
|
|
664
754
|
await wipe();
|
|
755
|
+
/**
|
|
756
|
+
* **取消「记住登录」= 本机不再保留登录态**,所以连扫码用的浏览器 profile
|
|
757
|
+
* 一起清掉。
|
|
758
|
+
*
|
|
759
|
+
* 只删票据文件是不够的:那份 profile 里躺着 QQ 的长期登录 cookie,
|
|
760
|
+
* 后台续期会拿它自动换票——用户看到的是「我明明没让它记住,怎么还每次
|
|
761
|
+
* 都在后台登录」。清掉之后 `autoRelogin` 的前提(profile 或存过票)
|
|
762
|
+
* 不成立,自然不再开浏览器;内存里的票仍然可用,重启才需要重扫。
|
|
763
|
+
*/
|
|
764
|
+
try {
|
|
765
|
+
/** 正在扫码时别清:那会把用户眼前那个窗口一起关掉,等他扫完再说 */
|
|
766
|
+
if (!scanner.status?.().busy) await scanner.purgeProfile?.();
|
|
767
|
+
} catch (e) {
|
|
768
|
+
log(`[dev] 清理扫码 profile 失败(不影响本次会话):${e?.message ?? e}`);
|
|
769
|
+
}
|
|
665
770
|
}
|
|
666
771
|
return { ok: true, remembered: await hasSaved() };
|
|
667
772
|
}
|
|
@@ -692,6 +797,11 @@ export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
|
692
797
|
await autoRenewPromise.catch(() => false);
|
|
693
798
|
}
|
|
694
799
|
scanRemember = Boolean(remember);
|
|
800
|
+
/** 发起扫码时也同步一次否决位:面板可能刚把勾去掉,而 `scanRemember` 是上次的 */
|
|
801
|
+
rememberOff = !scanRemember;
|
|
802
|
+
await savePref(scanRemember);
|
|
803
|
+
/** 同上:本次不记就顺手清掉旧票,别让上次的选择继续生效 */
|
|
804
|
+
if (!scanRemember) await wipe();
|
|
695
805
|
return scanner.start();
|
|
696
806
|
}
|
|
697
807
|
|
|
@@ -700,9 +810,19 @@ export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
|
700
810
|
}
|
|
701
811
|
|
|
702
812
|
async function status() {
|
|
813
|
+
/** 读偏好时会顺手清理「取消记住」留下的残留,面板拿到的就是清理后的真实状态 */
|
|
814
|
+
await ensurePref();
|
|
703
815
|
return {
|
|
704
816
|
...session.describe(),
|
|
705
817
|
remembered: await hasSaved(),
|
|
818
|
+
/**
|
|
819
|
+
* 扫码 profile 还在不在。
|
|
820
|
+
*
|
|
821
|
+
* 面板要用它判断「取消记住之后是不是真清干净了」:只删票据文件、profile
|
|
822
|
+
* 还留着的话,后台续期照样能自动登录——用户看到的是「我没让它记住,
|
|
823
|
+
* 怎么每次打开还在登录」。
|
|
824
|
+
*/
|
|
825
|
+
profile: Boolean(await scanner.hasProfile?.()),
|
|
706
826
|
lastWrite,
|
|
707
827
|
/** 手填的浏览器路径(空 = 自动探测);面板要回显,改完不用猜 */
|
|
708
828
|
browserPath,
|
|
@@ -1136,12 +1256,23 @@ export function createDevPlatform({ log = () => {}, fetchImpl = fetch } = {}) {
|
|
|
1136
1256
|
|
|
1137
1257
|
const u = String(url || '').trim();
|
|
1138
1258
|
if (!/^https?:\/\//i.test(u)) throw devError('BAD_IMAGE', '要么选一张本地图片,要么填 http(s) 图片地址');
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1259
|
+
/**
|
|
1260
|
+
* 出站校验不能只验 `^https?://`:OneBot 的 `set_qq_profile` 是**模型可触发**的,
|
|
1261
|
+
* 裸 fetch 等于把「让宿主访问任意地址」开给模型(内网 / 云元数据 / 端口探测)。
|
|
1262
|
+
* `fetchImageLimited` 逐跳校验、禁私有地址、按 2MB 流式截断。
|
|
1263
|
+
*/
|
|
1264
|
+
let got;
|
|
1265
|
+
try {
|
|
1266
|
+
got = await fetchImageLimited(u, {
|
|
1267
|
+
maxBytes: AVATAR_MAX_BYTES,
|
|
1268
|
+
fetchImpl,
|
|
1269
|
+
...(lookup ? { lookup } : {}),
|
|
1270
|
+
});
|
|
1271
|
+
} catch (e) {
|
|
1272
|
+
throw devError('BAD_IMAGE', `取图失败:${e?.message ?? e}`);
|
|
1273
|
+
}
|
|
1274
|
+
const type = check(got.type, got.buf.length);
|
|
1275
|
+
return { blob: new Blob([got.buf], { type }), filename: `avatar.${type.split('/')[1] || 'png'}`, bytes: got.buf.length };
|
|
1145
1276
|
}
|
|
1146
1277
|
|
|
1147
1278
|
/**
|