From 803692ffb1de25ee9a7553481ee5285cdacda96e Mon Sep 17 00:00:00 2001 From: Il Harper Date: Fri, 23 Aug 2024 14:20:14 +0800 Subject: [PATCH] refactor(shell): logger: functional messager --- packages/shell/src/services/logger/logiri.ts | 4 +- .../shell/src/services/logger/messager.ts | 140 +++++++++--------- 2 files changed, 71 insertions(+), 73 deletions(-) diff --git a/packages/shell/src/services/logger/logiri.ts b/packages/shell/src/services/logger/logiri.ts index 95f4803..f349b04 100644 --- a/packages/shell/src/services/logger/logiri.ts +++ b/packages/shell/src/services/logger/logiri.ts @@ -1,12 +1,12 @@ import { link } from 'logiri' -import { LogiriMessager } from './messager' import type { Event } from '../../satori/types' import { blue, cyan, grey } from '../../utils/colors' +import { send } from './messager' export const logiriMessageCreated = async (data: object) => { const d = data as Event if (d.type !== 'message-created') return - const rawMessage = await new LogiriMessager().send(d.message?.content) + const rawMessage = await send(d.message?.content) const message = rawMessage.join('').replace(/\r/g, '').replace(/\n/g, ' ') return [message].map( (x) => diff --git a/packages/shell/src/services/logger/messager.ts b/packages/shell/src/services/logger/messager.ts index f569a87..7e460ec 100644 --- a/packages/shell/src/services/logger/messager.ts +++ b/packages/shell/src/services/logger/messager.ts @@ -4,93 +4,91 @@ import { grey } from '../../utils/colors' type DisplayComponent = string -export class LogiriMessager { - prepare = async () => {} +// const prepare = async () => {} - render = async (elements: h[]): Promise => { - if (!elements.length) return ['空消息'] - const result = await Promise.all(elements.map(this.visit)) - if (result.every((x) => x === false)) return false - return result.flatMap((x) => (x === false ? ['[不支持的消息]'] : x)) - } +const render = async (elements: h[]): Promise => { + if (!elements.length) return ['空消息'] + const result = await Promise.all(elements.map(visit)) + if (result.every((x) => x === false)) return false + return result.flatMap((x) => (x === false ? ['[不支持的消息]'] : x)) +} - send = async ( - content: string | null | undefined, - ): Promise => { - if (!content) return ['空消息'] - await this.prepare() - const elements = h.normalize(content) - let result = await this.render(elements) - if (result === false) result = ['[不支持的消息]'] - return result - } +export const send = async ( + content: string | null | undefined, +): Promise => { + if (!content) return ['空消息'] + // await prepare() + const elements = h.normalize(content) + let result = await render(elements) + if (result === false) result = ['[不支持的消息]'] + return result +} - visit = async (element: h): Promise => { - const { type, attrs, children } = element +const visit = async (element: h): Promise => { + const { type, attrs, children } = element - switch (type) { - case 'text': { - return [attrs['content'] as string] - } + switch (type) { + case 'text': { + return [attrs['content'] as string] + } - case 'img': { - return [link('[图片]', attrs['src'] as string)] - } + case 'img': { + return [link('[图片]', attrs['src'] as string)] + } - case 'audio': { - return [link('[语音]', attrs['src'] as string)] - } + case 'audio': { + return [link('[语音]', attrs['src'] as string)] + } - case 'file': { - return [link('[文件]', attrs['src'] as string)] - } + case 'file': { + return [link('[文件]', attrs['src'] as string)] + } - case 'at': { - if (attrs['type'] === 'all') return ['@全体成员 '] - else return [`@${attrs['name'] as string}(${attrs['id'] as string}) `] - } + case 'at': { + if (attrs['type'] === 'all') return ['@全体成员 '] + else return [`@${attrs['name'] as string}(${attrs['id'] as string}) `] + } - case 'quote': { - const [author] = h.select(children, 'author') - const id = author?.attrs['id'] as string | undefined + case 'quote': { + const [author] = h.select(children, 'author') + const id = author?.attrs['id'] as string | undefined - return [ - grey( - id - ? `${link( - `[回复${id}]`, - `http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${id}&spec=640`, - )} ` - : `[回复] `, - ), - ] - } + return [ + grey( + id + ? `${link( + `[回复${id}]`, + `http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${id}&spec=640`, + )} ` + : `[回复] `, + ), + ] + } - case 'chronocat:poke': { - return ['[戳一戳]'] - } + case 'chronocat:poke': { + return ['[戳一戳]'] + } - case 'message': { - if ('forward' in attrs) { - if ('id' in attrs) { - return ['[单条转发消息]'] - } else if (children.every((x) => 'id' in x)) { - return ['[普通合并转发消息]'] - } else { - return ['[伪造合并转发消息]'] - } + case 'message': { + if ('forward' in attrs) { + if ('id' in attrs) { + return ['[单条转发消息]'] + } else if (children.every((x) => 'id' in x)) { + return ['[普通合并转发消息]'] } else { - // 普通切割消息 - const result = await this.render(children) - if (result) return ['[切割消息]', ...result] - else return ['[切割消息]'] + return ['[伪造合并转发消息]'] } + } else { + // 普通切割消息 + const result = await render(children) + if (result) return ['[切割消息]', ...result] + else return ['[切割消息]'] } + } - default: { - // 兜底 - return await this.render(children) - } + default: { + // 兜底 + return await render(children) } } }