lint
This commit is contained in:
977 files changed
+198426
-158324
No files matched your search
+270
-212
@@ -1,286 +1,344 @@
|
||||
import type {
|
||||
AnyMessageSegment,
|
||||
ContactSegment,
|
||||
FaceSegment,
|
||||
ImageSegment,
|
||||
JsonObject,
|
||||
LocationSegment,
|
||||
MessageChainLike,
|
||||
MusicSegment,
|
||||
OutgoingMessage,
|
||||
RecordSegment,
|
||||
RequestOptions,
|
||||
ShareSegment,
|
||||
VideoSegment,
|
||||
XmlSegment,
|
||||
AnyMessageSegment,
|
||||
ContactSegment,
|
||||
FaceSegment,
|
||||
ImageSegment,
|
||||
JsonObject,
|
||||
LocationSegment,
|
||||
MessageChainLike,
|
||||
MusicSegment,
|
||||
OutgoingMessage,
|
||||
RecordSegment,
|
||||
RequestOptions,
|
||||
ShareSegment,
|
||||
VideoSegment,
|
||||
XmlSegment,
|
||||
} from '../types/index';
|
||||
import { segmentsToCQString } from './cq-format';
|
||||
import { segments } from './segments';
|
||||
|
||||
export interface MessageChainSender {
|
||||
sendGroupMessage(groupId: number, message: OutgoingMessage, options?: RequestOptions & { autoEscape?: boolean }): Promise<unknown>;
|
||||
sendPrivateMessage(userId: number, message: OutgoingMessage, options?: RequestOptions & { autoEscape?: boolean }): Promise<unknown>;
|
||||
sendGroupMessage(
|
||||
groupId: number,
|
||||
message: OutgoingMessage,
|
||||
options?: RequestOptions & { autoEscape?: boolean },
|
||||
): Promise<unknown>;
|
||||
sendPrivateMessage(
|
||||
userId: number,
|
||||
message: OutgoingMessage,
|
||||
options?: RequestOptions & { autoEscape?: boolean },
|
||||
): Promise<unknown>;
|
||||
}
|
||||
|
||||
const SINGLE_USE_TYPES = new Set(['reply']);
|
||||
|
||||
export class MessageChain<THasReply extends boolean = false> implements MessageChainLike {
|
||||
declare private readonly replyStateBrand?: THasReply;
|
||||
declare private readonly replyStateBrand?: THasReply;
|
||||
|
||||
private constructor(
|
||||
private readonly items: AnyMessageSegment[] = [],
|
||||
private readonly used: ReadonlySet<string> = new Set(),
|
||||
) { }
|
||||
private constructor(
|
||||
private readonly items: AnyMessageSegment[] = [],
|
||||
private readonly used: ReadonlySet<string> = new Set(),
|
||||
) {}
|
||||
|
||||
static empty(): MessageChain<false> {
|
||||
return new MessageChain<false>();
|
||||
}
|
||||
|
||||
get length(): number {
|
||||
return this.items.length;
|
||||
}
|
||||
|
||||
get isEmpty(): boolean {
|
||||
return this.items.length === 0;
|
||||
}
|
||||
|
||||
text(text = ''): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.text(text));
|
||||
}
|
||||
|
||||
br(): MessageChain<THasReply> {
|
||||
return this.text('\n');
|
||||
}
|
||||
|
||||
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> {
|
||||
return this.addSegment(segments.at(qq, options));
|
||||
}
|
||||
|
||||
atAll(): MessageChain<THasReply> {
|
||||
return this.at('all');
|
||||
}
|
||||
|
||||
reply(this: MessageChain<false>, id: number | string): MessageChain<true> {
|
||||
return this.addSegment(segments.reply(id), 'reply') as MessageChain<true>;
|
||||
}
|
||||
|
||||
image(file: string, options: Omit<ImageSegment['data'], 'file'> = {}): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.image(file, options));
|
||||
}
|
||||
|
||||
record(file: string, options: Omit<RecordSegment['data'], 'file'> = {}): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.record(file, options));
|
||||
}
|
||||
|
||||
video(file: string, options: Omit<VideoSegment['data'], 'file'> = {}): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.video(file, options));
|
||||
}
|
||||
|
||||
json(data: string | JsonObject): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.json(data));
|
||||
}
|
||||
|
||||
xml(data: string, options: Omit<XmlSegment['data'], 'data'> = {}): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.xml(data, options));
|
||||
}
|
||||
|
||||
poke(type: number | string, id?: number | string): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.poke(type, id));
|
||||
}
|
||||
|
||||
forward(id: string): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.forward(id));
|
||||
}
|
||||
|
||||
node(userId: number, nickname: string, content: OutgoingMessage): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.node(userId, nickname, content));
|
||||
}
|
||||
|
||||
share(options: ShareSegment['data']): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.share(options));
|
||||
}
|
||||
|
||||
music(options: MusicSegment['data']): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.music(options));
|
||||
}
|
||||
|
||||
location(options: LocationSegment['data']): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.location(options));
|
||||
}
|
||||
|
||||
contact(type: ContactSegment['data']['type'], id: number | string): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.contact(type, id));
|
||||
}
|
||||
|
||||
raw<TType extends string, TData extends JsonObject>(type: TType, data: TData): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.raw(type, data), type);
|
||||
}
|
||||
|
||||
append(messageInput: OutgoingMessage): MessageChain<THasReply> {
|
||||
let next: MessageChain<THasReply> = this;
|
||||
const normalized = normalizeMessage(messageInput);
|
||||
if (typeof normalized === 'string') {
|
||||
return next.text(normalized);
|
||||
}
|
||||
for (const segment of normalized) {
|
||||
next = next.addFromSegment(segment);
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
build(): AnyMessageSegment[] {
|
||||
return this.toSegments();
|
||||
}
|
||||
|
||||
toArray(): AnyMessageSegment[] {
|
||||
return this.toSegments();
|
||||
}
|
||||
|
||||
toSegments(): AnyMessageSegment[] {
|
||||
return [...this.items];
|
||||
}
|
||||
|
||||
toJSON(): AnyMessageSegment[] {
|
||||
return this.toSegments();
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return segmentsToCQString(this.items);
|
||||
}
|
||||
|
||||
sendToGroup(
|
||||
client: MessageChainSender,
|
||||
groupId: number,
|
||||
options?: RequestOptions & { autoEscape?: boolean },
|
||||
): Promise<unknown> {
|
||||
return client.sendGroupMessage(groupId, this, options);
|
||||
}
|
||||
|
||||
sendToPrivate(
|
||||
client: MessageChainSender,
|
||||
userId: number,
|
||||
options?: RequestOptions & { autoEscape?: boolean },
|
||||
): Promise<unknown> {
|
||||
return client.sendPrivateMessage(userId, this, options);
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<AnyMessageSegment> {
|
||||
return this.items[Symbol.iterator]();
|
||||
}
|
||||
|
||||
private addFromSegment(segment: AnyMessageSegment): MessageChain<THasReply> {
|
||||
return this.addSegment(segment, segment.type);
|
||||
}
|
||||
|
||||
private addSegment<TNextReply extends boolean = THasReply>(
|
||||
segment: AnyMessageSegment,
|
||||
singleUseType?: string,
|
||||
): MessageChain<TNextReply> {
|
||||
if (singleUseType && SINGLE_USE_TYPES.has(singleUseType) && this.used.has(singleUseType)) {
|
||||
throw new Error(`Message segment "${singleUseType}" can only appear once in a chain`);
|
||||
static empty(): MessageChain<false> {
|
||||
return new MessageChain<false>();
|
||||
}
|
||||
|
||||
const nextUsed = new Set(this.used);
|
||||
if (singleUseType && SINGLE_USE_TYPES.has(singleUseType)) {
|
||||
nextUsed.add(singleUseType);
|
||||
get length(): number {
|
||||
return this.items.length;
|
||||
}
|
||||
|
||||
get isEmpty(): boolean {
|
||||
return this.items.length === 0;
|
||||
}
|
||||
|
||||
text(text = ''): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.text(text));
|
||||
}
|
||||
|
||||
br(): MessageChain<THasReply> {
|
||||
return this.text('\n');
|
||||
}
|
||||
|
||||
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> {
|
||||
return this.addSegment(segments.at(qq, options));
|
||||
}
|
||||
|
||||
atAll(): MessageChain<THasReply> {
|
||||
return this.at('all');
|
||||
}
|
||||
|
||||
reply(this: MessageChain<false>, id: number | string): MessageChain<true> {
|
||||
return this.addSegment(segments.reply(id), 'reply') as MessageChain<true>;
|
||||
}
|
||||
|
||||
image(file: string, options: Omit<ImageSegment['data'], 'file'> = {}): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.image(file, options));
|
||||
}
|
||||
|
||||
record(
|
||||
file: string,
|
||||
options: Omit<RecordSegment['data'], 'file'> = {},
|
||||
): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.record(file, options));
|
||||
}
|
||||
|
||||
video(file: string, options: Omit<VideoSegment['data'], 'file'> = {}): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.video(file, options));
|
||||
}
|
||||
|
||||
json(data: string | JsonObject): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.json(data));
|
||||
}
|
||||
|
||||
xml(data: string, options: Omit<XmlSegment['data'], 'data'> = {}): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.xml(data, options));
|
||||
}
|
||||
|
||||
poke(type: number | string, id?: number | string): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.poke(type, id));
|
||||
}
|
||||
|
||||
forward(id: string): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.forward(id));
|
||||
}
|
||||
|
||||
node(userId: number, nickname: string, content: OutgoingMessage): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.node(userId, nickname, content));
|
||||
}
|
||||
|
||||
share(options: ShareSegment['data']): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.share(options));
|
||||
}
|
||||
|
||||
music(options: MusicSegment['data']): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.music(options));
|
||||
}
|
||||
|
||||
location(options: LocationSegment['data']): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.location(options));
|
||||
}
|
||||
|
||||
contact(type: ContactSegment['data']['type'], id: number | string): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.contact(type, id));
|
||||
}
|
||||
|
||||
raw<TType extends string, TData extends JsonObject>(
|
||||
type: TType,
|
||||
data: TData,
|
||||
): MessageChain<THasReply> {
|
||||
return this.addSegment(segments.raw(type, data), type);
|
||||
}
|
||||
|
||||
append(messageInput: OutgoingMessage): MessageChain<THasReply> {
|
||||
let next: MessageChain<THasReply> = this;
|
||||
const normalized = normalizeMessage(messageInput);
|
||||
|
||||
if (typeof normalized === 'string') {
|
||||
return next.text(normalized);
|
||||
}
|
||||
|
||||
for (const segment of normalized) {
|
||||
next = next.addFromSegment(segment);
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
build(): AnyMessageSegment[] {
|
||||
return this.toSegments();
|
||||
}
|
||||
|
||||
toArray(): AnyMessageSegment[] {
|
||||
return this.toSegments();
|
||||
}
|
||||
|
||||
toSegments(): AnyMessageSegment[] {
|
||||
return [...this.items];
|
||||
}
|
||||
|
||||
toJSON(): AnyMessageSegment[] {
|
||||
return this.toSegments();
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return segmentsToCQString(this.items);
|
||||
}
|
||||
|
||||
sendToGroup(
|
||||
client: MessageChainSender,
|
||||
groupId: number,
|
||||
options?: RequestOptions & { autoEscape?: boolean },
|
||||
): Promise<unknown> {
|
||||
return client.sendGroupMessage(groupId, this, options);
|
||||
}
|
||||
|
||||
sendToPrivate(
|
||||
client: MessageChainSender,
|
||||
userId: number,
|
||||
options?: RequestOptions & { autoEscape?: boolean },
|
||||
): Promise<unknown> {
|
||||
return client.sendPrivateMessage(userId, this, options);
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<AnyMessageSegment> {
|
||||
return this.items[Symbol.iterator]();
|
||||
}
|
||||
|
||||
private addFromSegment(segment: AnyMessageSegment): MessageChain<THasReply> {
|
||||
return this.addSegment(segment, segment.type);
|
||||
}
|
||||
|
||||
private addSegment<TNextReply extends boolean = THasReply>(
|
||||
segment: AnyMessageSegment,
|
||||
singleUseType?: string,
|
||||
): MessageChain<TNextReply> {
|
||||
if (singleUseType && SINGLE_USE_TYPES.has(singleUseType) && this.used.has(singleUseType)) {
|
||||
throw new Error(`Message segment "${singleUseType}" can only appear once in a chain`);
|
||||
}
|
||||
|
||||
const nextUsed = new Set(this.used);
|
||||
|
||||
if (singleUseType && SINGLE_USE_TYPES.has(singleUseType)) {
|
||||
nextUsed.add(singleUseType);
|
||||
}
|
||||
|
||||
return new MessageChain<TNextReply>([...this.items, segment], nextUsed);
|
||||
}
|
||||
return new MessageChain<TNextReply>([...this.items, segment], nextUsed);
|
||||
}
|
||||
}
|
||||
|
||||
export function chain(): MessageChain<false> {
|
||||
return MessageChain.empty();
|
||||
return MessageChain.empty();
|
||||
}
|
||||
|
||||
export function text(value = ''): MessageChain<false> {
|
||||
return chain().text(value);
|
||||
return chain().text(value);
|
||||
}
|
||||
|
||||
export function br(): MessageChain<false> {
|
||||
return chain().br();
|
||||
return chain().br();
|
||||
}
|
||||
|
||||
export function face(id: number | string, options: Omit<FaceSegment['data'], 'id'> = {}): MessageChain<false> {
|
||||
return chain().face(id, options);
|
||||
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> {
|
||||
return chain().at(qq, options);
|
||||
export function at(
|
||||
qq: number | 'all',
|
||||
options: { name?: string; uid?: string } = {},
|
||||
): MessageChain<false> {
|
||||
return chain().at(qq, options);
|
||||
}
|
||||
|
||||
export function atAll(): MessageChain<false> {
|
||||
return chain().atAll();
|
||||
return chain().atAll();
|
||||
}
|
||||
|
||||
export function reply(id: number | string): MessageChain<true> {
|
||||
return chain().reply(id);
|
||||
return chain().reply(id);
|
||||
}
|
||||
|
||||
export function image(file: string, options: Omit<ImageSegment['data'], 'file'> = {}): MessageChain<false> {
|
||||
return chain().image(file, options);
|
||||
export function image(
|
||||
file: string,
|
||||
options: Omit<ImageSegment['data'], 'file'> = {},
|
||||
): MessageChain<false> {
|
||||
return chain().image(file, options);
|
||||
}
|
||||
|
||||
export function record(file: string, options: Omit<RecordSegment['data'], 'file'> = {}): MessageChain<false> {
|
||||
return chain().record(file, options);
|
||||
export function record(
|
||||
file: string,
|
||||
options: Omit<RecordSegment['data'], 'file'> = {},
|
||||
): MessageChain<false> {
|
||||
return chain().record(file, options);
|
||||
}
|
||||
|
||||
export function video(file: string, options: Omit<VideoSegment['data'], 'file'> = {}): MessageChain<false> {
|
||||
return chain().video(file, options);
|
||||
export function video(
|
||||
file: string,
|
||||
options: Omit<VideoSegment['data'], 'file'> = {},
|
||||
): MessageChain<false> {
|
||||
return chain().video(file, options);
|
||||
}
|
||||
|
||||
export function json(data: string | JsonObject): MessageChain<false> {
|
||||
return chain().json(data);
|
||||
return chain().json(data);
|
||||
}
|
||||
|
||||
export function xml(data: string, options: Omit<XmlSegment['data'], 'data'> = {}): MessageChain<false> {
|
||||
return chain().xml(data, options);
|
||||
export function xml(
|
||||
data: string,
|
||||
options: Omit<XmlSegment['data'], 'data'> = {},
|
||||
): MessageChain<false> {
|
||||
return chain().xml(data, options);
|
||||
}
|
||||
|
||||
export function poke(type: number | string, id?: number | string): MessageChain<false> {
|
||||
return chain().poke(type, id);
|
||||
return chain().poke(type, id);
|
||||
}
|
||||
|
||||
export function forward(id: string): MessageChain<false> {
|
||||
return chain().forward(id);
|
||||
return chain().forward(id);
|
||||
}
|
||||
|
||||
export function node(userId: number, nickname: string, content: OutgoingMessage): MessageChain<false> {
|
||||
return chain().node(userId, nickname, content);
|
||||
export function node(
|
||||
userId: number,
|
||||
nickname: string,
|
||||
content: OutgoingMessage,
|
||||
): MessageChain<false> {
|
||||
return chain().node(userId, nickname, content);
|
||||
}
|
||||
|
||||
export function share(options: ShareSegment['data']): MessageChain<false> {
|
||||
return chain().share(options);
|
||||
return chain().share(options);
|
||||
}
|
||||
|
||||
export function music(options: MusicSegment['data']): MessageChain<false> {
|
||||
return chain().music(options);
|
||||
return chain().music(options);
|
||||
}
|
||||
|
||||
export function location(options: LocationSegment['data']): MessageChain<false> {
|
||||
return chain().location(options);
|
||||
return chain().location(options);
|
||||
}
|
||||
|
||||
export function contact(type: ContactSegment['data']['type'], id: number | string): MessageChain<false> {
|
||||
return chain().contact(type, id);
|
||||
export function contact(
|
||||
type: ContactSegment['data']['type'],
|
||||
id: number | string,
|
||||
): MessageChain<false> {
|
||||
return chain().contact(type, id);
|
||||
}
|
||||
|
||||
export function raw<TType extends string, TData extends JsonObject>(type: TType, data: TData): MessageChain<false> {
|
||||
return chain().raw(type, data);
|
||||
export function raw<TType extends string, TData extends JsonObject>(
|
||||
type: TType,
|
||||
data: TData,
|
||||
): MessageChain<false> {
|
||||
return chain().raw(type, data);
|
||||
}
|
||||
|
||||
export function normalizeMessage(input: OutgoingMessage): string | AnyMessageSegment[] {
|
||||
if (typeof input === 'string') return input;
|
||||
if (isMessageChainLike(input)) return input.toSegments();
|
||||
return Array.isArray(input) ? input : [input];
|
||||
if (typeof input === 'string') {
|
||||
return input;
|
||||
}
|
||||
|
||||
if (isMessageChainLike(input)) {
|
||||
return input.toSegments();
|
||||
}
|
||||
|
||||
return Array.isArray(input) ? input : [input];
|
||||
}
|
||||
|
||||
function isMessageChainLike(value: unknown): value is MessageChainLike {
|
||||
return typeof value === 'object'
|
||||
&& value !== null
|
||||
&& 'toSegments' in value
|
||||
&& typeof value.toSegments === 'function';
|
||||
return (
|
||||
typeof value === 'object' &&
|
||||
value !== null &&
|
||||
'toSegments' in value &&
|
||||
typeof value.toSegments === 'function'
|
||||
);
|
||||
}
|
||||
@@ -1,29 +1,26 @@
|
||||
import type { KnownMessageSegment, MessageSegment } from '../types/index';
|
||||
|
||||
export function escapeCqText(text: string): string {
|
||||
return text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/\[/g, '[')
|
||||
.replace(/\]/g, ']');
|
||||
return text.replace(/&/g, '&').replace(/\[/g, '[').replace(/\]/g, ']');
|
||||
}
|
||||
|
||||
export function escapeCqParam(text: string): string {
|
||||
return escapeCqText(text).replace(/,/g, ',');
|
||||
return escapeCqText(text).replace(/,/g, ',');
|
||||
}
|
||||
|
||||
export function segmentsToCQString(segments: Array<KnownMessageSegment | MessageSegment>): string {
|
||||
return segments.map(segmentToCq).join('');
|
||||
return segments.map(segmentToCq).join('');
|
||||
}
|
||||
|
||||
function segmentToCq(segment: KnownMessageSegment | MessageSegment): string {
|
||||
if (segment.type === 'text') {
|
||||
return escapeCqText(String(segment.data.text ?? ''));
|
||||
}
|
||||
if (segment.type === 'text') {
|
||||
return escapeCqText(String(segment.data.text ?? ''));
|
||||
}
|
||||
|
||||
const params = Object.entries(segment.data)
|
||||
.filter(([, value]) => value !== undefined && value !== null)
|
||||
.map(([key, value]) => `${key}=${escapeCqParam(String(value))}`)
|
||||
.join(',');
|
||||
const params = Object.entries(segment.data)
|
||||
.filter(([, value]) => value !== undefined && value !== null)
|
||||
.map(([key, value]) => `${key}=${escapeCqParam(String(value))}`)
|
||||
.join(',');
|
||||
|
||||
return params ? `[CQ:${segment.type},${params}]` : `[CQ:${segment.type}]`;
|
||||
return params ? `[CQ:${segment.type},${params}]` : `[CQ:${segment.type}]`;
|
||||
}
|
||||
+114
-78
@@ -6,108 +6,144 @@ import type { AnyMessageSegment, FaceSegment, OutgoingMessage } from '../types/i
|
||||
const CQ_REGEX = /\[CQ:([a-zA-Z0-9_.-]+)(?:,([^\]]*))?\]/g;
|
||||
|
||||
export function toCQString(input: OutgoingMessage): string {
|
||||
if (typeof input === 'string') return escapeCqText(input);
|
||||
const normalized = normalizeMessage(input);
|
||||
return typeof normalized === 'string'
|
||||
? escapeCqText(normalized)
|
||||
: segmentsToCQString(normalized);
|
||||
if (typeof input === 'string') {
|
||||
return escapeCqText(input);
|
||||
}
|
||||
|
||||
const normalized = normalizeMessage(input);
|
||||
return typeof normalized === 'string'
|
||||
? escapeCqText(normalized)
|
||||
: segmentsToCQString(normalized);
|
||||
}
|
||||
|
||||
export function fromCQString(input: string) {
|
||||
let current = chain();
|
||||
let lastIndex = 0;
|
||||
let current = chain();
|
||||
let lastIndex = 0;
|
||||
|
||||
for (const match of input.matchAll(CQ_REGEX)) {
|
||||
const index = match.index ?? 0;
|
||||
if (index > lastIndex) {
|
||||
const textValue = cqUnescape(input.slice(lastIndex, index));
|
||||
if (textValue) current = current.text(textValue);
|
||||
for (const match of input.matchAll(CQ_REGEX)) {
|
||||
const index = match.index ?? 0;
|
||||
|
||||
if (index > lastIndex) {
|
||||
const textValue = cqUnescape(input.slice(lastIndex, index));
|
||||
|
||||
if (textValue) {
|
||||
current = current.text(textValue);
|
||||
}
|
||||
}
|
||||
|
||||
const segment = segmentFromCq(match[1] ?? '', parseCQParams(match[2] ?? ''));
|
||||
|
||||
if (segment) {
|
||||
current = current.append(segment);
|
||||
}
|
||||
|
||||
lastIndex = index + match[0].length;
|
||||
}
|
||||
|
||||
const segment = segmentFromCq(match[1] ?? '', parseCQParams(match[2] ?? ''));
|
||||
if (segment) current = current.append(segment);
|
||||
lastIndex = index + match[0].length;
|
||||
}
|
||||
if (lastIndex < input.length) {
|
||||
const textValue = cqUnescape(input.slice(lastIndex));
|
||||
|
||||
if (lastIndex < input.length) {
|
||||
const textValue = cqUnescape(input.slice(lastIndex));
|
||||
if (textValue) current = current.text(textValue);
|
||||
}
|
||||
if (textValue) {
|
||||
current = current.text(textValue);
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
return current;
|
||||
}
|
||||
|
||||
export function parseSegments(input: string): AnyMessageSegment[] {
|
||||
return fromCQString(input).toSegments();
|
||||
return fromCQString(input).toSegments();
|
||||
}
|
||||
|
||||
function parseCQParams(raw: string): Record<string, string> {
|
||||
const params: Record<string, string> = {};
|
||||
if (!raw) return params;
|
||||
const params: Record<string, string> = {};
|
||||
|
||||
for (const pair of raw.split(',')) {
|
||||
const eq = pair.indexOf('=');
|
||||
if (eq <= 0) continue;
|
||||
params[pair.slice(0, eq)] = cqUnescapeParam(pair.slice(eq + 1));
|
||||
}
|
||||
if (!raw) {
|
||||
return params;
|
||||
}
|
||||
|
||||
return params;
|
||||
for (const pair of raw.split(',')) {
|
||||
const eq = pair.indexOf('=');
|
||||
|
||||
if (eq <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
params[pair.slice(0, eq)] = cqUnescapeParam(pair.slice(eq + 1));
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
function cqUnescape(text: string): string {
|
||||
return text
|
||||
.replace(/[/g, '[')
|
||||
.replace(/]/g, ']')
|
||||
.replace(/&/g, '&');
|
||||
return text.replace(/[/g, '[').replace(/]/g, ']').replace(/&/g, '&');
|
||||
}
|
||||
|
||||
function cqUnescapeParam(text: string): string {
|
||||
return cqUnescape(text).replace(/,/g, ',');
|
||||
return cqUnescape(text).replace(/,/g, ',');
|
||||
}
|
||||
|
||||
function segmentFromCq(type: string, data: Record<string, string>): AnyMessageSegment | null {
|
||||
switch (type) {
|
||||
case 'text':
|
||||
return segments.text(data.text ?? '');
|
||||
case 'face': {
|
||||
const options: Omit<FaceSegment['data'], 'id'> = {};
|
||||
if (data.resultId !== undefined) options.resultId = data.resultId;
|
||||
if (data.large !== undefined) {
|
||||
if (data.large === 'true' || data.large === '1') options.large = true;
|
||||
else if (data.large === 'false' || data.large === '0') options.large = false;
|
||||
else throw new Error('face.large must be true, false, 1, or 0');
|
||||
}
|
||||
return segments.face(data.id ?? '0', options);
|
||||
switch (type) {
|
||||
case 'text':
|
||||
return segments.text(data.text ?? '');
|
||||
|
||||
case 'face': {
|
||||
const options: Omit<FaceSegment['data'], 'id'> = {};
|
||||
|
||||
if (data.resultId !== undefined) {
|
||||
options.resultId = data.resultId;
|
||||
}
|
||||
|
||||
if (data.large !== undefined) {
|
||||
if (data.large === 'true' || data.large === '1') {
|
||||
options.large = true;
|
||||
} else if (data.large === 'false' || data.large === '0') {
|
||||
options.large = false;
|
||||
} else {
|
||||
throw new Error('face.large must be true, false, 1, or 0');
|
||||
}
|
||||
}
|
||||
|
||||
return segments.face(data.id ?? '0', options);
|
||||
}
|
||||
|
||||
case 'at':
|
||||
if (data.qq === 'all') {
|
||||
return segments.at('all');
|
||||
}
|
||||
|
||||
{
|
||||
const qq = Number(data.qq);
|
||||
return Number.isFinite(qq) && qq > 0
|
||||
? segments.at(qq, { name: data.name, uid: data.uid })
|
||||
: segments.raw(type, data);
|
||||
}
|
||||
|
||||
case 'reply':
|
||||
return segments.reply(data.id ?? '0');
|
||||
case 'image':
|
||||
return segments.image(data.file ?? data.url ?? '', {
|
||||
url: data.url,
|
||||
type: data.type === 'flash' ? 'flash' : undefined,
|
||||
summary: data.summary,
|
||||
});
|
||||
case 'record':
|
||||
return segments.record(data.file ?? data.url ?? '', { url: data.url });
|
||||
case 'video':
|
||||
return segments.video(data.file ?? data.url ?? '', {
|
||||
url: data.url,
|
||||
thumb: data.thumb,
|
||||
});
|
||||
case 'json':
|
||||
return segments.json(data.data ?? '');
|
||||
case 'xml':
|
||||
return segments.xml(data.data ?? '', { id: data.id });
|
||||
case 'poke':
|
||||
return segments.poke(data.type ?? data.id ?? '0', data.id);
|
||||
case 'forward':
|
||||
return segments.forward(data.id ?? data.res_id ?? data.forward_id ?? '');
|
||||
default:
|
||||
return segments.raw(type, data);
|
||||
}
|
||||
case 'at':
|
||||
if (data.qq === 'all') return segments.at('all');
|
||||
{
|
||||
const qq = Number(data.qq);
|
||||
return Number.isFinite(qq) && qq > 0
|
||||
? segments.at(qq, { name: data.name, uid: data.uid })
|
||||
: segments.raw(type, data);
|
||||
}
|
||||
case 'reply':
|
||||
return segments.reply(data.id ?? '0');
|
||||
case 'image':
|
||||
return segments.image(data.file ?? data.url ?? '', {
|
||||
url: data.url,
|
||||
type: data.type === 'flash' ? 'flash' : undefined,
|
||||
summary: data.summary,
|
||||
});
|
||||
case 'record':
|
||||
return segments.record(data.file ?? data.url ?? '', { url: data.url });
|
||||
case 'video':
|
||||
return segments.video(data.file ?? data.url ?? '', { url: data.url, thumb: data.thumb });
|
||||
case 'json':
|
||||
return segments.json(data.data ?? '');
|
||||
case 'xml':
|
||||
return segments.xml(data.data ?? '', { id: data.id });
|
||||
case 'poke':
|
||||
return segments.poke(data.type ?? data.id ?? '0', data.id);
|
||||
case 'forward':
|
||||
return segments.forward(data.id ?? data.res_id ?? data.forward_id ?? '');
|
||||
default:
|
||||
return segments.raw(type, data);
|
||||
}
|
||||
}
|
||||
@@ -2,32 +2,32 @@ import { segments } from './segments';
|
||||
|
||||
export { MessageChain } from './chain';
|
||||
export {
|
||||
at,
|
||||
atAll,
|
||||
br,
|
||||
chain,
|
||||
contact,
|
||||
face,
|
||||
forward,
|
||||
image,
|
||||
json,
|
||||
location,
|
||||
music,
|
||||
node,
|
||||
normalizeMessage,
|
||||
poke,
|
||||
raw,
|
||||
record,
|
||||
reply,
|
||||
share,
|
||||
text,
|
||||
video,
|
||||
xml,
|
||||
at,
|
||||
atAll,
|
||||
br,
|
||||
chain,
|
||||
contact,
|
||||
face,
|
||||
forward,
|
||||
image,
|
||||
json,
|
||||
location,
|
||||
music,
|
||||
node,
|
||||
normalizeMessage,
|
||||
poke,
|
||||
raw,
|
||||
record,
|
||||
reply,
|
||||
share,
|
||||
text,
|
||||
video,
|
||||
xml,
|
||||
} from './chain';
|
||||
export { escapeCqParam, escapeCqText } from './cq-format';
|
||||
export { fromCQString, parseSegments, toCQString } from './cq';
|
||||
export { segments };
|
||||
|
||||
export const message = {
|
||||
...segments,
|
||||
...segments,
|
||||
};
|
||||
@@ -1,100 +1,117 @@
|
||||
import type {
|
||||
AtSegment,
|
||||
ContactSegment,
|
||||
FaceSegment,
|
||||
ForwardSegment,
|
||||
ImageSegment,
|
||||
JsonObject,
|
||||
JsonSegment,
|
||||
LocationSegment,
|
||||
MessageSegment,
|
||||
MusicSegment,
|
||||
NodeSegment,
|
||||
OutgoingMessage,
|
||||
PokeSegment,
|
||||
RecordSegment,
|
||||
ReplySegment,
|
||||
ShareSegment,
|
||||
TextSegment,
|
||||
VideoSegment,
|
||||
XmlSegment,
|
||||
AtSegment,
|
||||
ContactSegment,
|
||||
FaceSegment,
|
||||
ForwardSegment,
|
||||
ImageSegment,
|
||||
JsonObject,
|
||||
JsonSegment,
|
||||
LocationSegment,
|
||||
MessageSegment,
|
||||
MusicSegment,
|
||||
NodeSegment,
|
||||
OutgoingMessage,
|
||||
PokeSegment,
|
||||
RecordSegment,
|
||||
ReplySegment,
|
||||
ShareSegment,
|
||||
TextSegment,
|
||||
VideoSegment,
|
||||
XmlSegment,
|
||||
} from '../types/index';
|
||||
|
||||
function seg<TType extends string, TData extends Record<string, unknown>>(type: TType, data: TData): MessageSegment<TType, TData> {
|
||||
return { type, data };
|
||||
function seg<TType extends string, TData extends Record<string, unknown>>(
|
||||
type: TType,
|
||||
data: TData,
|
||||
): MessageSegment<TType, TData> {
|
||||
return { type, data };
|
||||
}
|
||||
|
||||
export const segments = {
|
||||
text(text: string): TextSegment {
|
||||
return seg('text', { text });
|
||||
},
|
||||
text(text: string): TextSegment {
|
||||
return seg('text', { text });
|
||||
},
|
||||
|
||||
face(id: number | string, options: Omit<FaceSegment['data'], 'id'> = {}): FaceSegment {
|
||||
return seg('face', { id: String(id), ...options });
|
||||
},
|
||||
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 {
|
||||
const data: AtSegment['data'] = { qq: String(qq) };
|
||||
if (options.name) data.name = options.name;
|
||||
if (options.uid) data.uid = options.uid;
|
||||
return seg('at', data);
|
||||
},
|
||||
at(qq: number | 'all', options: { name?: string; uid?: string } = {}): AtSegment {
|
||||
const data: AtSegment['data'] = { qq: String(qq) };
|
||||
|
||||
reply(id: number | string): ReplySegment {
|
||||
return seg('reply', { id: String(id) });
|
||||
},
|
||||
if (options.name) {
|
||||
data.name = options.name;
|
||||
}
|
||||
|
||||
image(file: string, options: Omit<ImageSegment['data'], 'file'> = {}): ImageSegment {
|
||||
return seg('image', { file, ...options });
|
||||
},
|
||||
if (options.uid) {
|
||||
data.uid = options.uid;
|
||||
}
|
||||
|
||||
record(file: string, options: Omit<RecordSegment['data'], 'file'> = {}): RecordSegment {
|
||||
return seg('record', { file, ...options });
|
||||
},
|
||||
return seg('at', data);
|
||||
},
|
||||
|
||||
video(file: string, options: Omit<VideoSegment['data'], 'file'> = {}): VideoSegment {
|
||||
return seg('video', { file, ...options });
|
||||
},
|
||||
reply(id: number | string): ReplySegment {
|
||||
return seg('reply', { id: String(id) });
|
||||
},
|
||||
|
||||
json(data: string | JsonObject): JsonSegment {
|
||||
return seg('json', { data: typeof data === 'string' ? data : JSON.stringify(data) });
|
||||
},
|
||||
image(file: string, options: Omit<ImageSegment['data'], 'file'> = {}): ImageSegment {
|
||||
return seg('image', { file, ...options });
|
||||
},
|
||||
|
||||
xml(data: string, options: Omit<XmlSegment['data'], 'data'> = {}): XmlSegment {
|
||||
return seg('xml', { data, ...options });
|
||||
},
|
||||
record(file: string, options: Omit<RecordSegment['data'], 'file'> = {}): RecordSegment {
|
||||
return seg('record', { file, ...options });
|
||||
},
|
||||
|
||||
poke(type: number | string, id?: number | string): PokeSegment {
|
||||
const data: PokeSegment['data'] = { type };
|
||||
if (id !== undefined) data.id = id;
|
||||
return seg('poke', data);
|
||||
},
|
||||
video(file: string, options: Omit<VideoSegment['data'], 'file'> = {}): VideoSegment {
|
||||
return seg('video', { file, ...options });
|
||||
},
|
||||
|
||||
forward(id: string): ForwardSegment {
|
||||
return seg('forward', { id, res_id: id, forward_id: id });
|
||||
},
|
||||
json(data: string | JsonObject): JsonSegment {
|
||||
return seg('json', { data: typeof data === 'string' ? data : JSON.stringify(data) });
|
||||
},
|
||||
|
||||
node(userId: number, nickname: string, content: OutgoingMessage): NodeSegment {
|
||||
return seg('node', { user_id: userId, nickname, content });
|
||||
},
|
||||
xml(data: string, options: Omit<XmlSegment['data'], 'data'> = {}): XmlSegment {
|
||||
return seg('xml', { data, ...options });
|
||||
},
|
||||
|
||||
share(options: ShareSegment['data']): ShareSegment {
|
||||
return seg('share', options);
|
||||
},
|
||||
poke(type: number | string, id?: number | string): PokeSegment {
|
||||
const data: PokeSegment['data'] = { type };
|
||||
|
||||
music(options: MusicSegment['data']): MusicSegment {
|
||||
return seg('music', options);
|
||||
},
|
||||
if (id !== undefined) {
|
||||
data.id = id;
|
||||
}
|
||||
|
||||
location(options: LocationSegment['data']): LocationSegment {
|
||||
return seg('location', options);
|
||||
},
|
||||
return seg('poke', data);
|
||||
},
|
||||
|
||||
contact(type: ContactSegment['data']['type'], id: number | string): ContactSegment {
|
||||
return seg('contact', { type, id });
|
||||
},
|
||||
forward(id: string): ForwardSegment {
|
||||
return seg('forward', { id, res_id: id, forward_id: id });
|
||||
},
|
||||
|
||||
raw<TType extends string, TData extends JsonObject>(type: TType, data: TData): MessageSegment<TType, TData> {
|
||||
return seg(type, data);
|
||||
},
|
||||
node(userId: number, nickname: string, content: OutgoingMessage): NodeSegment {
|
||||
return seg('node', { user_id: userId, nickname, content });
|
||||
},
|
||||
|
||||
share(options: ShareSegment['data']): ShareSegment {
|
||||
return seg('share', options);
|
||||
},
|
||||
|
||||
music(options: MusicSegment['data']): MusicSegment {
|
||||
return seg('music', options);
|
||||
},
|
||||
|
||||
location(options: LocationSegment['data']): LocationSegment {
|
||||
return seg('location', options);
|
||||
},
|
||||
|
||||
contact(type: ContactSegment['data']['type'], id: number | string): ContactSegment {
|
||||
return seg('contact', { type, id });
|
||||
},
|
||||
|
||||
raw<TType extends string, TData extends JsonObject>(
|
||||
type: TType,
|
||||
data: TData,
|
||||
): MessageSegment<TType, TData> {
|
||||
return seg(type, data);
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user