Files
SnowLuma/packages/onebot/src/reply-preview.ts
T
MliKiowa c7428363cb feat: add async SQLite on Node 24 and improve messaging diagnostics (#520)
Move database work off the main thread on Node.js 24 and improve message handling and runtime diagnostics.

Preserve migration, restart, and concurrency coverage with the async SQLite driver.
2026-10-13 10:12:47 +08:00

83 lines
2.3 KiB
TypeScript

import type { MessageElement } from '@snowluma/protocol/events';
import { MessageElementValidationError } from '@snowluma/protocol/element-manifest';
const labels: Readonly<Record<string, string>> = {
image: '[图片]',
record: '[语音]',
video: '[视频]',
file: '[文件]',
face: '[表情]',
mface: '[表情]',
forward: '[聊天记录]',
json: '[卡片]',
xml: '[卡片]',
markdown: '[Markdown]',
};
function previewLabel(type: string): string {
if (!Object.hasOwn(labels, type)) {
throw new MessageElementValidationError(
'UNSENDABLE_TYPE',
`message segment "${type}" is not supported in a reply preview`,
type,
);
}
return labels[type]!;
}
export function assertReplyPreviewType(type: string): void {
if (type !== 'text' && type !== 'at') {
previewLabel(type);
}
}
/** Quotes carry display text; media must not be uploaded again for a preview. */
export function elementsToReplyPreview(elements: readonly MessageElement[]): MessageElement[] {
const text = elements
.map((element) => {
if (element.type === 'text') {
return element.text;
}
if (element.type === 'at') {
return element.targetUin === 0 ? '@全体成员' : `@${element.targetUin}`;
}
return previewLabel(element.type);
})
.join('');
return [{ type: 'text', text }];
}
export function replyPreviewElements(message: unknown): MessageElement[] {
if (!Array.isArray(message)) {
return [];
}
const parts: string[] = [];
for (const segment of message) {
if (!segment || typeof segment !== 'object') {
continue;
}
const data = segment.data;
if (!data || typeof data !== 'object') {
continue;
}
if (segment.type === 'text' && typeof data.text === 'string') {
parts.push(data.text);
} else if (segment.type === 'at') {
parts.push(data.qq === 'all' ? '@全体成员' : `@${data.qq ?? ''}`);
} else if (typeof segment.type === 'string' && Object.hasOwn(labels, segment.type)) {
parts.push(labels[segment.type]!);
}
}
const text = parts.join('');
return text ? [{ type: 'text', text }] : [];
}