@@ -2,72 +2,59 @@ import h from '@satorijs/element'
2
2
import { link } from 'logiri'
3
3
import { grey } from '../../utils/colors'
4
4
5
- export class LogiriMessager {
6
- private children : string [ ] = [ ]
7
- private results : string [ ] = [ ]
5
+ type DisplayComponent = string
8
6
7
+ export class LogiriMessager {
9
8
prepare = async ( ) => { }
10
9
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 ) )
14
15
}
15
16
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 [ '空消息' ]
18
21
await this . prepare ( )
19
22
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
23
26
}
24
27
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 > => {
34
29
const { type, attrs, children } = element
35
30
36
31
switch ( type ) {
37
32
case 'text' : {
38
- this . children . push ( attrs [ 'content' ] as string )
39
- return
33
+ return [ attrs [ 'content' ] as string ]
40
34
}
41
35
42
36
case 'img' : {
43
- this . children . push ( link ( '[图片]' , attrs [ 'src' ] as string ) )
44
- return
37
+ return [ link ( '[图片]' , attrs [ 'src' ] as string ) ]
45
38
}
46
39
47
40
case 'audio' : {
48
- this . children . push ( link ( '[语音]' , attrs [ 'src' ] as string ) )
49
- return
41
+ return [ link ( '[语音]' , attrs [ 'src' ] as string ) ]
50
42
}
51
43
52
44
case 'file' : {
53
- this . children . push ( link ( '[文件]' , attrs [ 'src' ] as string ) )
54
- return
45
+ return [ link ( '[文件]' , attrs [ 'src' ] as string ) ]
55
46
}
56
47
57
48
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 } ) ` ]
64
51
}
65
52
66
53
case 'quote' : {
67
54
const [ author ] = h . select ( children , 'author' )
68
55
const id = author ?. attrs [ 'id' ] as string | undefined
69
56
70
- this . children . push (
57
+ return [
71
58
grey (
72
59
id
73
60
? `${ link (
@@ -76,38 +63,33 @@ export class LogiriMessager {
76
63
) } `
77
64
: `[回复] ` ,
78
65
) ,
79
- )
80
- return
66
+ ]
81
67
}
82
68
83
69
case 'chronocat:poke' : {
84
- this . children . push ( '[戳一戳]' )
85
- return
70
+ return [ '[戳一戳]' ]
86
71
}
87
72
88
73
case 'message' : {
89
- // 前面的消息直接发送,开始一条新消息
90
- await this . flush ( )
91
-
92
74
if ( 'forward' in attrs ) {
93
75
if ( 'id' in attrs ) {
94
- this . children . push ( '[单条转发消息]' )
76
+ return [ '[单条转发消息]' ]
95
77
} else if ( children . every ( ( x ) => 'id' in x ) ) {
96
- this . children . push ( '[普通合并转发消息]' )
78
+ return [ '[普通合并转发消息]' ]
97
79
} else {
98
- this . children . push ( '[伪造合并转发消息]' )
80
+ return [ '[伪造合并转发消息]' ]
99
81
}
100
82
} else {
101
83
// 普通切割消息
102
- await this . render ( children , true )
84
+ const result = await this . render ( children )
85
+ if ( result ) return [ '[切割消息]' , ...result ]
86
+ else return [ '[切割消息]' ]
103
87
}
104
- return
105
88
}
106
89
107
90
default : {
108
91
// 兜底
109
- await this . render ( children )
110
- return
92
+ return await this . render ( children )
111
93
}
112
94
}
113
95
}
0 commit comments