From 005154077faba95f6e4d4bab3d496ec5a5570fcd Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Fri, 7 Aug 2026 21:50:28 +0800 Subject: [PATCH] fix(onebot): accept id-less custom music cards (#319) --- README.md | 20 ++++++++++ packages/onebot/src/message-parser.ts | 17 +++++--- packages/onebot/tests/message-parser.test.ts | 42 ++++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b1d5db23..00265270 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,26 @@ WebUI 的“系统设置 → 存储管理”可查看受管数据占用、调整 | MCP | 让支持 MCP 的工具查询或调用 SnowLuma 动作 | [`packages/mcp`](packages/mcp/README.md) | | WebUI | 管理运行状态、日志、账号与连接 | 启动后访问 `http://localhost:5099` | +### 音乐分享消息段兼容 + +标准自定义音乐分享使用 `type: "custom"`,无需提供 `id`。为兼容 NapCat,SnowLuma 也接受省略 `id`、保留平台类型并携带完整自定义数据的写法: + +```json +{ + "type": "music", + "data": { + "type": "163", + "url": "https://music.example/song/1", + "audio": "https://cdn.example/song.mp3", + "title": "歌曲标题", + "image": "https://cdn.example/cover.jpg", + "content": "歌手" + } +} +``` + +这种写法必须提供非空的 `url`、`audio` 和 `title`;一旦提供 `id`,则按对应平台的歌曲编号分享处理。 + ## 开发与贡献 本地开发需要 Node.js 22.13+(23 系需 23.4+)与项目锁定的 pnpm 10.28.0。所有日常开发基于 `dev` 分支: diff --git a/packages/onebot/src/message-parser.ts b/packages/onebot/src/message-parser.ts index 87bab01f..367ce7ec 100644 --- a/packages/onebot/src/message-parser.ts +++ b/packages/onebot/src/message-parser.ts @@ -248,12 +248,19 @@ export async function segmentToElement(type: string, data: Record Object.prototype.hasOwnProperty.call(data, field)); + const usesCustomMusicData = musicType === 'custom' || (!hasMusicId && hasCustomMusicData); + if (usesCustomMusicData) { requireNonEmptyStringField(normalizedType, data, 'url'); requireNonEmptyStringField(normalizedType, data, 'audio'); requireNonEmptyStringField(normalizedType, data, 'title'); - } else if (data.id === undefined || data.id === null || String(data.id).trim() === '') { + } else if (!hasMusicId) { throw new MessageElementValidationError( 'MISSING_FIELD', 'message segment "music" requires field "id" for a platform card', @@ -264,9 +271,9 @@ export async function segmentToElement(type: string, data: Record; - if (musicType === 'custom') { + if (usesCustomMusicData) { postData = { - type: 'custom', + type: musicType, id: undefined, url: String(data.url ?? ''), audio: String(data.audio ?? ''), diff --git a/packages/onebot/tests/message-parser.test.ts b/packages/onebot/tests/message-parser.test.ts index 10514574..b4413aae 100644 --- a/packages/onebot/tests/message-parser.test.ts +++ b/packages/onebot/tests/message-parser.test.ts @@ -729,6 +729,48 @@ describe('parseMessage', () => { expect(parsed.meta.contact.type).toBe('group'); }); + it('accepts id-less custom music data with a platform type for NapCat compatibility', async () => { + const card = JSON.stringify({ + app: 'com.tencent.structmsg', + view: 'music', + prompt: '[音乐]光', + }); + const fetchMock = vi.fn(async (_input: string | URL | Request, _init?: RequestInit) => ({ + ok: true, + text: async () => card, + })); + vi.stubGlobal('fetch', fetchMock); + + try { + const result = await parseMessage( + [{ + type: 'music', + data: { + type: '163', + url: 'https://music.example/song/3402687142', + audio: 'https://cdn.example/3402687142.mp3', + title: '光', + image: 'https://cdn.example/3402687142.jpg', + content: '陈粒', + }, + }] as any, + false, + ); + + expect(result).toEqual([{ type: 'json', text: card }]); + expect(JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body))).toEqual({ + type: '163', + url: 'https://music.example/song/3402687142', + audio: 'https://cdn.example/3402687142.mp3', + title: '光', + image: 'https://cdn.example/3402687142.jpg', + singer: '陈粒', + }); + } finally { + vi.unstubAllGlobals(); + } + }); + it('parses signed music cards and buildSendElems preserves non-ASCII JSON', async () => { const card = JSON.stringify({ app: 'com.tencent.structmsg',