mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-22 07:07:53 +00:00
refactor(shell): logger: functional messager
This commit is contained in:
parent
14729260de
commit
803692ffb1
@ -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) =>
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user