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,93 +4,91 @@ import { grey } from '../../utils/colors'
type DisplayComponent = string
export class LogiriMessager {
prepare = async () => {}
// const prepare = async () => {}
render = async (elements: h[]): Promise<DisplayComponent[] | false> => {
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<DisplayComponent[] | false> => {
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<DisplayComponent[]> => {
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<DisplayComponent[]> => {
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<DisplayComponent[] | false> => {
const { type, attrs, children } = element
const visit = async (element: h): Promise<DisplayComponent[] | false> => {
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)
}
}
}