mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-21 22:58:10 +00:00
feat(shell): logger: LogiriMessager
adapt fm
This commit is contained in:
parent
a52427bd27
commit
0df0a2f548
@ -2,72 +2,59 @@ import h from '@satorijs/element'
|
|||||||
import { link } from 'logiri'
|
import { link } from 'logiri'
|
||||||
import { grey } from '../../utils/colors'
|
import { grey } from '../../utils/colors'
|
||||||
|
|
||||||
export class LogiriMessager {
|
type DisplayComponent = string
|
||||||
private children: string[] = []
|
|
||||||
private results: string[] = []
|
|
||||||
|
|
||||||
|
export class LogiriMessager {
|
||||||
prepare = async () => {}
|
prepare = async () => {}
|
||||||
|
|
||||||
render = async (elements: h[], flush?: boolean) => {
|
render = async (elements: h[]): Promise<DisplayComponent[] | false> => {
|
||||||
for (const element of elements) await this.visit(element)
|
if (!elements.length) return ['空消息']
|
||||||
if (flush) await this.flush()
|
const result = await Promise.all(elements.map(this.visit))
|
||||||
|
if (result.every((x) => x === false)) return false
|
||||||
|
return result.flatMap((x) => (x === false ? ['[不支持的消息]'] : x))
|
||||||
}
|
}
|
||||||
|
|
||||||
send = async (content: string | null | undefined) => {
|
send = async (
|
||||||
if (!content) return []
|
content: string | null | undefined,
|
||||||
|
): Promise<DisplayComponent[]> => {
|
||||||
|
if (!content) return ['空消息']
|
||||||
await this.prepare()
|
await this.prepare()
|
||||||
const elements = h.normalize(content)
|
const elements = h.normalize(content)
|
||||||
await this.render(elements)
|
let result = await this.render(elements)
|
||||||
await this.flush()
|
if (result === false) result = ['[不支持的消息]']
|
||||||
return this.results.filter(Boolean)
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
flush = async () => {
|
visit = async (element: h): Promise<DisplayComponent[] | false> => {
|
||||||
if (!this.children.length) return
|
|
||||||
this.results.push(
|
|
||||||
this.children.join('').replace(/\r/g, '').replace(/\n/g, ' '),
|
|
||||||
)
|
|
||||||
this.children = []
|
|
||||||
}
|
|
||||||
|
|
||||||
visit = async (element: h) => {
|
|
||||||
const { type, attrs, children } = element
|
const { type, attrs, children } = element
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'text': {
|
case 'text': {
|
||||||
this.children.push(attrs['content'] as string)
|
return [attrs['content'] as string]
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'img': {
|
case 'img': {
|
||||||
this.children.push(link('[图片]', attrs['src'] as string))
|
return [link('[图片]', attrs['src'] as string)]
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'audio': {
|
case 'audio': {
|
||||||
this.children.push(link('[语音]', attrs['src'] as string))
|
return [link('[语音]', attrs['src'] as string)]
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'file': {
|
case 'file': {
|
||||||
this.children.push(link('[文件]', attrs['src'] as string))
|
return [link('[文件]', attrs['src'] as string)]
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'at': {
|
case 'at': {
|
||||||
if (attrs['type'] === 'all') this.children.push('@全体成员 ')
|
if (attrs['type'] === 'all') return ['@全体成员 ']
|
||||||
else
|
else return [`@${attrs['name'] as string}(${attrs['id'] as string}) `]
|
||||||
this.children.push(
|
|
||||||
`@${attrs['name'] as string}(${attrs['id'] as string}) `,
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'quote': {
|
case 'quote': {
|
||||||
const [author] = h.select(children, 'author')
|
const [author] = h.select(children, 'author')
|
||||||
const id = author?.attrs['id'] as string | undefined
|
const id = author?.attrs['id'] as string | undefined
|
||||||
|
|
||||||
this.children.push(
|
return [
|
||||||
grey(
|
grey(
|
||||||
id
|
id
|
||||||
? `${link(
|
? `${link(
|
||||||
@ -76,38 +63,33 @@ export class LogiriMessager {
|
|||||||
)} `
|
)} `
|
||||||
: `[回复] `,
|
: `[回复] `,
|
||||||
),
|
),
|
||||||
)
|
]
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'chronocat:poke': {
|
case 'chronocat:poke': {
|
||||||
this.children.push('[戳一戳]')
|
return ['[戳一戳]']
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'message': {
|
case 'message': {
|
||||||
// 前面的消息直接发送,开始一条新消息
|
|
||||||
await this.flush()
|
|
||||||
|
|
||||||
if ('forward' in attrs) {
|
if ('forward' in attrs) {
|
||||||
if ('id' in attrs) {
|
if ('id' in attrs) {
|
||||||
this.children.push('[单条转发消息]')
|
return ['[单条转发消息]']
|
||||||
} else if (children.every((x) => 'id' in x)) {
|
} else if (children.every((x) => 'id' in x)) {
|
||||||
this.children.push('[普通合并转发消息]')
|
return ['[普通合并转发消息]']
|
||||||
} else {
|
} else {
|
||||||
this.children.push('[伪造合并转发消息]')
|
return ['[伪造合并转发消息]']
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 普通切割消息
|
// 普通切割消息
|
||||||
await this.render(children, true)
|
const result = await this.render(children)
|
||||||
|
if (result) return ['[切割消息]', ...result]
|
||||||
|
else return ['[切割消息]']
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
// 兜底
|
// 兜底
|
||||||
await this.render(children)
|
return await this.render(children)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user