Skip to content

Commit 0df0a2f

Browse files
committed
feat(shell): logger: LogiriMessager adapt fm
1 parent a52427b commit 0df0a2f

File tree

1 file changed

+31
-49
lines changed

1 file changed

+31
-49
lines changed

packages/shell/src/services/logger/messager.ts

+31-49
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,59 @@ import h from '@satorijs/element'
22
import { link } from 'logiri'
33
import { grey } from '../../utils/colors'
44

5-
export class LogiriMessager {
6-
private children: string[] = []
7-
private results: string[] = []
5+
type DisplayComponent = string
86

7+
export class LogiriMessager {
98
prepare = async () => {}
109

11-
render = async (elements: h[], flush?: boolean) => {
12-
for (const element of elements) await this.visit(element)
13-
if (flush) await this.flush()
10+
render = async (elements: h[]): Promise<DisplayComponent[] | false> => {
11+
if (!elements.length) return ['空消息']
12+
const result = await Promise.all(elements.map(this.visit))
13+
if (result.every((x) => x === false)) return false
14+
return result.flatMap((x) => (x === false ? ['[不支持的消息]'] : x))
1415
}
1516

16-
send = async (content: string | null | undefined) => {
17-
if (!content) return []
17+
send = async (
18+
content: string | null | undefined,
19+
): Promise<DisplayComponent[]> => {
20+
if (!content) return ['空消息']
1821
await this.prepare()
1922
const elements = h.normalize(content)
20-
await this.render(elements)
21-
await this.flush()
22-
return this.results.filter(Boolean)
23+
let result = await this.render(elements)
24+
if (result === false) result = ['[不支持的消息]']
25+
return result
2326
}
2427

25-
flush = async () => {
26-
if (!this.children.length) return
27-
this.results.push(
28-
this.children.join('').replace(/\r/g, '').replace(/\n/g, ' '),
29-
)
30-
this.children = []
31-
}
32-
33-
visit = async (element: h) => {
28+
visit = async (element: h): Promise<DisplayComponent[] | false> => {
3429
const { type, attrs, children } = element
3530

3631
switch (type) {
3732
case 'text': {
38-
this.children.push(attrs['content'] as string)
39-
return
33+
return [attrs['content'] as string]
4034
}
4135

4236
case 'img': {
43-
this.children.push(link('[图片]', attrs['src'] as string))
44-
return
37+
return [link('[图片]', attrs['src'] as string)]
4538
}
4639

4740
case 'audio': {
48-
this.children.push(link('[语音]', attrs['src'] as string))
49-
return
41+
return [link('[语音]', attrs['src'] as string)]
5042
}
5143

5244
case 'file': {
53-
this.children.push(link('[文件]', attrs['src'] as string))
54-
return
45+
return [link('[文件]', attrs['src'] as string)]
5546
}
5647

5748
case 'at': {
58-
if (attrs['type'] === 'all') this.children.push('@全体成员 ')
59-
else
60-
this.children.push(
61-
`@${attrs['name'] as string}(${attrs['id'] as string}) `,
62-
)
63-
return
49+
if (attrs['type'] === 'all') return ['@全体成员 ']
50+
else return [`@${attrs['name'] as string}(${attrs['id'] as string}) `]
6451
}
6552

6653
case 'quote': {
6754
const [author] = h.select(children, 'author')
6855
const id = author?.attrs['id'] as string | undefined
6956

70-
this.children.push(
57+
return [
7158
grey(
7259
id
7360
? `${link(
@@ -76,38 +63,33 @@ export class LogiriMessager {
7663
)} `
7764
: `[回复] `,
7865
),
79-
)
80-
return
66+
]
8167
}
8268

8369
case 'chronocat:poke': {
84-
this.children.push('[戳一戳]')
85-
return
70+
return ['[戳一戳]']
8671
}
8772

8873
case 'message': {
89-
// 前面的消息直接发送,开始一条新消息
90-
await this.flush()
91-
9274
if ('forward' in attrs) {
9375
if ('id' in attrs) {
94-
this.children.push('[单条转发消息]')
76+
return ['[单条转发消息]']
9577
} else if (children.every((x) => 'id' in x)) {
96-
this.children.push('[普通合并转发消息]')
78+
return ['[普通合并转发消息]']
9779
} else {
98-
this.children.push('[伪造合并转发消息]')
80+
return ['[伪造合并转发消息]']
9981
}
10082
} else {
10183
// 普通切割消息
102-
await this.render(children, true)
84+
const result = await this.render(children)
85+
if (result) return ['[切割消息]', ...result]
86+
else return ['[切割消息]']
10387
}
104-
return
10588
}
10689

10790
default: {
10891
// 兜底
109-
await this.render(children)
110-
return
92+
return await this.render(children)
11193
}
11294
}
11395
}

0 commit comments

Comments
 (0)