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:
5 files changed
+28
-8
No files matched your search
@@ -1,6 +1,7 @@
|
|||||||
import type {
|
import type {
|
||||||
AnyMessageSegment,
|
AnyMessageSegment,
|
||||||
ContactSegment,
|
ContactSegment,
|
||||||
|
FaceSegment,
|
||||||
ImageSegment,
|
ImageSegment,
|
||||||
JsonObject,
|
JsonObject,
|
||||||
LocationSegment,
|
LocationSegment,
|
||||||
@@ -51,8 +52,8 @@ export class MessageChain<THasReply extends boolean = false> implements MessageC
|
|||||||
return this.text('\n');
|
return this.text('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
face(id: number | string): MessageChain<THasReply> {
|
face(id: number | string, options: Omit<FaceSegment['data'], 'id'> = {}): MessageChain<THasReply> {
|
||||||
return this.addSegment(segments.face(id));
|
return this.addSegment(segments.face(id, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
at(qq: number | 'all', options: { name?: string; uid?: string } = {}): MessageChain<THasReply> {
|
at(qq: number | 'all', options: { name?: string; uid?: string } = {}): MessageChain<THasReply> {
|
||||||
@@ -203,8 +204,8 @@ export function br(): MessageChain<false> {
|
|||||||
return chain().br();
|
return chain().br();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function face(id: number | string): MessageChain<false> {
|
export function face(id: number | string, options: Omit<FaceSegment['data'], 'id'> = {}): MessageChain<false> {
|
||||||
return chain().face(id);
|
return chain().face(id, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function at(qq: number | 'all', options: { name?: string; uid?: string } = {}): MessageChain<false> {
|
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':
|
case 'text':
|
||||||
return segments.text(data.text ?? '');
|
return segments.text(data.text ?? '');
|
||||||
case 'face':
|
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':
|
case 'at':
|
||||||
if (data.qq === 'all') return segments.at('all');
|
if (data.qq === 'all') return segments.at('all');
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ export const segments = {
|
|||||||
return seg('text', { text });
|
return seg('text', { text });
|
||||||
},
|
},
|
||||||
|
|
||||||
face(id: number | string): FaceSegment {
|
face(id: number | string, options: Omit<FaceSegment['data'], 'id'> = {}): FaceSegment {
|
||||||
return seg('face', { id: String(id) });
|
return seg('face', { id: String(id), ...options });
|
||||||
},
|
},
|
||||||
|
|
||||||
at(qq: number | 'all', options: { name?: string; uid?: string } = {}): AtSegment {
|
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 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 AtSegment = MessageSegment<'at', { qq: string; name?: string; uid?: string }>;
|
||||||
export type ReplySegment = MessageSegment<'reply', { id: string }>;
|
export type ReplySegment = MessageSegment<'reply', { id: string }>;
|
||||||
export type ImageSegment = MessageSegment<'image', {
|
export type ImageSegment = MessageSegment<'image', {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { describe, expect, it, vi } from 'vitest';
|
import { describe, expect, it, vi } from 'vitest';
|
||||||
import {
|
import {
|
||||||
atAll,
|
atAll,
|
||||||
|
chain,
|
||||||
|
face,
|
||||||
fromCQString,
|
fromCQString,
|
||||||
message,
|
message,
|
||||||
parseSegments,
|
parseSegments,
|
||||||
@@ -9,6 +11,20 @@ import {
|
|||||||
} from '../src';
|
} from '../src';
|
||||||
|
|
||||||
describe('MessageChain', () => {
|
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', () => {
|
it('builds fluent segment chains', () => {
|
||||||
const chain = text('hello').at(10001).br().image('/tmp/a.png');
|
const chain = text('hello').at(10001).br().image('/tmp/a.png');
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user