/
writer.go
270 lines (241 loc) · 5.97 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package mmsg
import (
"fmt"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Sora233/DDBOT/requests"
"github.com/samber/lo"
"strings"
"unicode"
)
// MSG 线程不安全
type MSG struct {
elements []message.IMessageElement
textBuf strings.Builder
}
func NewMSG() *MSG {
return &MSG{}
}
func NewMSGFromGroupMessage(gm *message.GroupMessage) *MSG {
return &MSG{
elements: gm.Elements,
}
}
func NewMSGFromPrivateMessage(pm *message.PrivateMessage) *MSG {
return &MSG{
elements: pm.Elements,
}
}
func NewText(s string) *MSG {
msg := NewMSG()
msg.Text(s)
return msg
}
func NewTextf(format string, args ...interface{}) *MSG {
msg := NewMSG()
msg.Textf(format, args...)
return msg
}
// Drop predicate返回true的元素被去掉
func (m *MSG) Drop(predicate func(e message.IMessageElement, index int) bool) *MSG {
m.flushText()
m.elements = lo.Filter(m.elements, func(e message.IMessageElement, index int) bool {
return !predicate(e, index)
})
return m
}
func (m *MSG) Clone() *MSG {
m.flushText()
return &MSG{
elements: m.elements[:],
}
}
func (m *MSG) Clear() *MSG {
m.flushText()
m.elements = nil
return m
}
func (m *MSG) Append(elems ...message.IMessageElement) *MSG {
if len(elems) == 0 {
return m
}
for _, e := range elems {
if e == nil {
continue
}
if textE, ok := e.(*message.TextElement); ok {
m.textBuf.WriteString(textE.Content)
continue
}
m.flushText()
m.elements = append(m.elements, e)
}
return m
}
func (m *MSG) flushText() {
if m.textBuf.Len() > 0 {
m.elements = append(m.elements, message.NewText(m.textBuf.String()))
m.textBuf.Reset()
}
}
func (m *MSG) Text(s string) *MSG {
m.textBuf.WriteString(s)
return m
}
func (m *MSG) Textf(format string, args ...interface{}) *MSG {
m.textBuf.WriteString(fmt.Sprintf(format, args...))
return m
}
func (m *MSG) Image(buf []byte, alternative string) *MSG {
img := NewImage(buf)
if len(alternative) > 0 {
img.Alternative(alternative)
}
return m.Append(img)
}
func (m *MSG) ImageWithNorm(buf []byte, alternative string) *MSG {
img := NewImage(buf).Norm()
if len(alternative) > 0 {
img.Alternative(alternative)
}
return m.Append(img)
}
func (m *MSG) ImageWithResize(buf []byte, alternative string, width, height uint) *MSG {
img := NewImage(buf).Resize(width, height)
if len(alternative) > 0 {
img.Alternative(alternative)
}
return m.Append(img)
}
func (m *MSG) ImageByUrl(url string, alternative string, opts ...requests.Option) *MSG {
img := NewImageByUrl(url, opts...)
if len(alternative) > 0 {
img.Alternative(alternative)
}
return m.Append(img)
}
func (m *MSG) ImageByUrlWithNorm(url string, alternative string, opts ...requests.Option) *MSG {
img := NewImageByUrl(url, opts...).Norm()
if len(alternative) > 0 {
img.Alternative(alternative)
}
return m.Append(img)
}
func (m *MSG) ImageByUrlWithResize(url string, alternative string, width, height uint, opts ...requests.Option) *MSG {
img := NewImageByUrl(url, opts...).Resize(width, height)
if len(alternative) > 0 {
img.Alternative(alternative)
}
return m.Append(img)
}
func (m *MSG) ImageByLocal(filepath, alternative string) *MSG {
img := NewImageByLocal(filepath)
if len(alternative) > 0 {
img.Alternative(alternative)
}
return m.Append(img)
}
func (m *MSG) ImageByLocalWithNorm(filepath, alternative string) *MSG {
img := NewImageByLocal(filepath).Norm()
if len(alternative) > 0 {
img.Alternative(alternative)
}
return m.Append(img)
}
func (m *MSG) At(target int64) *MSG {
return m.Append(NewAt(target))
}
// AtAll 添加@全体成员,如果prepend设置为true,则会添加在消息最前面
func (m *MSG) AtAll(prepend ...bool) *MSG {
if len(prepend) > 0 && prepend[0] {
m.elements = append([]message.IMessageElement{NewAt(0)}, m.elements...)
return m
}
return m.Append(NewAt(0))
}
// Poke 戳一戳,只支持群聊,如果MSG发送给私聊,将自动忽略
func (m *MSG) Poke(target int64) *MSG {
return m.Append(NewPoke(target))
}
func (m *MSG) ImageByLocalWithResize(filepath, alternative string, width, height uint) *MSG {
img := NewImageByLocal(filepath).Resize(width, height)
if len(alternative) > 0 {
img.Alternative(alternative)
}
return m.Append(img)
}
// ToCombineMessage 总是返回 non-nil
func (m *MSG) ToCombineMessage(target Target) *message.SendingMessage {
var result = message.NewSendingMessage()
sms := m.ToMessage(target)
for _, sm := range sms {
for _, e := range sm.Elements {
result.Append(e)
}
}
return result
}
// ToMessage 返回消息用于发送
func (m *MSG) ToMessage(target Target) []*message.SendingMessage {
if m == nil {
return nil
}
var result []*message.SendingMessage
m.Cut()
var sending = message.NewSendingMessage()
for _, e := range m.elements {
if custom, ok := e.(CustomElement); ok {
if e.Type() == Cut {
if len(sending.Elements) > 0 {
result = append(result, sending)
sending = message.NewSendingMessage()
}
} else {
packed := custom.PackToElement(target)
if packed != nil {
sending.Append(packed)
}
}
continue
}
sending.Append(e)
}
if len(sending.Elements) > 0 {
result = append(result, sending)
}
cleanText := func(m *message.SendingMessage) {
var lastText *message.TextElement
lastIdx := -1
for idx, e := range m.Elements {
if t, ok := e.(*message.TextElement); ok {
lastText = t
lastIdx = idx
}
}
if lastText != nil {
lastText.Content = strings.TrimRightFunc(lastText.Content, unicode.IsSpace)
if lastText.Content == "" {
m.Elements = lo.Filter(m.Elements, func(_ message.IMessageElement, index int) bool {
return index != lastIdx
})
}
}
}
if len(result) > 0 {
cleanText(result[len(result)-1])
}
result = lo.Filter(result, func(item *message.SendingMessage, _ int) bool {
return len(item.Elements) > 0
})
return result
}
func (m *MSG) Cut() *MSG {
m.flushText()
if len(m.elements) > 0 {
m.elements = append(m.elements, new(CutElement))
}
return m
}
func (m *MSG) Elements() []message.IMessageElement {
m.flushText()
return m.elements
}