@zaofan/dsh-qqbot 1.13.25 → 1.13.28
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/client/qqbot-settings.js +9 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +5 -0
- package/dist/config.js.map +1 -1
- package/dist/features/image-path-surface.d.ts +38 -0
- package/dist/features/image-path-surface.d.ts.map +1 -0
- package/dist/features/image-path-surface.js +225 -0
- package/dist/features/image-path-surface.js.map +1 -0
- package/dist/features/qun-login.d.ts +21 -0
- package/dist/features/qun-login.d.ts.map +1 -1
- package/dist/features/qun-login.js +202 -59
- package/dist/features/qun-login.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -1
- package/dist/index.js.map +1 -1
- package/dist/middleware/sticker-capture.d.ts +9 -0
- package/dist/middleware/sticker-capture.d.ts.map +1 -1
- package/dist/middleware/sticker-capture.js +56 -19
- package/dist/middleware/sticker-capture.js.map +1 -1
- package/dist/session/session-manager.d.ts.map +1 -1
- package/dist/session/session-manager.js +185 -13
- package/dist/session/session-manager.js.map +1 -1
- package/dist/transport/inbound.d.ts.map +1 -1
- package/dist/transport/inbound.js +29 -0
- package/dist/transport/inbound.js.map +1 -1
- package/docs/extension-contract.md +191 -2
- package/examples/ext-commands//346/214/211/351/222/256/346/265/213/350/257/225.mjs +321 -0
- package/examples/ext-tools/send_md3.mjs +406 -0
- package/package.json +4 -3
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 按钮测试.mjs — `/按钮测试` 斜杠命令(带按钮卡片 + 一次性回调)
|
|
3
|
+
*
|
|
4
|
+
* ★ 2026-11-24 主人需求(原话):
|
|
5
|
+
* 「写一个斜杠命令,弹一个按钮卡片,按第一次,qq发消息显示这个是谁点的,
|
|
6
|
+
* 再点一次,显示你好,然后消灭回调,第三次点就没用了」
|
|
7
|
+
*
|
|
8
|
+
* 状态机(按 cardId 存盘,跨"命令侧/回调侧两个模块实例"也能认出来):
|
|
9
|
+
* step 0 → 点第一次:回「是 <openid>(昵称)点的」,step 改成 1
|
|
10
|
+
* step 1 → 点第二次:回「你好」,step 改成 2,**同时消灭回调**(卡表移除 + 状态删除)
|
|
11
|
+
* step ≥2 → 走不到这里:卡表里已经没有这张卡,插件不会派发(第三次点 = 没反应)
|
|
12
|
+
*
|
|
13
|
+
* ⚠️ 从 `/会话列表` 抄来的四条硬经验(都踩过,别再犯):
|
|
14
|
+
* 1. 按钮 id **绝不能带冒号** —— 插件把 button_data 拆成 `ext:<工具名>:<卡id>:<按钮id>`,
|
|
15
|
+
* 要求恰好 3 段;带冒号会 split 出 4 段被静默丢弃(现象:点了只弹"操作成功")。
|
|
16
|
+
* 2. `TOOL` 必须与**文件名**、`name[0]` 三者一致,否则插件查表查不到。
|
|
17
|
+
* 3. 命令侧 `ctx.caps` 有值,**回调侧的 ctx 是把能力平铺在自身**(没有 caps 键)
|
|
18
|
+
* ⇒ 一律写 `ctx?.caps ?? ctx`。
|
|
19
|
+
* 4. QQ 内联键盘:总行数 ≤ 5、每行 ≤ 5 按钮,超了直接被拒(还会静默退化成纯文本)。
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const TOOL = '按钮测试'; // ★ 必须与文件名一致
|
|
23
|
+
const CARD_EXPIRE_MS = 10 * 60_000; // 卡表里保留多久
|
|
24
|
+
const STATE_EXPIRE_MS = 10 * 60_000; // 状态文件里保留多久
|
|
25
|
+
|
|
26
|
+
const cn = (v) => (v === undefined || v === null ? '' : String(v));
|
|
27
|
+
const short = (s, n) => {
|
|
28
|
+
const t = cn(s).replace(/\s+/g, ' ').trim();
|
|
29
|
+
return t.length > n ? t.slice(0, n - 1) + '…' : t;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** dataRoot:命令侧从 caps,回调侧可能平铺 */
|
|
33
|
+
function dataRootOf(ctx) {
|
|
34
|
+
const c = ctx?.caps ?? ctx ?? {};
|
|
35
|
+
return cn(c.dataRoot || ctx?.dataRoot || '');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 状态文件(按 cardId 存 step) */
|
|
39
|
+
function stateFileOf(ctx) {
|
|
40
|
+
const root = dataRootOf(ctx);
|
|
41
|
+
return root ? `${root}/.qqbot/btn-test-state.json` : '';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function readStates(ctx) {
|
|
45
|
+
try {
|
|
46
|
+
const { readFileSync } = await import('node:fs');
|
|
47
|
+
const all = JSON.parse(readFileSync(stateFileOf(ctx), 'utf8'));
|
|
48
|
+
return all && typeof all === 'object' ? all : {};
|
|
49
|
+
} catch {
|
|
50
|
+
return {};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function writeStates(ctx, all) {
|
|
55
|
+
try {
|
|
56
|
+
const { writeFileSync, mkdirSync } = await import('node:fs');
|
|
57
|
+
const { dirname } = await import('node:path');
|
|
58
|
+
const f = stateFileOf(ctx);
|
|
59
|
+
if (!f) return false;
|
|
60
|
+
const now = Date.now();
|
|
61
|
+
for (const k of Object.keys(all)) if (Number(all[k]?.expireAt ?? 0) < now) delete all[k];
|
|
62
|
+
const keys = Object.keys(all);
|
|
63
|
+
if (keys.length > 30) for (const k of keys.slice(0, keys.length - 30)) delete all[k];
|
|
64
|
+
mkdirSync(dirname(f), { recursive: true });
|
|
65
|
+
writeFileSync(f, JSON.stringify(all, null, 2));
|
|
66
|
+
return true;
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 卡表:插件靠它决定"这个按钮事件归谁",删掉 = 后续点击不再派发 */
|
|
73
|
+
function cardTableFile(ctx) {
|
|
74
|
+
const root = dataRootOf(ctx);
|
|
75
|
+
return root ? `${root}/.qqbot/ext-tool-cards.json` : '';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function regCard(ctx, cardId, buttonIds) {
|
|
79
|
+
const f = cardTableFile(ctx);
|
|
80
|
+
if (!f) return false;
|
|
81
|
+
try {
|
|
82
|
+
const { readFileSync, writeFileSync, mkdirSync } = await import('node:fs');
|
|
83
|
+
const { dirname } = await import('node:path');
|
|
84
|
+
let table = { version: 1, cards: [] };
|
|
85
|
+
try {
|
|
86
|
+
table = JSON.parse(readFileSync(f, 'utf8')) ?? table;
|
|
87
|
+
} catch {
|
|
88
|
+
/* 首次或坏文件 */
|
|
89
|
+
}
|
|
90
|
+
if (!Array.isArray(table.cards)) table.cards = [];
|
|
91
|
+
const mc = ctx?.message ?? {};
|
|
92
|
+
const rt = ctx?.replyTarget ?? {};
|
|
93
|
+
const pe = ctx?.peer ?? {};
|
|
94
|
+
const scope = cn(rt.scope || ctx?.scope || pe.scope || mc.kind || ((mc.groupOpenid || pe.groupId || pe.peerId) ? 'group' : 'c2c'));
|
|
95
|
+
const targetId = cn(mc.groupOpenid || rt.targetId || pe.groupId || pe.groupOpenid || pe.peerId || pe.id || mc.senderId);
|
|
96
|
+
const now = Date.now();
|
|
97
|
+
table.cards = table.cards.filter((c) => Number(c?.expireAt ?? 0) > now && !(c?.toolName === TOOL && c?.cardId === cardId));
|
|
98
|
+
table.cards.push({ toolName: TOOL, cardId, buttonIds, scope, targetId, createdAt: now, expireAt: now + CARD_EXPIRE_MS });
|
|
99
|
+
if (table.cards.length > 200) table.cards = table.cards.slice(-200);
|
|
100
|
+
mkdirSync(dirname(f), { recursive: true });
|
|
101
|
+
writeFileSync(f, JSON.stringify(table, null, 2));
|
|
102
|
+
return true;
|
|
103
|
+
} catch {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** ★ 消灭回调:把这张卡从卡表里删掉 —— 之后点击插件不会再派发 */
|
|
109
|
+
async function killCard(ctx, cardId) {
|
|
110
|
+
const f = cardTableFile(ctx);
|
|
111
|
+
if (!f) return false;
|
|
112
|
+
try {
|
|
113
|
+
const { readFileSync, writeFileSync } = await import('node:fs');
|
|
114
|
+
let table = { version: 1, cards: [] };
|
|
115
|
+
try {
|
|
116
|
+
table = JSON.parse(readFileSync(f, 'utf8')) ?? table;
|
|
117
|
+
} catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
if (!Array.isArray(table.cards)) return false;
|
|
121
|
+
const before = table.cards.length;
|
|
122
|
+
table.cards = table.cards.filter((c) => !(c?.toolName === TOOL && c?.cardId === cardId));
|
|
123
|
+
if (table.cards.length === before) return false;
|
|
124
|
+
writeFileSync(f, JSON.stringify(table, null, 2));
|
|
125
|
+
return true;
|
|
126
|
+
} catch {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 按钮(id 不能带冒号) */
|
|
132
|
+
function btn(id, label, style = 1) {
|
|
133
|
+
return {
|
|
134
|
+
id,
|
|
135
|
+
render_data: { label: short(label, 18), visited_label: short(label, 18), style },
|
|
136
|
+
action: { type: 1, permission: { type: 2 }, data: id, unsupport_tips: '请升级QQ客户端后重试' },
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 拼 QQ inline_keyboard;★ 工具名必须是本命令 */
|
|
141
|
+
function keyboardOf(rows, cardId) {
|
|
142
|
+
return {
|
|
143
|
+
content: {
|
|
144
|
+
rows: rows.map((r) => ({
|
|
145
|
+
buttons: r.map((b) => ({ ...b, action: { ...b.action, data: `ext:${TOOL}:${cardId}:${b.id}` } })),
|
|
146
|
+
})),
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 发卡(markdown + 按钮键盘);四级兜底。
|
|
153
|
+
* 返回 'kb' = 带按钮发出去了 / 'text' = 退化成纯文本(按钮没了)/ 'fail' = 什么都没发出去
|
|
154
|
+
*/
|
|
155
|
+
let LAST_ERR = '';
|
|
156
|
+
async function sendCard(ctx, text, keyboard) {
|
|
157
|
+
const caps = ctx?.caps ?? ctx ?? {};
|
|
158
|
+
const mc = ctx?.message ?? {};
|
|
159
|
+
const rt = ctx?.replyTarget ?? {};
|
|
160
|
+
const pe = ctx?.peer ?? {};
|
|
161
|
+
const scope = cn(rt.scope || ctx?.scope || pe.scope || mc.kind || ((mc.groupOpenid || pe.groupId || pe.peerId) ? 'group' : 'c2c'));
|
|
162
|
+
const gid = cn(mc.groupOpenid || rt.targetId || rt.openid || rt.id || pe.groupId || pe.groupOpenid || (scope === 'group' ? (pe.peerId || pe.id) : '') || '');
|
|
163
|
+
const uid = cn(mc.senderId || rt.openid || rt.id || pe.openid || pe.userId || '');
|
|
164
|
+
const api = caps.api;
|
|
165
|
+
if (keyboard && typeof api === 'function' && (gid || uid)) {
|
|
166
|
+
const path = gid ? `/v2/groups/${gid}/messages` : `/v2/users/${uid}/messages`;
|
|
167
|
+
const body = { msg_type: 2, markdown: { content: text }, keyboard, ...(gid ? {} : { content: text }) };
|
|
168
|
+
try {
|
|
169
|
+
const r = await api(path, { method: 'POST', body });
|
|
170
|
+
if (r && r.ok !== false) {
|
|
171
|
+
LAST_ERR = '';
|
|
172
|
+
return 'kb';
|
|
173
|
+
}
|
|
174
|
+
LAST_ERR = cn(r?.err?.human ?? r?.err?.message ?? r?.err?.code ?? 'ok=false');
|
|
175
|
+
} catch (e) {
|
|
176
|
+
LAST_ERR = cn(e?.message ?? e);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
if (keyboard && typeof caps.markdownCard === 'function') {
|
|
181
|
+
const r2 = await caps.markdownCard(text, keyboard);
|
|
182
|
+
if (r2) return 'kb';
|
|
183
|
+
}
|
|
184
|
+
} catch {
|
|
185
|
+
/* ignore */
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
if (keyboard && typeof ctx?.sender?.sendMarkdownWithKeyboard === 'function') {
|
|
189
|
+
await ctx.sender.sendMarkdownWithKeyboard(ctx.replyTarget, text, keyboard);
|
|
190
|
+
return 'kb';
|
|
191
|
+
}
|
|
192
|
+
} catch {
|
|
193
|
+
/* ignore */
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
if (typeof caps.markdown === 'function' && (await caps.markdown(text))) return 'text';
|
|
197
|
+
} catch {
|
|
198
|
+
/* ignore */
|
|
199
|
+
}
|
|
200
|
+
return 'fail';
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let seq = 0;
|
|
204
|
+
|
|
205
|
+
export default {
|
|
206
|
+
name: ['按钮测试', 'btntest'],
|
|
207
|
+
description: '弹一张带按钮的卡片:点第一次显示是谁点的,点第二次说你好并让这张卡失效',
|
|
208
|
+
usage: '/按钮测试',
|
|
209
|
+
|
|
210
|
+
async handler(ctx) {
|
|
211
|
+
const cardId = `${TOOL}-${++seq}-${Date.now().toString(36)}`;
|
|
212
|
+
const kb = keyboardOf([[btn('go', '点我试试', 1)]], cardId);
|
|
213
|
+
|
|
214
|
+
const text = [
|
|
215
|
+
'### 🔘 按钮测试',
|
|
216
|
+
'',
|
|
217
|
+
'这张卡只有 **两次** 有效点击:',
|
|
218
|
+
'',
|
|
219
|
+
'- 第一次:告诉你**是谁**点的',
|
|
220
|
+
'- 第二次:说声「你好」,然后这张卡就**彻底失效**',
|
|
221
|
+
'',
|
|
222
|
+
'> 失效之后再点,不会有任何反应 —— 那正是"消灭回调"生效了。',
|
|
223
|
+
].join('\n');
|
|
224
|
+
|
|
225
|
+
const mode = await sendCard(ctx, text, kb);
|
|
226
|
+
|
|
227
|
+
// 登记卡表(插件靠它决定按钮事件归谁)
|
|
228
|
+
const allIds = ['go'];
|
|
229
|
+
try {
|
|
230
|
+
const caps = ctx?.caps ?? ctx ?? {};
|
|
231
|
+
if (typeof caps.registerInteractionCard === 'function') {
|
|
232
|
+
caps.registerInteractionCard({ cardId, buttonIds: allIds, expireAt: Date.now() + CARD_EXPIRE_MS });
|
|
233
|
+
}
|
|
234
|
+
} catch {
|
|
235
|
+
/* ignore */
|
|
236
|
+
}
|
|
237
|
+
await regCard(ctx, cardId, allIds);
|
|
238
|
+
|
|
239
|
+
// 状态落盘(回调侧可能是另一个模块实例)
|
|
240
|
+
const all = await readStates(ctx);
|
|
241
|
+
all[cardId] = { step: 0, createdAt: Date.now(), expireAt: Date.now() + STATE_EXPIRE_MS };
|
|
242
|
+
await writeStates(ctx, all);
|
|
243
|
+
|
|
244
|
+
if (mode !== 'kb') {
|
|
245
|
+
return mode === 'text'
|
|
246
|
+
? `按钮没发出去(QQ 拒了键盘:${short(LAST_ERR, 60) || '未知'}),只能纯文本显示`
|
|
247
|
+
: '卡片完全没发出去(当前环境可能不支持富文本卡片)';
|
|
248
|
+
}
|
|
249
|
+
return '';
|
|
250
|
+
},
|
|
251
|
+
|
|
252
|
+
async onInteraction(ctx, info) {
|
|
253
|
+
const cardId = cn(info?.cardId);
|
|
254
|
+
const bid = cn(info?.buttonId);
|
|
255
|
+
|
|
256
|
+
// ★ 点击者身份:**扩展命令的回调 ctx 用 performer,不是 actor**
|
|
257
|
+
// (2026-11-24 真机实测:ctx 顶层键里有 performer,actor 是 null;
|
|
258
|
+
// performer = { openid, name, isOwner },isOwner 已按 owners 算好。
|
|
259
|
+
// 再兜底一次 event.group_member_openid —— QQ 的 type=11 事件里它是有的)
|
|
260
|
+
const perf = (ctx?.performer ?? ctx?.caps?.performer ?? {}) || {};
|
|
261
|
+
const ev = (info?.event ?? {}) || {};
|
|
262
|
+
const openid = cn(perf.openid) || cn(ev.group_member_openid) || cn(ev.user_openid);
|
|
263
|
+
const name = cn(perf.name);
|
|
264
|
+
const isOwner = perf.isOwner === true;
|
|
265
|
+
|
|
266
|
+
const all = await readStates(ctx);
|
|
267
|
+
const st = all[cardId];
|
|
268
|
+
|
|
269
|
+
// 状态没了(已用过 / 已过期)—— 理论上卡表也已删,走不到这里;留着兜底
|
|
270
|
+
if (!st) return '这张卡已经失效啦~ 重新发一次 /按钮测试 吧';
|
|
271
|
+
|
|
272
|
+
if (bid !== 'go') return '';
|
|
273
|
+
|
|
274
|
+
const who = openid
|
|
275
|
+
? `${openid}${name ? `(${name})` : ''}${isOwner ? ' 👑' : ''}`
|
|
276
|
+
: '(拿不到 openid,可能是匿名/系统事件)';
|
|
277
|
+
|
|
278
|
+
// ── 第一次点:告诉他"是谁点的" + 把所有可用字段打出来(诊断用)──
|
|
279
|
+
if (Number(st.step ?? 0) === 0) {
|
|
280
|
+
st.step = 1;
|
|
281
|
+
st.firstBy = openid;
|
|
282
|
+
all[cardId] = st;
|
|
283
|
+
await writeStates(ctx, all);
|
|
284
|
+
|
|
285
|
+
// 拿得到就直接说"是谁";拿不到才打诊断(避免每次点都刷一屏)
|
|
286
|
+
if (openid) {
|
|
287
|
+
const label = name || `${openid.slice(0, 8)}…`;
|
|
288
|
+
return `👉 是 ${label}${isOwner ? ' 👑(主人)' : ''} 点的\n\nopenid:\`${openid}\``;
|
|
289
|
+
}
|
|
290
|
+
const evKeys = Object.keys(ev).sort();
|
|
291
|
+
const L = [];
|
|
292
|
+
L.push('⚠️ 拿不到 openid —— 把下面这段发给我');
|
|
293
|
+
L.push('');
|
|
294
|
+
L.push('**ctx 顶层键**:' + (Object.keys(ctx ?? {}).sort().join(', ') || '(空)'));
|
|
295
|
+
L.push('performer = ' + JSON.stringify(perf));
|
|
296
|
+
L.push('event 顶层键:' + (evKeys.join(', ') || '(空)'));
|
|
297
|
+
L.push('event.group_member_openid = ' + String(ev.group_member_openid));
|
|
298
|
+
L.push('event.user_openid = ' + String(ev.user_openid));
|
|
299
|
+
return L.join('\n');
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// ── 第二次点:说你好,然后**消灭回调** ──
|
|
303
|
+
if (Number(st.step ?? 0) === 1) {
|
|
304
|
+
// 先删状态、再删卡表 —— 顺序无所谓,但两个都要删干净
|
|
305
|
+
delete all[cardId];
|
|
306
|
+
await writeStates(ctx, all);
|
|
307
|
+
const killed = await killCard(ctx, cardId);
|
|
308
|
+
return [
|
|
309
|
+
`你好呀 ${name || (openid ? openid.slice(0, 8) + '…' : '朋友')} 👋`,
|
|
310
|
+
'',
|
|
311
|
+
killed ? '这张卡的回调已经**消灭**了 —— 再点不会有任何反应。' : '(卡表没删成功,但状态已清,再点会提示失效)',
|
|
312
|
+
].join('\n');
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// 兜底:step 异常
|
|
316
|
+
delete all[cardId];
|
|
317
|
+
await writeStates(ctx, all);
|
|
318
|
+
await killCard(ctx, cardId);
|
|
319
|
+
return '这张卡已经用完了~';
|
|
320
|
+
},
|
|
321
|
+
};
|