-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.go
369 lines (337 loc) · 8.61 KB
/
context.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package nano
import (
"encoding/base64"
"errors"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"sync"
base14 "github.com/fumiama/go-base16384"
"github.com/fumiama/imoto"
"github.com/sirupsen/logrus"
)
//go:generate go run codegen/context/main.go
type Ctx struct {
Event
State
Message *Message
IsToMe bool
IsQQ bool
caller *Bot
ma *Matcher
}
// decoder 反射获取的数据
type decoder []dec
type dec struct {
index int
key string
}
// decoder 缓存
var decoderCache = sync.Map{}
// Parse 将 Ctx.State 映射到结构体
func (ctx *Ctx) Parse(model interface{}) (err error) {
var (
rv = reflect.ValueOf(model).Elem()
t = rv.Type()
modelDec decoder
)
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("parse state error: %v", r)
}
}()
d, ok := decoderCache.Load(t)
if ok {
modelDec = d.(decoder)
} else {
modelDec = decoder{}
for i := 0; i < t.NumField(); i++ {
t1 := t.Field(i)
if key, ok := t1.Tag.Lookup("zero"); ok {
modelDec = append(modelDec, dec{
index: i,
key: key,
})
}
}
decoderCache.Store(t, modelDec)
}
for _, d := range modelDec { // decoder类型非小内存,无法被编译器优化为快速拷贝
rv.Field(d.index).Set(reflect.ValueOf(ctx.State[d.key]))
}
return nil
}
// CheckSession 判断会话连续性
func (ctx *Ctx) CheckSession() Rule {
msg := ctx.Value.(*Message)
return func(ctx2 *Ctx) bool {
msg2, ok := ctx.Value.(*Message)
if !ok || msg.Author == nil || msg2.Author == nil { // 确保无空
return false
}
return msg.Author.ID == msg2.Author.ID && msg.ChannelID == msg2.ChannelID
}
}
var imotoken = "f7f06a63b8c111df0d4faa256cd5ba35cb98678ee8274923576d0416b52fe768"
// Send 发送一批消息
func (ctx *Ctx) Send(messages Messages) (m []*Message, err error) {
isnextreply := false
textlist := []any{}
var reply *Message
for _, msg := range messages {
switch msg.Type {
case MessageSegmentTypeText:
textlist = append(textlist, msg.Data)
case MessageSegmentTypeImage:
reply, err = ctx.SendImage(msg.Data, isnextreply, textlist...)
if isnextreply {
isnextreply = false
}
textlist = textlist[:0]
m = append(m, reply)
if err != nil {
return
}
case MessageSegmentTypeImageBytes:
reply, err = ctx.SendImageBytes(StringToBytes(msg.Data), isnextreply, textlist...)
if isnextreply {
isnextreply = false
}
textlist = textlist[:0]
m = append(m, reply)
if err != nil {
return
}
case MessageSegmentTypeReply:
isnextreply = true
case MessageSegmentTypeAudio, MessageSegmentTypeVideo:
if !ctx.IsQQ {
continue
}
fp := &FilePost{
URL: msg.Data,
}
if msg.Type == MessageSegmentTypeAudio {
fp.Type = FileTypeAudio
} else if msg.Type == MessageSegmentTypeVideo {
fp.Type = FileTypeVideo
}
if OnlyQQGroup(ctx) {
reply, err = ctx.PostFileToQQGroup(ctx.Message.ChannelID, fp)
} else if OnlyQQPrivate(ctx) {
reply, err = ctx.PostFileToQQUser(ctx.Message.Author.ID, fp)
}
if err != nil {
return
}
logrus.Infoln(getLogHeader(), "=> 上传:", reply)
reply, err = ctx.Post(isnextreply, &MessagePost{
Content: " ",
Media: &MessageMedia{FileInfo: reply.FileInfo},
})
m = append(m, reply)
if err != nil {
return
}
}
}
if len(textlist) > 0 {
reply, err = ctx.SendPlainMessage(isnextreply, textlist...)
m = append(m, reply)
}
return
}
// SendChain 链式发送
func (ctx *Ctx) SendChain(message ...MessageSegment) (m []*Message, err error) {
return ctx.Send(message)
}
// Post 发送消息到对方
func (ctx *Ctx) Post(replytosender bool, post *MessagePost) (reply *Message, err error) {
msg := ctx.Message
if msg != nil {
post.ReplyMessageID = msg.ID
if OnlyGuild(ctx) && replytosender {
post.MessageReference = &MessageReference{
MessageID: msg.ID,
}
}
} else {
post.ReplyMessageID = "MESSAGE_CREATE"
}
if OnlyDirect(ctx) { // dms
reply, err = ctx.PostMessageToUser(msg.GuildID, post)
} else if OnlyChannel(ctx) {
reply, err = ctx.PostMessageToChannel(msg.ChannelID, post)
} else { // v2
switch {
case post.Markdown != nil:
post.Type = MessageTypeMarkdown
case post.Ark != nil:
post.Type = MessageTypeArk
case post.Embed != nil:
post.Type = MessageTypeEmbed
case post.Media != nil:
post.Type = MessageTypeMedia
default:
post.Type = MessageTypeText
}
post.Seq = len(GetTriggeredMessages(msg.ID)) + 1
if OnlyQQGroup(ctx) {
reply, err = ctx.PostMessageToQQGroup(msg.ChannelID, post)
} else if OnlyQQPrivate(ctx) {
reply, err = ctx.PostMessageToQQUser(msg.ChannelID, post)
}
if err == nil {
logtriggeredmessages(msg.ID, "") // only to log message seq
}
return
}
if err == nil && msg != nil && reply != nil && reply.ID != "" {
logtriggeredmessages(msg.ID, reply.ID)
}
return
}
// SendPlainMessage 发送纯文本消息到对方
func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (*Message, error) {
return ctx.Post(replytosender, &MessagePost{
Content: HideURL(fmt.Sprint(printable...)),
})
}
// SendImage 发送带图片消息到对方
func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (reply *Message, err error) {
post := &MessagePost{
Content: HideURL(fmt.Sprint(caption...)),
}
if OnlyQQ(ctx) {
if strings.HasPrefix(file, "file:///") {
data, err := os.ReadFile(file[8:])
if err != nil {
return nil, err
}
return ctx.SendImageBytes(data, replytosender, caption...)
}
if strings.HasPrefix(file, "base64://") {
data, err := base64.StdEncoding.DecodeString(file[9:])
if err != nil {
return nil, err
}
return ctx.SendImageBytes(data, replytosender, caption...)
}
if strings.HasPrefix(file, "base16384://") {
data := base14.DecodeFromString(file[12:])
if len(data) == 0 {
return nil, errors.New("invalid base16384 image")
}
return ctx.SendImageBytes(data, replytosender, caption...)
}
fp := &FilePost{
Type: FileTypeImage,
URL: file,
}
/*if len(caption) > 0 {
_, _ = ctx.SendPlainMessage(replytosender, caption...)
}*/
if post.Content == "" {
post.Content = " "
}
if OnlyQQGroup(ctx) {
reply, err = ctx.PostFileToQQGroup(ctx.Message.ChannelID, fp)
} else if OnlyQQPrivate(ctx) {
reply, err = ctx.PostFileToQQUser(ctx.Message.Author.ID, fp)
}
if err != nil {
return
}
logrus.Infoln(getLogHeader(), "=> 上传:", reply)
post.Media = &MessageMedia{FileInfo: reply.FileInfo}
} else {
if strings.HasPrefix(file, "http") {
post.Image = file
} else {
post.ImageFile = file
}
}
return ctx.Post(replytosender, post)
}
// SendImageBytes 发送带图片消息到对方
func (ctx *Ctx) SendImageBytes(data []byte, replytosender bool, caption ...any) (*Message, error) {
if OnlyQQ(ctx) {
file, _, _, err := imoto.Bed(imotoken, data)
if err != nil {
return nil, err
}
return ctx.SendImage(file, replytosender, caption...)
}
post := &MessagePost{
Content: HideURL(fmt.Sprint(caption...)),
}
post.ImageBytes = data
return ctx.Post(replytosender, post)
}
// Echo 向自身分发虚拟事件
func (ctx *Ctx) Echo(payload *WebsocketPayload) {
ctx.caller.processEvent(payload)
}
// FutureEvent ...
func (ctx *Ctx) FutureEvent(Type string, rule ...Rule) *FutureEvent {
return ctx.ma.FutureEvent(Type, rule...)
}
// Get 从 promt 获得回复
func (ctx *Ctx) Get(prompt string) string {
if prompt != "" {
_, _ = ctx.SendPlainMessage(false, prompt)
}
return (<-ctx.FutureEvent("Message", ctx.CheckSession()).Next()).Event.Value.(*Message).Content
}
// ExtractPlainText 提取消息中的纯文本
func (ctx *Ctx) ExtractPlainText() string {
if ctx == nil || ctx.Value == nil {
return ""
}
if msg, ok := ctx.Value.(*Message); ok {
return msg.Content
}
return ""
}
// MessageString 字符串消息便于Regex
func (ctx *Ctx) MessageString() string {
return ctx.ExtractPlainText()
}
// Block 匹配成功后阻止后续触发
func (ctx *Ctx) Block() {
ctx.ma.SetBlock(true)
}
// Block 在 pre, rules, mid 阶段阻止后续触发
func (ctx *Ctx) Break() {
ctx.ma.Break = true
}
// GroupID 唯一的发送者所属组 ID
func (ctx *Ctx) GroupID() uint64 {
grp := uint64(0)
if ctx.IsQQ {
if OnlyQQGroup(ctx) {
grp = DigestID(ctx.Message.ChannelID)
} else if OnlyQQPrivate(ctx) {
grp = DigestID(ctx.Message.Author.ID)
} else {
return 0
}
} else {
var err error
grp, err = strconv.ParseUint(ctx.Message.ChannelID, 10, 64)
if err != nil {
return 0
}
}
return grp
}
// GroupID 唯一的发送者 ID
func (ctx *Ctx) UserID() uint64 {
if ctx.IsQQ {
return DigestID(ctx.Message.Author.ID)
}
grp, _ := strconv.ParseUint(ctx.Message.Author.ID, 10, 64)
return grp
}