// 只读导出群/私聊 history 的段结构,便于人工核对「事件段 vs history 段」差异。 // 无副作用。凭据只从环境变量读取。 // // 用法: // NAPCAT_WS_TOKEN=xxx node tools/probe/history-dump.mjs # 群 + 私聊各 25 条 // NAPCAT_WS_TOKEN=xxx node tools/probe/history-dump.mjs group 60 // NAPCAT_WS_TOKEN=xxx node tools/probe/history-dump.mjs --grep=record # 只看含某段的 import fs from 'node:fs'; const WS_URL = process.env.NAPCAT_WS_URL || 'ws://192.168.5.32:16099'; const TOKEN = process.env.NAPCAT_WS_TOKEN; const GROUP = Number(process.env.PROBE_GROUP || 933280401); const FRIEND = Number(process.env.PROBE_FRIEND || 2161656029); if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); } const args = process.argv.slice(2); const grep = (args.find((a) => a.startsWith('--grep=')) || '').slice(7); const count = Number(args.find((a) => /^\d+$/.test(a)) || 25); const which = args.find((a) => a === 'group' || a === 'private') || 'both'; const pending = new Map(); let opened = false; const ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`); ws.onopen = () => { opened = true; }; ws.onmessage = (ev) => { let o; try { o = JSON.parse(String(ev.data)); } catch { return; } if (o.echo && pending.has(o.echo)) { pending.get(o.echo).resolve(o); pending.delete(o.echo); } }; for (let i = 0; i < 100 && !opened; i++) await new Promise((r) => setTimeout(r, 100)); if (!opened) { console.error('连接超时'); process.exit(1); } const api = (action, params, ms = 30000) => new Promise((resolve) => { const echo = `hd-${action}-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`; const to = setTimeout(() => { pending.delete(echo); resolve({ __timeout: true }); }, ms); pending.set(echo, { resolve: (o) => { clearTimeout(to); resolve(o); }, to }); ws.send(JSON.stringify({ action, params, echo })); }); const brief = (x) => { const d = x.data || {}; switch (x.type) { case 'text': return `text<${String(d.text ?? '').replace(/\s+/g, ' ').slice(0, 46)}>`; case 'record': return `record`; case 'image': return `image`; case 'video': return `video`; case 'file': return `file`; case 'at': return `at`; case 'music': return `music`; case 'reply': return `reply`; case 'location': return `location<${d.lat},${d.lon}>`; case 'node': return `node<${d.nickname ?? '-'}>`; case 'json': { let t = 'json'; try { const o = JSON.parse(d.data); t = `json`; const n = o.meta?.detail?.news; if (n) t += JSON.stringify(n).slice(0, 100); } catch { t = 'json'; } return t; } default: return `${x.type}<${JSON.stringify(d).slice(0, 60)}>`; } }; async function dump(action, key, val, label) { const r = await api(action, { [key]: val, count }); const list = (r?.data?.messages ?? []).slice().reverse(); console.log(`\n===== ${label}(${list.length} 条,retcode=${r.retcode})=====`); for (const m of list) { const segs = (m.message ?? []).map(brief); const types = (m.message ?? []).map((x) => x.type); if (grep && !types.includes(grep) && !JSON.stringify(m).includes(grep)) continue; console.log(`${String(m.real_seq ?? '-').padEnd(6)}${(m.message ?? []).length === 0 ? '[空消息/已撤回]' : segs.join(' | ').slice(0, 210)}`); } return list; } const out = []; if (which !== 'private') out.push(...await dump('get_group_msg_history', 'group_id', GROUP, '群 ' + GROUP)); if (which !== 'group') out.push(...await dump('get_friend_msg_history', 'user_id', FRIEND, '私聊 ' + FRIEND)); if (process.env.DUMP_RAW) { fs.writeFileSync(process.env.DUMP_RAW, out.map((m) => JSON.stringify(m)).join('\n') + '\n'); console.log('\n原始 -> ' + process.env.DUMP_RAW); } try { ws.close(); } catch {} process.exit(0);