qq-bot-roost 1.1.9 → 1.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/README.md +37 -27
- package/docs/images/bot-switcher.jpg +0 -0
- package/docs/images/events.jpg +0 -0
- package/docs/images/logs.jpg +0 -0
- package/docs/images/overview.jpg +0 -0
- package/docs/images/plugins.jpg +0 -0
- package/package.json +3 -3
- package/src/api.js +264 -3
- package/src/bot.js +2 -0
- package/src/config.js +37 -0
- package/src/deps-mirror.js +222 -0
- package/src/ext-deps.js +150 -17
- package/src/faces/index.js +236 -0
- package/src/faces/table.js +350 -0
- package/src/gateway.js +84 -15
- package/src/host/index.js +16 -7
- package/src/host/methods.js +1 -0
- package/src/main.js +7 -2
- package/src/md-image.js +208 -0
- package/src/message.js +15 -1
- package/src/web/server.js +24 -1
- package/web/app.js +48 -8
- package/docs/images/bot-switcher.png +0 -0
- package/docs/images/events.png +0 -0
- package/docs/images/logs.png +0 -0
- package/docs/images/overview.png +0 -0
- package/docs/images/plugins.png +0 -0
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-1410%20passed-brightgreen" alt="测试 1410 项">
|
|
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.10-blueviolet" alt="版本 1.1.10">
|
|
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/roost-qq/qq-bot-roost/actions"><img src="https://img.shields.io/github/actions/workflow/status/roost-qq/qq-bot-roost/ci.yml?branch=master" alt="CI"></a>
|
|
16
16
|
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
</div>
|
|
20
20
|
|
|
21
|
-

|
|
22
22
|
|
|
23
23
|
用 Node.js 从零实现 QQ 开放平台 v2 协议,**不依赖任何第三方包**——装个 Node 22 就能跑,
|
|
24
24
|
不需要 `npm install`,**装插件和更新框架也不需要 git**(下载 GitHub 的 tar 包自己解,不调外部命令)。
|
|
@@ -115,7 +115,7 @@
|
|
|
115
115
|
- **同会话串行、跨会话并行**:插件不用自己加锁
|
|
116
116
|
- **独立配置、独立日志、独立存档**,卸载自动清理定时器和中间件
|
|
117
117
|
- **依赖自动隔离**:第三方包装进插件目录的 `.venv`,不污染系统
|
|
118
|
-
-
|
|
118
|
+
- **面板可改数据**:插件在「扩展 → 插件管理」里自动获得配置表单和存档编辑,**不需要写前端代码**
|
|
119
119
|
- **有脚手架和测试命令**:`npm run create` 生成骨架,`npm run test:plugin` 直接跑命令看回复,
|
|
120
120
|
不连 QQ 不开面板;外部插件还有单文件 SDK(Python / Node)
|
|
121
121
|
- **存盘即生效**:改完插件代码 2 秒内自动热重载,不用重启也不用点重载;新增/删除插件目录同样自动识别
|
|
@@ -152,16 +152,15 @@
|
|
|
152
152
|
|
|
153
153
|
**可视化面板**
|
|
154
154
|
|
|
155
|
-
-
|
|
156
|
-
|
|
157
|
-
**插件自己注册的页面也挂在「插件」下**——插件装多了不会把顶栏挤爆
|
|
155
|
+
- **分组导航**:侧栏分五组——运行(概览 / 统计 / 日志)、诊断(事件 / 可达性 / 投递 / 账本 / 查询)、管理(配置 / 开放平台 / 身份 / 群组 / 权限 / 素材 / 通知 / 外接)、扩展(插件管理)、系统(外观 / 关于),
|
|
156
|
+
**插件自己注册的页面也挂在「扩展」组下**——插件装多了不会把侧栏挤爆
|
|
158
157
|
- **体检**:打开面板先跑一遍,凭证 / 连接 / intents / 目录权限 / 面板暴露面
|
|
159
158
|
一次查完,出问题直接摆在最上面并告诉你该去哪儿改
|
|
160
159
|
- 实时日志流(SSE),按级别和来源着色
|
|
161
160
|
- 一个面板管多个机器人,顶栏随时切换
|
|
162
161
|
- 插件卡片直接显示它自己上报的运行数据(已回复条数、在线人数…)
|
|
163
|
-
-
|
|
164
|
-
- 日间 /
|
|
162
|
+
- **「通知」页**:配任意邮箱服务商的 SMTP、一键发测试邮件到主人邮箱,普通成员自助绑定的说明也在这里
|
|
163
|
+
- 日间 / 夜间主题与配色,启动自动开浏览器,端口被占用自动顺延
|
|
165
164
|
|
|
166
165
|
---
|
|
167
166
|
|
|
@@ -310,30 +309,41 @@ ROOST_NO_SYSTEM_PROXY=1 node src/main.js
|
|
|
310
309
|
|
|
311
310
|
顶栏可以切换机器人,一个面板管全部:
|
|
312
311
|
|
|
313
|
-

|
|
313
|
+
|
|
314
|
+

|
|
315
|
+
|
|
316
|
+
侧栏分五组,各组页签如下:
|
|
317
|
+
|
|
318
|
+
| 分组 | 页签 | 能做什么 |
|
|
319
|
+
|------|------|----------|
|
|
320
|
+
| **运行** | 概览 | 连接状态、运行时长、凭证有效期、内存、收发统计、活跃会话 Top20、重连 / 刷新凭证、调试发消息 |
|
|
321
|
+
| | 统计 | 收发量、命令命中、错误等计数 |
|
|
322
|
+
| | 日志 | SSE 实时流,按级别和来源过滤,支持关键词搜索 |
|
|
323
|
+
| **诊断** | 事件 | **47 个官方事件的订阅状态与接收计数**,一眼看出哪个事件没收到 |
|
|
324
|
+
| | 可达性 | 机器人能否主动发言(靠入群 / 退群 / 加好友等事件维护) |
|
|
325
|
+
| | 投递 | 出站投递状态与失败原因 |
|
|
326
|
+
| | 账本 | 每个会话的历史消息:发言人缩略头像 / 群名片 / QQ 昵称、会话显示群名,支持导出与按条件删除 |
|
|
327
|
+
| | 查询 | 跨会话检索:关键词(多词默认全中,无结果自动放宽)+ 发言人(昵称 / openid / QQ号)+ 时间;另有**人物反查**:给昵称 / QQ号 / openid 任一个,查出这个人的其它字段,openid 一键复制 |
|
|
328
|
+
| **管理** | 配置 | 凭证、intents 勾选、命令前缀、白名单、限流、去重窗口、消息日志、日志级别、面板端口 / 密钥;底部**实例锁**:哪个 AppID 被哪个进程占着、残留记录一键清 |
|
|
329
|
+
| | 开放平台 | 扫码登录、开发者后台里的机器人资料 / 回调 / 白名单(与本机「配置」页不是一回事) |
|
|
330
|
+
| | 身份 | QQ号 / 群号 ↔ openid 互查 |
|
|
331
|
+
| | 群组 | 全局主人增删、群档案(邀请人 / 群主 / 状态)、手动登记群 |
|
|
332
|
+
| | 权限 | 等级与作用域说明、按作用域增删管理员、已授权列表 |
|
|
333
|
+
| | 素材 | 机器人级 Markdown 图床:可视化上传 / 批量管理,上传后得到 `qqbot-media://` 引用 |
|
|
334
|
+
| | 通知 | SMTP 服务器 / 端口 / 用户名 / 授权码 / 发件人 / 主人邮箱、测试邮件通知、普通成员自助绑定说明 |
|
|
335
|
+
| | 外接 | 自有宿主协议 + OneBot v11,共用按需依赖 |
|
|
336
|
+
| **扩展** | 插件管理 | 列表、启用 / 禁用 / 重载、加载错误提示;插件注册的页面也挂在这一组 |
|
|
337
|
+
| **系统** | 外观 | 明暗 / 底色 / 配色、方案导入导出(纯本机浏览器偏好,不碰服务端) |
|
|
338
|
+
| | 关于 | 版本、许可、项目信息 |
|
|
329
339
|
|
|
330
340
|
事件页把官方 47 个事件全列出来,灰掉的是当前 intents 没订阅的:
|
|
331
341
|
|
|
332
|
-

|
|
333
343
|
|
|
334
344
|
日志按来源着色,可按级别和来源过滤,也能搜关键词:
|
|
335
345
|
|
|
336
|
-

|
|
337
347
|
|
|
338
348
|
**没有凭证也想看界面**:
|
|
339
349
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qq-bot-roost",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "栖 Roost:零依赖的 QQ 官方机器人框架,填 AppID/AppSecret 即连,插件可写任意语言",
|
|
6
6
|
"license": "AGPL-3.0-only",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"mock": "node src/main.js --mock",
|
|
31
31
|
"check": "node src/main.js --check",
|
|
32
32
|
"release:prepare": "node scripts/release-prepare.mjs",
|
|
33
|
-
"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/sync-beta-tag.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/transport-regressions.mjs && node --test test/check-mode.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-keep.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 && node --test test/plugin-update.mjs && node --test test/market-source.mjs",
|
|
33
|
+
"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/sync-beta-tag.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/transport-regressions.mjs && node --test test/check-mode.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-keep.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 && node --test test/plugin-update.mjs && node --test test/deps-mirror.mjs && node --test test/faces.mjs && node --test test/md-image.mjs && node --test test/media-time.mjs && node --test test/market-source.mjs",
|
|
34
34
|
"test:e2e": "node test/e2e.mjs",
|
|
35
|
-
"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/sync-beta-tag.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/transport-regressions.mjs && node --test test/check-mode.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-keep.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 && node --test test/plugin-update.mjs && node --test test/market-source.mjs",
|
|
35
|
+
"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/sync-beta-tag.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/transport-regressions.mjs && node --test test/check-mode.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-keep.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 && node --test test/plugin-update.mjs && node --test test/deps-mirror.mjs && node --test test/faces.mjs && node --test test/md-image.mjs && node --test test/market-source.mjs",
|
|
36
36
|
"check:secrets": "node scripts/check-secrets.mjs",
|
|
37
37
|
"create": "node scripts/create-plugin.mjs",
|
|
38
38
|
"test:plugin": "node scripts/test-plugin.mjs",
|
package/src/api.js
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
1
|
import crypto from 'node:crypto';
|
|
2
|
+
import fsSync from 'node:fs';
|
|
2
3
|
import fs from 'node:fs/promises';
|
|
3
4
|
import path from 'node:path';
|
|
4
5
|
import { createLogger } from './logger.js';
|
|
5
6
|
import { DIRS } from './paths.js';
|
|
7
|
+
import { encodeFaces, stripFaceMarks } from './faces/index.js';
|
|
8
|
+
import { dimsOf, normalizeMarkdownImages } from './md-image.js';
|
|
9
|
+
import { mdToPlain } from './markdown.js';
|
|
6
10
|
/** 路径里的 id 一律编码:openid / 群号是外部数据,含 / 会拼出别的路径 */
|
|
7
11
|
const enc = (v) => encodeURIComponent(String(v ?? ''));
|
|
8
12
|
|
|
13
|
+
/**
|
|
14
|
+
* 官方的 `created_at` / `expires_at` 是 Unix **秒**,而框架内其它 `expiresAt`
|
|
15
|
+
* (如 `token.js`)是毫秒、下游面板一律按毫秒渲染。同一个名字两种量级,
|
|
16
|
+
* 曾经把 `1.83e9` 秒当毫秒显示成 **1970-01-22**。
|
|
17
|
+
*
|
|
18
|
+
* 所以在这里一次性归一:小于 1e12 的按秒乘 1000(1e12 ms 是 2001 年,
|
|
19
|
+
* 现实里不会有 1970~2001 之间的素材时间戳),已经是毫秒的原样返回——
|
|
20
|
+
* 万一官方哪天改成毫秒,也不会反着错一次。
|
|
21
|
+
*
|
|
22
|
+
* 导出是为了能被单测钉住:这个函数错了,面板上只会显示一个错的时间,
|
|
23
|
+
* 不会报错,属于「静默错」,必须有测试。
|
|
24
|
+
*/
|
|
25
|
+
export const toMillis = (v) => {
|
|
26
|
+
const n = Number(v) || 0;
|
|
27
|
+
if (!n) return 0;
|
|
28
|
+
return n < 1e12 ? n * 1000 : n;
|
|
29
|
+
};
|
|
30
|
+
|
|
9
31
|
/** 本进程创建、尚未删除的 Markdown 素材 id(崩溃后靠它认领孤儿) */
|
|
10
32
|
const OWN_MEDIA_FILE = path.join(DIRS.data, 'media-images-own.json');
|
|
11
33
|
/**
|
|
@@ -14,6 +36,14 @@ const OWN_MEDIA_FILE = path.join(DIRS.data, 'media-images-own.json');
|
|
|
14
36
|
* 老用户升级后也不需要做格式迁移。
|
|
15
37
|
*/
|
|
16
38
|
const KEPT_MEDIA_FILE = path.join(DIRS.data, 'media-images-kept.json');
|
|
39
|
+
/**
|
|
40
|
+
* Markdown 素材的宽高(`qqbot-media://` 引用 → `{ w, h }`)。
|
|
41
|
+
*
|
|
42
|
+
* 为什么要落盘:插件会缓存引用反复发(「线上那份留着复用」是默认行为),
|
|
43
|
+
* 框架重启后内存表是空的,而官方接口事后**不返回宽高**。不落盘的话,
|
|
44
|
+
* 重启后所有复用图都会退化成「没有尺寸」→ 手机上不显示,且没有任何报错。
|
|
45
|
+
*/
|
|
46
|
+
const MD_DIMS_FILE = path.join(DIRS.data, 'media-images-dims.json');
|
|
17
47
|
|
|
18
48
|
/** 官方给的单机器人素材配额,查询接口的 total 与它同一口径 */
|
|
19
49
|
export const MEDIA_IMAGE_QUOTA = 100;
|
|
@@ -219,6 +249,116 @@ export function buildSendPayload(opts = {}, nextSeq = () => 1) {
|
|
|
219
249
|
return payload;
|
|
220
250
|
}
|
|
221
251
|
|
|
252
|
+
/**
|
|
253
|
+
* 把正文里的 `【表情: 微笑】` 编成 QQ 协议表情(`0x14 + faceId`)。
|
|
254
|
+
*
|
|
255
|
+
* 抽成纯函数(而不是写在 `#payload()` 里)有两个原因:
|
|
256
|
+
*
|
|
257
|
+
* 1. `#payload` 是私有方法,测试摸不到;这里是发送链路上唯一会被单测钉住的环节;
|
|
258
|
+
* 2. **content 和 markdown.content 是两个字段**,只改一个就会出现
|
|
259
|
+
* 「纯文本回复有表情、markdown 回复没有」这种一半好一半坏的情况。
|
|
260
|
+
*
|
|
261
|
+
* ## markdown 通道必须降级(2026-11-22 实测)
|
|
262
|
+
*
|
|
263
|
+
* 同样的正文、同样的编码,分两条发到手机:
|
|
264
|
+
*
|
|
265
|
+
* · `msg_type=0`(纯文本)→ 表情正常显示 ✅
|
|
266
|
+
* · `msg_type=2`(markdown)→ 表情消失,只剩文字 ❌
|
|
267
|
+
*
|
|
268
|
+
* 结论:markdown 渲染器把 `0x14` 当非法控制字符吃掉了。
|
|
269
|
+
* 所以带表情的 markdown 消息在 `auto` 模式下**转成纯文本发**:
|
|
270
|
+
* 正文先用 `mdToPlain()` 去掉 `**` `#` 这类记号,表情字符原样保留。
|
|
271
|
+
*
|
|
272
|
+
* 三种情况不降级(降级代价比丢表情更大):
|
|
273
|
+
* · 正文里有图(`` / `qqbot-media://`)—— 纯文本会把图弄没;
|
|
274
|
+
* · 这条消息本身是富媒体 / 卡片 / ark —— 一条消息只能有一种富文本;
|
|
275
|
+
* · `mode: 'keep'` —— 用户明确要排版优先。
|
|
276
|
+
*
|
|
277
|
+
* @param {object} payload `buildSendPayload()` 的产物(原地修改)
|
|
278
|
+
* @param {object} [opts]
|
|
279
|
+
* @param {boolean} [opts.enabled=true] 总开关
|
|
280
|
+
* @param {'drop'|'keep'} [opts.unknown='drop'] 名字不认识时删掉 / 保留原样
|
|
281
|
+
* @param {'auto'|'keep'|'force'} [opts.mode='auto'] 通道策略,见上
|
|
282
|
+
* @param {(name:string)=>void} [opts.onMiss] 诊断回调
|
|
283
|
+
* @returns {object} 同一个 payload
|
|
284
|
+
*/
|
|
285
|
+
export function applyFaceEncoding(payload, opts = {}) {
|
|
286
|
+
if (!payload || opts.enabled === false) return payload;
|
|
287
|
+
const unknown = opts.unknown === 'keep' ? 'keep' : 'drop';
|
|
288
|
+
const mode = ['auto', 'keep', 'force'].includes(opts.mode) ? opts.mode : 'auto';
|
|
289
|
+
const onMiss = opts.onMiss;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* 富媒体 / 卡片 / ark:一条消息只能有一种富文本,**表情一律让位**。
|
|
293
|
+
*
|
|
294
|
+
* 这个判断必须放在编码之前:`msg_type=7` 的 `content` 官方口径是
|
|
295
|
+
* 「只在 0 时才是全文」,控制字符塞进去同样没人渲染,还会在正文里
|
|
296
|
+
* 留下看不见的字符。所以这里只删标记、不编码。
|
|
297
|
+
* 卡片那两个字段(title / description)是卡片自己的内容,不碰。
|
|
298
|
+
*/
|
|
299
|
+
if (payload.media || payload.card || payload.ark) {
|
|
300
|
+
if (typeof payload.content === 'string' && payload.content.includes('【表情')) {
|
|
301
|
+
payload.content = stripFaceMarks(payload.content);
|
|
302
|
+
}
|
|
303
|
+
if (payload.markdown && typeof payload.markdown.content === 'string') {
|
|
304
|
+
payload.markdown.content = stripFaceMarks(payload.markdown.content);
|
|
305
|
+
}
|
|
306
|
+
return payload;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/* 1) 纯文本:编完就发。`msg_type=0` 是客户端原生认 `0x14` 的那条通道 */
|
|
310
|
+
if (typeof payload.content === 'string' && payload.content.includes('【表情')) {
|
|
311
|
+
payload.content = encodeFaces(payload.content, { unknown, onMiss });
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/* 2) markdown:编完之后要决定走哪条通道 */
|
|
315
|
+
const mdObj = payload.markdown;
|
|
316
|
+
if (!mdObj || typeof mdObj.content !== 'string' || !mdObj.content.includes('【表情')) {
|
|
317
|
+
return payload;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const raw = mdObj.content;
|
|
321
|
+
const encoded = encodeFaces(raw, { unknown, onMiss });
|
|
322
|
+
|
|
323
|
+
/** `force`:明知 markdown 通道会吞,也照编(实测 / 高级用法) */
|
|
324
|
+
if (mode === 'force') {
|
|
325
|
+
mdObj.content = encoded;
|
|
326
|
+
return payload;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const hasFace = encoded.includes('\u0014');
|
|
330
|
+
const hasImage = /!\[|qqbot-media:\/\//.test(raw);
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* 不能降级:把表情标记删掉,保住图 / 排版。
|
|
334
|
+
*
|
|
335
|
+
* 这里必须用 `stripFaceMarks()` 而不是 `encodeFaces(…, {unknown:'drop'})`:
|
|
336
|
+
* 后者对**认识**的名字会编成控制字符,而 markdown 通道恰恰吞控制字符 ——
|
|
337
|
+
* 那就成了"既没表情、又留了两个看不见的字符"。
|
|
338
|
+
*/
|
|
339
|
+
if (mode === 'keep' || hasImage) {
|
|
340
|
+
mdObj.content = stripFaceMarks(raw);
|
|
341
|
+
return payload;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** 名字全都不认识(编完没有表情字符)→ 只是删了几个标记,markdown 照旧 */
|
|
345
|
+
if (!hasFace) {
|
|
346
|
+
mdObj.content = encoded;
|
|
347
|
+
return payload;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* `auto` 降级:markdown → 纯文本。
|
|
352
|
+
*
|
|
353
|
+
* `mdToPlain()` 只剥记号(`**` `#` `>` 链接),不碰 `0x14`——
|
|
354
|
+
* 它最后一步只 trim 空白,控制字符不在 `\s` 里。
|
|
355
|
+
*/
|
|
356
|
+
payload.content = mdToPlain(encoded);
|
|
357
|
+
delete payload.markdown;
|
|
358
|
+
payload.msg_type = 0;
|
|
359
|
+
return payload;
|
|
360
|
+
}
|
|
361
|
+
|
|
222
362
|
/**
|
|
223
363
|
* 素材 file_info 的缓存时长。
|
|
224
364
|
* 官方说有效期 3 天,这里按 2 天算——过期前就换新的,
|
|
@@ -297,6 +437,27 @@ export class QQApi {
|
|
|
297
437
|
this.groupInvited = false;
|
|
298
438
|
// 「发图带空格」兼容开关,由 BotRuntime 按 cfg.media.placeholderSpace 同步;默认关
|
|
299
439
|
this.mediaPlaceholderSpace = false;
|
|
440
|
+
/**
|
|
441
|
+
* 小黄脸出站开关,由 BotRuntime 按 cfg.faces 同步;默认开。
|
|
442
|
+
*
|
|
443
|
+
* 默认开是有意的:`【表情: 微笑】` 这种写法只有我们自己的模型会写,
|
|
444
|
+
* 真人用户不会在正文里打这一串。所以"默认开"不会误伤正常消息,
|
|
445
|
+
* 而关掉它只会让模型写的表情变成五个中文字。
|
|
446
|
+
*/
|
|
447
|
+
this.faces = { outbound: true, unknown: 'drop' };
|
|
448
|
+
/**
|
|
449
|
+
* Markdown 素材的宽高:`qqbot-media://` 引用 → `{ w, h }`。
|
|
450
|
+
*
|
|
451
|
+
* 只在上传那一刻有字节可读,官方接口事后**不返回宽高**(只有
|
|
452
|
+
* image_id / url / file_name / preview_url)。而 QQ 的 markdown 图片
|
|
453
|
+
* 语法 `` 缺了尺寸就**不渲染**——手机上只剩文字,
|
|
454
|
+
* 平台还照收 200、不报错。所以宽高必须当场记下来,发送时补进正文。
|
|
455
|
+
*
|
|
456
|
+
* 上限 500:一个机器人最多 100 张素材,留点余量给反复上传。
|
|
457
|
+
*/
|
|
458
|
+
this._mdImageDims = new Map();
|
|
459
|
+
/** 落盘的那份只读一次;`#payload()` 是同步的,不能每次都 await */
|
|
460
|
+
this._mdDimsLoaded = false;
|
|
300
461
|
// 群角色查询缓存:groupOpenid -> { at, value }。bot_state 只有 30 QPM
|
|
301
462
|
this.roleCache = new Map();
|
|
302
463
|
/**
|
|
@@ -910,6 +1071,7 @@ export class QQApi {
|
|
|
910
1071
|
* 官方的机器人级素材清单,按上传时间倒序。用它看剩余配额(上限 100 张)、
|
|
911
1072
|
* 找可复用的 qqbot-media:// 引用、定位要删除的 image_id。
|
|
912
1073
|
* 返回字段统一转 camelCase;`previewUrl` 是短时效预签名链接,别持久化。
|
|
1074
|
+
* `createdAt` / `expiresAt` 统一成**毫秒**(官方给的是秒,见 `toMillis`)。
|
|
913
1075
|
*/
|
|
914
1076
|
async listMediaImages({ cursor = '' } = {}) {
|
|
915
1077
|
const q = new URLSearchParams();
|
|
@@ -922,8 +1084,8 @@ export class QQApi {
|
|
|
922
1084
|
url: String(it?.url ?? ''),
|
|
923
1085
|
fileName: String(it?.file_name ?? ''),
|
|
924
1086
|
previewUrl: String(it?.preview_url ?? ''),
|
|
925
|
-
createdAt:
|
|
926
|
-
expiresAt:
|
|
1087
|
+
createdAt: toMillis(it?.created_at),
|
|
1088
|
+
expiresAt: toMillis(it?.expires_at),
|
|
927
1089
|
}));
|
|
928
1090
|
return {
|
|
929
1091
|
images,
|
|
@@ -1042,13 +1204,30 @@ export class QQApi {
|
|
|
1042
1204
|
e.reason = 'internal';
|
|
1043
1205
|
throw e;
|
|
1044
1206
|
}
|
|
1207
|
+
const url = String(done.url);
|
|
1208
|
+
/**
|
|
1209
|
+
* 宽高就在手上(`buf` 是刚上传的那份字节),现在不记,以后就再也拿不到了:
|
|
1210
|
+
* 官方素材接口不回宽高。`#payload()` 发消息时按这个表补 `#宽px #高px`。
|
|
1211
|
+
*
|
|
1212
|
+
* 读不出来(老图/怪格式)存 `{0,0}`,调用方按"没有尺寸"处理——
|
|
1213
|
+
* 宁可这一次不显示,也不要瞎猜比例把图压变形。
|
|
1214
|
+
*/
|
|
1215
|
+
const dims = dimsOf(buf);
|
|
1216
|
+
this._mdImageDims.set(url, dims);
|
|
1217
|
+
if (this._mdImageDims.size > 500) {
|
|
1218
|
+
this._mdImageDims.delete(this._mdImageDims.keys().next().value);
|
|
1219
|
+
}
|
|
1220
|
+
this.#saveMdImageDims();
|
|
1045
1221
|
return {
|
|
1046
1222
|
imageId: String(done.image_id),
|
|
1047
|
-
url
|
|
1223
|
+
url,
|
|
1048
1224
|
ttl: done.ttl,
|
|
1049
1225
|
previewUrl: String(done.preview_url ?? ''),
|
|
1050
1226
|
fileName: name,
|
|
1051
1227
|
retained: Boolean(retain),
|
|
1228
|
+
/** 插件拼 `` 直接用,省得自己再解文件头 */
|
|
1229
|
+
width: dims.w,
|
|
1230
|
+
height: dims.h,
|
|
1052
1231
|
};
|
|
1053
1232
|
}
|
|
1054
1233
|
|
|
@@ -1175,6 +1354,25 @@ export class QQApi {
|
|
|
1175
1354
|
return this.mediaPlaceholderSpace;
|
|
1176
1355
|
}
|
|
1177
1356
|
|
|
1357
|
+
/**
|
|
1358
|
+
* 小黄脸出站开关。由 BotRuntime 按 `cfg.faces` 初始化,面板改动时同步进来。
|
|
1359
|
+
*
|
|
1360
|
+
* 只认 `false` 是关(和 `setMediaPlaceholderSpace` 相反,这里的默认值是「开」):
|
|
1361
|
+
* 老配置文件里没有 faces 这一段,读出来是 undefined,语义应该是「保持默认开」,
|
|
1362
|
+
* 而不是「显式关闭」——后者会让升级后的老用户突然丢掉表情。
|
|
1363
|
+
*
|
|
1364
|
+
* @param {{outbound?:boolean, unknown?:'drop'|'keep'}} [v]
|
|
1365
|
+
*/
|
|
1366
|
+
setFaces(v) {
|
|
1367
|
+
const src = (v && typeof v === 'object') ? v : {};
|
|
1368
|
+
this.faces = {
|
|
1369
|
+
outbound: src.outbound !== false,
|
|
1370
|
+
unknown: src.unknown === 'keep' ? 'keep' : 'drop',
|
|
1371
|
+
mode: ['auto', 'keep', 'force'].includes(src.mode) ? src.mode : 'auto',
|
|
1372
|
+
};
|
|
1373
|
+
return this.faces;
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1178
1376
|
/** 内邀接口的统一闸门。没开门就抛错,别把请求发出去刷一屏 11253 */
|
|
1179
1377
|
#needInvite() {
|
|
1180
1378
|
if (!this.groupInvited) {
|
|
@@ -2145,6 +2343,39 @@ export class QQApi {
|
|
|
2145
2343
|
|
|
2146
2344
|
#payload(opts) {
|
|
2147
2345
|
const payload = buildSendPayload(opts, (id) => this.#nextSeq(id));
|
|
2346
|
+
/**
|
|
2347
|
+
* markdown 图片补尺寸:`` → ``。
|
|
2348
|
+
*
|
|
2349
|
+
* **缺了尺寸平台不渲染图**:照收 200,手机上只剩文字,日志里什么都没有
|
|
2350
|
+
* (2026-11-22 实测,见 `md-image.js` 顶部的对照表)。宽高只有上传那一刻
|
|
2351
|
+
* 能读到,事后官方接口不返回,所以在上传处记表、在这里补。
|
|
2352
|
+
*
|
|
2353
|
+
* 放在 `#payload` 的理由和下面的表情编码一样:`msg.reply` / `replyAuto` /
|
|
2354
|
+
* 插件直调 `ctx.api` / 宿主协议 `msg.send` 最后都过这里,一处覆盖全部出口。
|
|
2355
|
+
* 插件自己写对尺寸也不受影响——已经带两个尺寸位的原样放过。
|
|
2356
|
+
*/
|
|
2357
|
+
if (payload.markdown && typeof payload.markdown.content === 'string') {
|
|
2358
|
+
this.#loadMdImageDims();
|
|
2359
|
+
payload.markdown.content = normalizeMarkdownImages(
|
|
2360
|
+
payload.markdown.content,
|
|
2361
|
+
(u) => this._mdImageDims.get(u),
|
|
2362
|
+
);
|
|
2363
|
+
}
|
|
2364
|
+
/**
|
|
2365
|
+
* 小黄脸:`【表情: 微笑】` → `0x14 + faceId`。
|
|
2366
|
+
*
|
|
2367
|
+
* 放在这一层是**唯一能一处覆盖全部出口**的位置:`msg.reply` /
|
|
2368
|
+
* `replyAuto` / `replyMarkdown` / 插件拿到的 `ctx.api.sendGroup` /
|
|
2369
|
+
* 宿主协议的 `msg.send`,最后都过 `#payload`。
|
|
2370
|
+
* 放到 outbound 或 message 层都会漏掉「插件直调 ctx.api」那条路。
|
|
2371
|
+
*
|
|
2372
|
+
* 顺序在下面的「发图带空格」之前:那个只碰 msg_type=7 的空 content,
|
|
2373
|
+
* 和表情没有交集,但先编码能让"正文最终长什么样"更早定型。
|
|
2374
|
+
*/
|
|
2375
|
+
applyFaceEncoding(payload, {
|
|
2376
|
+
...this.faces,
|
|
2377
|
+
onMiss: (name) => log.warn(`表情名不认识,已从正文删掉:${name}`),
|
|
2378
|
+
});
|
|
2148
2379
|
/**
|
|
2149
2380
|
* 「发图带空格」兼容开关(默认关,面板「配置 → 发图兼容」)。
|
|
2150
2381
|
*
|
|
@@ -2164,6 +2395,36 @@ export class QQApi {
|
|
|
2164
2395
|
return payload;
|
|
2165
2396
|
}
|
|
2166
2397
|
|
|
2398
|
+
/**
|
|
2399
|
+
* 把宽高表落盘。失败只记一条警告:补不上尺寸顶多是图不显示,
|
|
2400
|
+
* 不能让一次磁盘错误把整条消息拖死。
|
|
2401
|
+
*/
|
|
2402
|
+
#saveMdImageDims() {
|
|
2403
|
+
const plain = Object.fromEntries(this._mdImageDims);
|
|
2404
|
+
fs.mkdir(path.dirname(MD_DIMS_FILE), { recursive: true })
|
|
2405
|
+
.then(() => fs.writeFile(MD_DIMS_FILE, JSON.stringify(plain), 'utf8'))
|
|
2406
|
+
.catch((e) => log.warn(`Markdown 素材宽高表写失败(不影响本次发送): ${e.message}`));
|
|
2407
|
+
}
|
|
2408
|
+
|
|
2409
|
+
/**
|
|
2410
|
+
* 首次发消息时把宽高表读回来(同步,一次就够)。
|
|
2411
|
+
*
|
|
2412
|
+
* 用同步读是为了能放进 `#payload()`:那是同步函数,整条发送链路都在等它,
|
|
2413
|
+
* 没必要为了几十 KB 的 JSON 把它改成 async。文件不存在 / 损坏都当空表。
|
|
2414
|
+
*/
|
|
2415
|
+
#loadMdImageDims() {
|
|
2416
|
+
if (this._mdDimsLoaded) return;
|
|
2417
|
+
this._mdDimsLoaded = true;
|
|
2418
|
+
try {
|
|
2419
|
+
const obj = JSON.parse(fsSync.readFileSync(MD_DIMS_FILE, 'utf8'));
|
|
2420
|
+
for (const [u, d] of Object.entries(obj ?? {})) {
|
|
2421
|
+
if (!this._mdImageDims.has(u)) this._mdImageDims.set(u, d);
|
|
2422
|
+
}
|
|
2423
|
+
} catch {
|
|
2424
|
+
/* 没有文件 / 内容坏了:当空表,走"没有尺寸"的分支 */
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2167
2428
|
/**
|
|
2168
2429
|
* 被动回复序号。硬顶见 `PASSIVE_MAX_SEQ`(群聊 5 / 单聊 4,这里取 5 兜底),
|
|
2169
2430
|
* 超了服务端返回 40034128。正常流程由 outbound.js 按会话类型先拦。
|
package/src/bot.js
CHANGED
|
@@ -290,6 +290,8 @@ export class BotRuntime {
|
|
|
290
290
|
// 发图带空格:只影响真实 QQApi 的最终报文;mock api 不拼官方报文、
|
|
291
291
|
// 没实现这个 setter,?. 会直接跳过(演示模式下开关不适用)
|
|
292
292
|
this.api.setMediaPlaceholderSpace?.(cfg.media?.placeholderSpace);
|
|
293
|
+
// 小黄脸:`【表情: 微笑】` → 协议表情字符。默认开,只有显式 false 才关
|
|
294
|
+
this.api.setFaces?.(cfg.faces);
|
|
293
295
|
|
|
294
296
|
this.session = new SessionManager({
|
|
295
297
|
stickyMs: Math.max(0, (cfg.session?.stickySec ?? 180) * 1000),
|
package/src/config.js
CHANGED
|
@@ -463,6 +463,32 @@ const BOT_DEFAULTS = {
|
|
|
463
463
|
media: {
|
|
464
464
|
placeholderSpace: false,
|
|
465
465
|
},
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* QQ 小黄脸(名字标记 ↔ 协议表情)。
|
|
469
|
+
*
|
|
470
|
+
* `outbound` 默认开:模型写 `【表情: 微笑】`,发出前换成 `0x14 + faceId`。
|
|
471
|
+
* 真人不会在正文里打这一串,所以默认开不会误伤正常消息。
|
|
472
|
+
*
|
|
473
|
+
* `inbound` 默认开:群友发表情时 QQ 推的是
|
|
474
|
+
* `<faceType=1,faceId="187",ext="…">`,翻成 `【表情: 幽灵】` 模型才看得懂。
|
|
475
|
+
*
|
|
476
|
+
* `unknown` 决定名字不认识时怎么办:
|
|
477
|
+
* · `drop`(默认)—— 删掉标记。`【表情: 不存在的脸】` 原样发进群,
|
|
478
|
+
* 用户看到括号加中文,比不发更怪;
|
|
479
|
+
* · `keep` —— 原样保留,方便调试码表。
|
|
480
|
+
*
|
|
481
|
+
* `mode` 决定 markdown 消息怎么办(2026-11-22 实测:markdown 通道会吞表情):
|
|
482
|
+
* · `auto`(默认)—— 带表情的 markdown 转成纯文本发,表情保得住;
|
|
483
|
+
* · `keep` —— 保留 markdown 排版,表情标记删掉(排版优先);
|
|
484
|
+
* · `force` —— 明知会吞也照编,只用于实测。
|
|
485
|
+
*/
|
|
486
|
+
faces: {
|
|
487
|
+
outbound: true,
|
|
488
|
+
inbound: true,
|
|
489
|
+
unknown: 'drop',
|
|
490
|
+
mode: 'auto',
|
|
491
|
+
},
|
|
466
492
|
};
|
|
467
493
|
|
|
468
494
|
/** 深拷贝:默认值里的对象/数组必须复制,见 `deepMerge` 的注释 */
|
|
@@ -758,6 +784,17 @@ export async function saveBotConfig(id, cfg) {
|
|
|
758
784
|
host: normalizeHostConfig(cfg.host),
|
|
759
785
|
// 脏值(字符串 / 数组 / 旧配置里混进来的东西)一律规范成默认关,别污染配置文件
|
|
760
786
|
media: { placeholderSpace: cfg.media?.placeholderSpace === true },
|
|
787
|
+
/**
|
|
788
|
+
* 小黄脸。和 media 相反,这两项**默认开**:
|
|
789
|
+
* 老配置文件里没有这一段时 `undefined !== false`,读出来仍是开。
|
|
790
|
+
* `unknown` 只认 'keep',其余(含拼错的)一律当 'drop'。
|
|
791
|
+
*/
|
|
792
|
+
faces: {
|
|
793
|
+
outbound: cfg.faces?.outbound !== false,
|
|
794
|
+
inbound: cfg.faces?.inbound !== false,
|
|
795
|
+
unknown: cfg.faces?.unknown === 'keep' ? 'keep' : 'drop',
|
|
796
|
+
mode: ['auto', 'keep', 'force'].includes(cfg.faces?.mode) ? cfg.faces.mode : 'auto',
|
|
797
|
+
},
|
|
761
798
|
});
|
|
762
799
|
}
|
|
763
800
|
|