feat(sdk): preserve system face animation choices
Expose large in face builders and preserve it through CQ parsing and serialization. Reject invalid animation choices instead of silently discarding them. Fixes #483
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type {
|
||||
AnyMessageSegment,
|
||||
ContactSegment,
|
||||
FaceSegment,
|
||||
ImageSegment,
|
||||
JsonObject,
|
||||
LocationSegment,
|
||||
@@ -51,8 +52,8 @@ export class MessageChain<THasReply extends boolean = false> implements MessageC
|
||||
return this.text('\n');
|
||||
}
|
||||
|
||||
face(id: number | string): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.face(id));
|
||||
face(id: number | string, options: Omit<FaceSegment['data'], 'id'> = {}): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.face(id, options));
|
||||
}
|
||||
|
||||
at(qq: number | 'all', options: { name?: string; uid?: string } = {}): MessageChain<THasReply> {
|
||||
@@ -203,8 +204,8 @@ export function br(): MessageChain<false> {
|
||||
return chain().br();
|
||||
}
|
||||
|
||||
export function face(id: number | string): MessageChain<false> {
|
||||
return chain().face(id);
|
||||
export function face(id: number | string, options: Omit<FaceSegment['data'], 'id'> = {}): MessageChain<false> {
|
||||
return chain().face(id, options);
|
||||
}
|
||||
|
||||
export function at(qq: number | 'all', options: { name?: string; uid?: string } = {}): MessageChain<false> {
|
||||
|
||||
@@ -70,7 +70,10 @@ function segmentFromCq(type: string, data: Record<string, string>): AnyMessageSe
|
||||
case 'text':
|
||||
return segments.text(data.text ?? '');
|
||||
case 'face':
|
||||
return segments.face(data.id ?? '0');
|
||||
if (data.large === undefined) return segments.face(data.id ?? '0');
|
||||
if (data.large === 'true' || data.large === '1') return segments.face(data.id ?? '0', { large: true });
|
||||
if (data.large === 'false' || data.large === '0') return segments.face(data.id ?? '0', { large: false });
|
||||
throw new Error('face.large must be true, false, 1, or 0');
|
||||
case 'at':
|
||||
if (data.qq === 'all') return segments.at('all');
|
||||
{
|
||||
|
||||
@@ -29,8 +29,8 @@ export const segments = {
|
||||
return seg('text', { text });
|
||||
},
|
||||
|
||||
face(id: number | string): FaceSegment {
|
||||
return seg('face', { id: String(id) });
|
||||
face(id: number | string, options: Omit<FaceSegment['data'], 'id'> = {}): FaceSegment {
|
||||
return seg('face', { id: String(id), ...options });
|
||||
},
|
||||
|
||||
at(qq: number | 'all', options: { name?: string; uid?: string } = {}): AtSegment {
|
||||
|
||||
@@ -9,7 +9,7 @@ export interface MessageSegment<
|
||||
}
|
||||
|
||||
export type TextSegment = MessageSegment<'text', { text: string }>;
|
||||
export type FaceSegment = MessageSegment<'face', { id: string }>;
|
||||
export type FaceSegment = MessageSegment<'face', { id: string; large?: boolean }>;
|
||||
export type AtSegment = MessageSegment<'at', { qq: string; name?: string; uid?: string }>;
|
||||
export type ReplySegment = MessageSegment<'reply', { id: string }>;
|
||||
export type ImageSegment = MessageSegment<'image', {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
atAll,
|
||||
chain,
|
||||
face,
|
||||
fromCQString,
|
||||
message,
|
||||
parseSegments,
|
||||
@@ -9,6 +11,20 @@ import {
|
||||
} from '../src';
|
||||
|
||||
describe('MessageChain', () => {
|
||||
it.each([false, true])('preserves face animation selection through builders and CQ, large=%s', (large) => {
|
||||
const expected = [{ type: 'face', data: { id: '451', large } }];
|
||||
const built = face(451, { large });
|
||||
expect(built.toSegments()).toEqual(expected);
|
||||
expect(chain().face(451, { large }).toSegments()).toEqual(expected);
|
||||
expect(message.face(451, { large })).toEqual(expected[0]);
|
||||
expect(parseSegments(toCQString(built))).toEqual(expected);
|
||||
expect(parseSegments(`[CQ:face,id=451,large=${large ? 1 : 0}]`)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('rejects an invalid face animation choice instead of discarding it', () => {
|
||||
expect(() => parseSegments('[CQ:face,id=451,large=invalid]')).toThrow('face.large');
|
||||
});
|
||||
|
||||
it('builds fluent segment chains', () => {
|
||||
const chain = text('hello').at(10001).br().image('/tmp/a.png');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user