refactor(shell): logger: functional messager

This commit is contained in:
Il Harper 2024-08-23 14:20:14 +08:00
parent 14729260de
commit 803692ffb1
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 71 additions and 73 deletions

View File

@ -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) =>

View File

@ -4,28 +4,27 @@ import { grey } from '../../utils/colors'
type DisplayComponent = string
export class LogiriMessager {
prepare = async () => {}
// const prepare = async () => {}
render = async (elements: h[]): Promise<DisplayComponent[] | false> => {
const render = async (elements: h[]): Promise<DisplayComponent[] | false> => {
if (!elements.length) return ['空消息']
const result = await Promise.all(elements.map(this.visit))
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 (
export const send = async (
content: string | null | undefined,
): Promise<DisplayComponent[]> => {
): Promise<DisplayComponent[]> => {
if (!content) return ['空消息']
await this.prepare()
// await prepare()
const elements = h.normalize(content)
let result = await this.render(elements)
let result = await render(elements)
if (result === false) result = ['[不支持的消息]']
return result
}
}
visit = async (element: h): Promise<DisplayComponent[] | false> => {
const visit = async (element: h): Promise<DisplayComponent[] | false> => {
const { type, attrs, children } = element
switch (type) {
@ -81,7 +80,7 @@ export class LogiriMessager {
}
} else {
// 普通切割消息
const result = await this.render(children)
const result = await render(children)
if (result) return ['[切割消息]', ...result]
else return ['[切割消息]']
}
@ -89,8 +88,7 @@ export class LogiriMessager {
default: {
// 兜底
return await this.render(children)
}
return await render(children)
}
}
}