qq-bot-roost 1.0.13 → 1.0.15

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.
Files changed (55) hide show
  1. package/README.md +10 -3
  2. package/config/bot.example.json +30 -0
  3. package/docs/AI/345/256/277/344/270/273/346/216/245/345/205/245.md +145 -0
  4. package/docs/DSH/346/216/245/345/205/245.md +267 -0
  5. package/docs/OneBot/345/205/274/345/256/271/345/261/202/350/256/276/350/256/241.md +1032 -0
  6. package/docs/images/bot-menu.png +0 -0
  7. package/docs/images/bot-switcher.png +0 -0
  8. package/docs/images/events.png +0 -0
  9. package/docs/images/group-manage.png +0 -0
  10. package/docs/images/logs.png +0 -0
  11. package/docs/images/overview.png +0 -0
  12. package/docs/images/plugin-dash-tab.png +0 -0
  13. package/docs/images/plugins-dash.png +0 -0
  14. package/docs/images/plugins.png +0 -0
  15. package/docs//345/217/221/347/211/210/346/270/205/345/215/225.md +167 -0
  16. package/docs//345/233/236/346/273/232/351/242/204/346/241/210.md +78 -0
  17. package/docs//345/256/230/346/226/271/346/216/245/345/217/243/351/231/220/345/210/266.md +375 -0
  18. package/docs//345/256/277/344/270/273/345/215/217/350/256/256v1.md +402 -0
  19. package/docs//345/270/270/350/247/201/351/227/256/351/242/230.md +559 -0
  20. package/docs//346/217/222/344/273/266/345/215/217/350/256/256.md +521 -0
  21. package/docs//346/217/222/344/273/266/345/274/200/345/217/221/350/247/204/350/214/203.md +1857 -0
  22. package/docs//346/235/203/351/231/220/346/250/241/345/236/213.md +237 -0
  23. package/docs//346/236/266/346/236/204/350/257/264/346/230/216.md +311 -0
  24. package/docs//347/234/237/346/234/272/350/201/224/350/260/203.md +253 -0
  25. package/docs//351/205/215/347/275/256/345/217/202/350/200/203.md +427 -0
  26. package/package.json +4 -3
  27. package/src/api.js +242 -77
  28. package/src/bot.js +93 -2
  29. package/src/config.js +150 -6
  30. package/src/endpoints.js +5 -3
  31. package/src/ext-deps.js +703 -0
  32. package/src/host/dispatch.js +125 -0
  33. package/src/host/index.js +395 -0
  34. package/src/host/media.js +462 -0
  35. package/src/host/methods.js +756 -0
  36. package/src/host/ops.js +462 -0
  37. package/src/host/serialize.js +79 -0
  38. package/src/host/server.js +397 -0
  39. package/src/instance.js +75 -13
  40. package/src/ledger.js +45 -0
  41. package/src/main.js +5 -1
  42. package/src/onebot/actions.js +524 -0
  43. package/src/onebot/capability.js +95 -0
  44. package/src/onebot/ids.js +265 -0
  45. package/src/onebot/index.js +410 -0
  46. package/src/onebot/segments.js +195 -0
  47. package/src/onebot/translate.js +219 -0
  48. package/src/onebot/voice.js +449 -0
  49. package/src/onebot/ws-server.js +270 -0
  50. package/src/plugin/loader.js +9 -0
  51. package/src/plugin/market.js +19 -0
  52. package/src/web/server.js +408 -1
  53. package/web/app.js +613 -0
  54. package/web/index.html +190 -1
  55. package/web/style.css +69 -0
@@ -0,0 +1,195 @@
1
+ /**
2
+ * OneBot 消息段 → 官方报文
3
+ * =========================
4
+ *
5
+ * 官方一条消息只能有一种富文本(`msg_type` 是单值),所以「一段文字 + 两张图」
6
+ * 在 OneBot 里是一条,在官方接口里必然是三条。这里只负责**拆**:
7
+ *
8
+ * { text, reference, images[] }
9
+ *
10
+ * 由 actions.js 按顺序发出去(第一条带引用,后面每条一张图)。
11
+ */
12
+
13
+ /**
14
+ * 官方的 @ 语法是 `<@openid>`。
15
+ *
16
+ * 纯文本出口发不出真 @(官方只在 markdown 里认),所以:
17
+ * · **合法 openid 形态的整段删掉**——`<@8C7419D4B8…>` 留着就是
18
+ * 一串 32 位十六进制,比没有还糟;
19
+ * · 其它 `<@` 变全角挡住,防止「`<@某人>` 这段字」被官方当 mention。
20
+ *
21
+ * `at` 段自己拼的那串**不经过这里**(由 actions 决定渲染成什么)。
22
+ */
23
+ function neutralizeMentions(s) {
24
+ return String(s)
25
+ .replace(/<@!?[0-9A-Za-z]{16,64}>/g, '')
26
+ .replace(/<@/g, '<@');
27
+ }
28
+
29
+ /**
30
+ * @param {Array|string} message OneBot 的 `message`(段数组或字符串)
31
+ * @param {object} ctx
32
+ * @param {import('./ids.js').OneBotIds} ctx.ids
33
+ * @param {number} ctx.selfUin
34
+ * @returns {{text:string, reference:string, images:Array, records:Array, ats:Array, warnings:string[]}}
35
+ * @throws {Error & {retcode?:number}} 遇到官方根本发不了的段
36
+ */
37
+ export function parseOutgoing(message, { ids, selfUin }) {
38
+ const out = { text: '', reference: '', images: [], records: [], ats: [], warnings: [] };
39
+
40
+ if (typeof message === 'string') {
41
+ // 只当纯文本:CQ 码原样剥掉,避免把 `[CQ:at,qq=…]` 当文字发出去
42
+ out.text = neutralizeMentions(message.replace(/\[CQ:[^\]]*\]/g, ''));
43
+ return out;
44
+ }
45
+
46
+ const segs = Array.isArray(message) ? message : [];
47
+ const texts = [];
48
+
49
+ for (const seg of segs) {
50
+ const type = String(seg?.type ?? '');
51
+ const d = seg?.data ?? {};
52
+
53
+ switch (type) {
54
+ case 'text':
55
+ texts.push(neutralizeMentions(d.text ?? ''));
56
+ break;
57
+
58
+ case 'at': {
59
+ const qq = String(d.qq ?? '');
60
+ // @机器人自己:被动回复本身已经指向它,多一个 @ 是噪音
61
+ if (qq && Number(qq) === Number(selfUin)) break;
62
+ const openid = ids.openidOfUser(qq);
63
+ if (!openid) {
64
+ // 不整条失败:降级成文字,让用户至少看到「@了谁」
65
+ const name = String(d.name ?? qq);
66
+ texts.push(`@${name} `);
67
+ out.warnings.push(`@${qq} 没有绑定 openid,已降级成文字`);
68
+ break;
69
+ }
70
+ /**
71
+ * 这里仍然拼 `<@openid>`,但**能不能原样发出去由 actions 决定**:
72
+ * 官方只在 markdown 消息里认 @ 用户(群聊还要求 member_openid),
73
+ * 纯文本里的这串会被当字面量显示。`ats` 记下来供出口判断。
74
+ */
75
+ texts.push(`<@${openid}>`);
76
+ out.ats.push({ openid, qq, name: String(d.name ?? '') });
77
+ break;
78
+ }
79
+
80
+ case 'reply': {
81
+ const rec = ids.getMessage(d.id);
82
+ if (rec?.i) out.reference = String(rec.i);
83
+ else out.warnings.push(`引用失败:找不到 message_id=${d.id}(重启前的消息不在映射表里)`);
84
+ break;
85
+ }
86
+
87
+ case 'image': {
88
+ const file = String(d.file ?? d.url ?? '');
89
+ if (file) out.images.push({ file });
90
+ break;
91
+ }
92
+
93
+ case 'face':
94
+ case 'mface':
95
+ // 官方没有 QQ 表情码,退化成文字
96
+ texts.push('[表情]');
97
+ break;
98
+
99
+ case 'record': {
100
+ const file = String(d.file ?? d.url ?? '');
101
+ if (!file) {
102
+ out.warnings.push('语音段没有 file,已忽略');
103
+ break;
104
+ }
105
+ // 转码与上传在 actions 里做(那里才知道有没有 silk-wasm / ffmpeg)
106
+ out.records.push({ file });
107
+ break;
108
+ }
109
+
110
+ case 'forward':
111
+ case 'node':
112
+ {
113
+ const err = new Error('官方接口只能收合并转发,不能发');
114
+ err.retcode = 1404;
115
+ throw err;
116
+ }
117
+
118
+ case 'share':
119
+ case 'music':
120
+ case 'location': {
121
+ const title = String(d.title ?? d.name ?? '');
122
+ const url = String(d.url ?? d.file ?? '');
123
+ texts.push([title, url].filter(Boolean).join(' ') || `[${type}]`);
124
+ out.warnings.push(`${type} 段已降级成文字`);
125
+ break;
126
+ }
127
+
128
+ default:
129
+ out.warnings.push(`不认识的段类型 ${type || '(空)'},已忽略`);
130
+ }
131
+ }
132
+
133
+ out.text = texts.join('').trim();
134
+ return out;
135
+ }
136
+
137
+ /** 拆条上限:官方一条入站消息最多回 5 次(群聊) */
138
+ export const MAX_PARTS = 5;
139
+
140
+ /**
141
+ * 纯文本出口的正文渲染:把 `<@openid>` 换成 `@昵称`。
142
+ *
143
+ * **为什么必须换**:官方「文本交互」文档——@用户 只在 **markdown 消息**里生效
144
+ * (群聊还要求 id 是 member_openid),纯文本里的这串会被当字面量原样显示。
145
+ * 2026-11-13 实机就是 `<@8C7419D4B8500528C2248E85360A719D> 咋啦`。
146
+ *
147
+ * 昵称来源顺序:at 段自带的 `name` → 调用方给的 nameOf(身份表)→ 都没有就
148
+ * **整段删掉**。宁可少一个 @,也不让 32 位 openid 漏进聊天记录。
149
+ *
150
+ * @param {string} text `parseOutgoing` 拼出来的正文(含 `<@openid>`)
151
+ * @param {Array<{openid:string, name?:string}>} ats
152
+ * @param {(openid:string)=>string} [nameOf]
153
+ * @returns {string}
154
+ */
155
+ export function renderPlainText(text, ats = [], nameOf = () => '') {
156
+ const label = new Map();
157
+ for (const a of ats) {
158
+ const name = String(a?.name ?? '').trim() || String(nameOf(a?.openid) ?? '').trim();
159
+ label.set(String(a?.openid ?? ''), name ? `@${name}` : '');
160
+ }
161
+ let s = String(text ?? '');
162
+ // 精确替换 at 段拼的那串(别用正则猜长度:mock / 短 openid 会漏)
163
+ for (const [openid, l] of label) {
164
+ if (openid) s = s.split(`<@${openid}>`).join(l);
165
+ }
166
+ // 兜底:模型自己在正文里写的 `<@openid>`(不是 at 段)同样不能漏出去
167
+ s = s.replace(/<@!?([0-9A-Za-z]{16,64})>/g, (_m, openid) => label.get(openid) ?? '');
168
+ return s.trim();
169
+ }
170
+
171
+ /**
172
+ * markdown 出口的正文渲染:`<@openid>` → `<qqbot-at-user id="openid" />`。
173
+ *
174
+ * 这是官方**唯一**能让 @ 真正生效的写法(`text-chain` 文档:群聊 @用户
175
+ * 只支持 markdown,id 填 member_openid)。`ids` 里的 openid 就是群消息
176
+ * 事件里的 member_openid,直接能用。
177
+ *
178
+ * 正文其余部分原样保留——消息按 markdown 渲染,模型写的加粗 / 列表 / 链接
179
+ * 会照常显示,这就是「自动识别 markdown」想要的。
180
+ *
181
+ * @param {string} text
182
+ * @param {Array<{openid:string}>} ats
183
+ * @returns {string}
184
+ */
185
+ export function renderMarkdownText(text, ats = []) {
186
+ const tag = (openid) => `<qqbot-at-user id="${openid}" />`;
187
+ let s = String(text ?? '');
188
+ for (const a of ats) {
189
+ const openid = String(a?.openid ?? '');
190
+ if (openid) s = s.split(`<@${openid}>`).join(tag(openid));
191
+ }
192
+ // 模型自己写的 `<@openid>` 也按同样规则转,别让它变字面量
193
+ s = s.replace(/<@!?([0-9A-Za-z]{16,64})>/g, (_m, openid) => tag(openid));
194
+ return s.trim();
195
+ }
@@ -0,0 +1,219 @@
1
+ /**
2
+ * 官方事件 → OneBot v11 事件
3
+ * ==========================
4
+ *
5
+ * 只做「能忠实映射」的部分。官方群聊/单聊**没有**撤回事件、拍一拍、群文件事件,
6
+ * 这些一律不产生(见设计稿 §4),别在这里造一个看起来像的事件糊过去。
7
+ *
8
+ * 时间统一用秒(OneBot 约定),`self_id` 用配置里的机器人 QQ 号。
9
+ */
10
+
11
+ /** 取第一个非空字段:官方各事件的 openid 字段名不统一 */
12
+ function pick(...vals) {
13
+ for (const v of vals) {
14
+ const s = String(v ?? '').trim();
15
+ if (s) return s;
16
+ }
17
+ return '';
18
+ }
19
+
20
+ /**
21
+ * 把 `msg` 拆成 OneBot 消息段。
22
+ *
23
+ * @param {object} msg buildMessage 的产物
24
+ * @param {import('./ids.js').OneBotIds} ids
25
+ * @param {number} [selfUin] 机器人自己的 QQ 号:被 @ 时要用它造 at 段
26
+ */
27
+ export function segmentsOf(msg, ids, selfUin = 0) {
28
+ const segs = [];
29
+
30
+ /**
31
+ * **被 @ 的是机器人自己**:必须单独发一个 at 段。
32
+ *
33
+ * `msg.mentions` 里已经把机器人过滤掉了(插件点名单选人时不该选到它),
34
+ * 早先这里就按「被动回复本身已经指向它,多一个 @ 是噪音」不发段——
35
+ * 但 OneBot 客户端判断「有没有人叫我」**只看 at 段里的 qq 是不是自己**
36
+ * (aurabot 的 `parseMessageEvent`:`d.qq === self`)。
37
+ *
38
+ * 不发段的后果(2026-11-13 实测):@ 她的群消息被当成普通群消息,
39
+ * 掉进「话题相关度」那道闸里,`相关度 0.00 → 不回`——用户看到的就是
40
+ * 「@ 了也没人理」。
41
+ */
42
+ if (msg.mentioned && Number(selfUin)) {
43
+ segs.push({ type: 'at', data: { qq: String(selfUin), name: '' } });
44
+ }
45
+
46
+ // 引用:官方给的是 msgIdx(REFIDX_…),换成我们发出去的 message_id
47
+ const quotedId = ids.messageIdOfIdx(msg.refMsgIdx);
48
+ if (quotedId) segs.push({ type: 'reply', data: { id: String(quotedId) } });
49
+
50
+ // 被 @ 的**其他人**(不含机器人自己;`mentions` 里已经过滤过 bot)
51
+ for (const m of Array.isArray(msg.mentions) ? msg.mentions : []) {
52
+ const qq = ids.user(m.id);
53
+ if (qq) segs.push({ type: 'at', data: { qq: String(qq), name: String(m.name ?? '') } });
54
+ }
55
+
56
+ if (msg.content) segs.push({ type: 'text', data: { text: String(msg.content) } });
57
+
58
+ for (const a of Array.isArray(msg.attachments) ? msg.attachments : []) {
59
+ if (a.type === 'image') {
60
+ segs.push({ type: 'image', data: { file: a.url, url: a.url, summary: '[图片]' } });
61
+ } else if (a.type === 'voice') {
62
+ // 官方直接给了 WAV(SILK 转好的),不用自己转码
63
+ segs.push({ type: 'record', data: { file: a.wavUrl || a.url, url: a.wavUrl || a.url } });
64
+ } else if (a.type === 'video') {
65
+ segs.push({ type: 'video', data: { file: a.url, url: a.url } });
66
+ } else if (a.type === 'file') {
67
+ segs.push({ type: 'file', data: { file: a.url, url: a.url, name: a.filename } });
68
+ }
69
+ }
70
+
71
+ return segs;
72
+ }
73
+
74
+ /** CQ 码形式的纯文本(客户端不解析段时用;只做 text/at/image 三种最常见的) */
75
+ export function rawMessageOf(segs) {
76
+ return segs
77
+ .map((s) => {
78
+ if (s.type === 'text') return String(s.data.text ?? '');
79
+ if (s.type === 'at') return `[CQ:at,qq=${s.data.qq}]`;
80
+ if (s.type === 'image') return '[CQ:image,file=' + String(s.data.file ?? '') + ']';
81
+ if (s.type === 'record') return '[CQ:record,file=' + String(s.data.file ?? '') + ']';
82
+ if (s.type === 'reply') return `[CQ:reply,id=${s.data.id}]`;
83
+ return '';
84
+ })
85
+ .join('');
86
+ }
87
+
88
+ /**
89
+ * 消息事件。
90
+ *
91
+ * @param {object} p
92
+ * @param {object} p.msg buildMessage 产物
93
+ * @param {import('./ids.js').OneBotIds} p.ids
94
+ * @param {number} p.selfUin
95
+ * @param {number} [p.selfOpenid] 机器人自己的 openid(用于过滤自己发的消息)
96
+ */
97
+ export function toMessageEvent({ msg, ids, selfUin }) {
98
+ const isGroup = msg.kind === 'group';
99
+ const segs = segmentsOf(msg, ids, selfUin);
100
+ const at = msg.raw?.author ?? {};
101
+
102
+ return {
103
+ time: Math.floor((Number(msg.receivedAt) || Date.now()) / 1000),
104
+ self_id: Number(selfUin) || 0,
105
+ post_type: 'message',
106
+ message_type: isGroup ? 'group' : 'private',
107
+ sub_type: isGroup ? 'normal' : 'friend',
108
+ message_id: ids.bindMessage(msg),
109
+ ...(isGroup ? { group_id: ids.group(msg.groupId) } : {}),
110
+ user_id: ids.user(msg.userId),
111
+ message: segs,
112
+ raw_message: rawMessageOf(segs),
113
+ font: 0,
114
+ sender: {
115
+ user_id: ids.user(msg.userId),
116
+ nickname: String(msg.userName ?? ''),
117
+ // 官方没有群名片,回落昵称(能力表里标了)
118
+ card: String(msg.userName ?? ''),
119
+ role: isGroup ? String(at.member_role || 'member') : 'member',
120
+ },
121
+ anonymous: null,
122
+ };
123
+ }
124
+
125
+ /**
126
+ * 非消息事件 → OneBot notice / request。
127
+ *
128
+ * @returns {object|null} 认不出来返回 null(不推)
129
+ */
130
+ export function toNotice(eventName, d, { ids, selfUin }) {
131
+ const base = {
132
+ time: Math.floor(Number(d?.timestamp) || Date.now() / 1000),
133
+ self_id: Number(selfUin) || 0,
134
+ post_type: 'notice',
135
+ };
136
+ const groupOpenid = pick(d?.group_openid);
137
+ const groupId = groupOpenid ? ids.group(groupOpenid) : 0;
138
+
139
+ switch (eventName) {
140
+ case 'GROUP_MEMBER_ADD':
141
+ return {
142
+ ...base,
143
+ notice_type: 'group_increase',
144
+ sub_type: 'approve',
145
+ group_id: groupId,
146
+ user_id: ids.user(pick(d?.member_openid, d?.user_openid)),
147
+ operator_id: 0,
148
+ };
149
+ case 'GROUP_MEMBER_REMOVE':
150
+ // 官方不区分主动退群 / 被踢,统一 leave——别猜
151
+ return {
152
+ ...base,
153
+ notice_type: 'group_decrease',
154
+ sub_type: 'leave',
155
+ group_id: groupId,
156
+ user_id: ids.user(pick(d?.member_openid, d?.user_openid)),
157
+ operator_id: 0,
158
+ };
159
+ case 'GROUP_ADD_ROBOT':
160
+ return {
161
+ ...base,
162
+ notice_type: 'notify',
163
+ sub_type: 'bot_group_add',
164
+ group_id: groupId,
165
+ user_id: ids.user(pick(d?.op_member_openid)),
166
+ };
167
+ case 'GROUP_DEL_ROBOT':
168
+ return {
169
+ ...base,
170
+ notice_type: 'notify',
171
+ sub_type: 'bot_group_remove',
172
+ group_id: groupId,
173
+ user_id: 0,
174
+ };
175
+ case 'FRIEND_ADD':
176
+ return { ...base, notice_type: 'friend_add', user_id: ids.user(pick(d?.openid, d?.user_openid)) };
177
+ case 'FRIEND_DEL':
178
+ return { ...base, notice_type: 'friend_remove', user_id: ids.user(pick(d?.openid, d?.user_openid)) };
179
+ default:
180
+ return null;
181
+ }
182
+ }
183
+
184
+ /** 自己撤回后补的那条 `group_recall`(官方没有撤回事件,只能自己补) */
185
+ export function recallNotice({ ids, selfUin, groupId, userId, messageId }) {
186
+ return {
187
+ time: Math.floor(Date.now() / 1000),
188
+ self_id: Number(selfUin) || 0,
189
+ post_type: 'notice',
190
+ notice_type: 'group_recall',
191
+ group_id: Number(groupId) || 0,
192
+ user_id: Number(userId) || 0,
193
+ operator_id: Number(selfUin) || 0,
194
+ message_id: Number(messageId) || 0,
195
+ };
196
+ }
197
+
198
+ /** 连接时的 lifecycle(每个客户端连上各推一次) */
199
+ export function lifecycleEvent(selfUin, subType = 'connect') {
200
+ return {
201
+ time: Math.floor(Date.now() / 1000),
202
+ self_id: Number(selfUin) || 0,
203
+ post_type: 'meta_event',
204
+ meta_event_type: 'lifecycle',
205
+ sub_type: subType,
206
+ };
207
+ }
208
+
209
+ /** 心跳:25s 一次,`status.online` 让客户端知道网关还活着 */
210
+ export function heartbeatEvent(selfUin, interval = 25000) {
211
+ return {
212
+ time: Math.floor(Date.now() / 1000),
213
+ self_id: Number(selfUin) || 0,
214
+ post_type: 'meta_event',
215
+ meta_event_type: 'heartbeat',
216
+ status: { online: true, good: true },
217
+ interval,
218
+ };
219
+ }