-
Notifications
You must be signed in to change notification settings - Fork 65
/
context.go
132 lines (115 loc) · 2.83 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
package zero
import (
"fmt"
"reflect"
"sync"
"github.com/wdvxdr1123/ZeroBot/message"
)
// Ctx represents the Context which hold the event.
// 代表上下文
type Ctx struct {
ma *Matcher
Event *Event
State State
caller APICaller
// lazy message
once sync.Once
message string
}
// GetMatcher ...
func (ctx *Ctx) GetMatcher() *Matcher {
return ctx.ma
}
// 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 {
return func(ctx2 *Ctx) bool {
return ctx.Event.UserID == ctx2.Event.UserID &&
ctx.Event.GroupID == ctx2.Event.GroupID // 私聊时GroupID为0,也相等
}
}
// Send 快捷发送消息
func (ctx *Ctx) Send(message interface{}) int64 {
if ctx.Event.GroupID != 0 {
return ctx.SendGroupMessage(ctx.Event.GroupID, message)
}
return ctx.SendPrivateMessage(ctx.Event.UserID, message)
}
// SendChain 快捷发送消息-消息链
func (ctx *Ctx) SendChain(message ...message.MessageSegment) int64 {
if ctx.Event.GroupID != 0 {
return ctx.SendGroupMessage(ctx.Event.GroupID, message)
}
return ctx.SendPrivateMessage(ctx.Event.UserID, message)
}
// FutureEvent ...
func (ctx *Ctx) FutureEvent(Type string, rule ...Rule) *FutureEvent {
return ctx.ma.FutureEvent(Type, rule...)
}
// Get ..
func (ctx *Ctx) Get(prompt string) string {
if prompt != "" {
ctx.Send(prompt)
}
return (<-ctx.FutureEvent("message", ctx.CheckSession()).Next()).RawMessage
}
// ExtractPlainText 提取消息中的纯文本
func (ctx *Ctx) ExtractPlainText() string {
if ctx == nil || ctx.Event == nil || ctx.Event.Message == nil {
return ""
}
return ctx.Event.Message.ExtractPlainText()
}
// Block 阻止后续触发
func (ctx *Ctx) Block() {
ctx.ma.SetBlock(true)
}
// MessageString 字符串消息便于Regex
func (ctx *Ctx) MessageString() string {
ctx.once.Do(func() {
if ctx.Event != nil && ctx.Event.Message != nil {
ctx.message = ctx.Event.Message.String()
}
})
return ctx.message
}