forked from wdvxdr1123/ZeroBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
172 lines (151 loc) · 4.06 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
package zero
import (
"fmt"
"reflect"
"sync"
"unsafe"
"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
}
// ExposeCaller as *T, maybe panic if misused
func ExposeCaller[T any](ctx *Ctx) *T {
return (*T)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ctx.caller), unsafe.Sizeof(uintptr(0)))))
}
// 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(msg interface{}) message.MessageID {
event := ctx.Event
m, ok := msg.(message.Message)
if !ok {
var p *message.Message
p, ok = msg.(*message.Message)
if ok {
m = *p
}
}
if ok && len(m) > 0 && m[0].Type == "node" && event.DetailType != "guild" {
if event.GroupID != 0 {
return message.NewMessageIDFromInteger(ctx.SendGroupForwardMessage(event.GroupID, m).Get("message_id").Int())
}
return message.NewMessageIDFromInteger(ctx.SendPrivateForwardMessage(event.UserID, m).Get("message_id").Int())
}
if event.DetailType == "guild" {
return message.NewMessageIDFromString(ctx.SendGuildChannelMessage(event.GuildID, event.ChannelID, msg))
}
if event.GroupID != 0 {
return message.NewMessageIDFromInteger(ctx.SendGroupMessage(event.GroupID, msg))
}
return message.NewMessageIDFromInteger(ctx.SendPrivateMessage(event.UserID, msg))
}
// SendChain 快捷发送消息/合并转发-消息链
func (ctx *Ctx) SendChain(msg ...message.MessageSegment) message.MessageID {
return ctx.Send((message.Message)(msg))
}
// Echo 向自身分发虚拟事件
func (ctx *Ctx) Echo(response []byte) {
if BotConfig.RingLen != 0 {
evring.processEvent(response, ctx.caller)
} else {
processEventAsync(response, ctx.caller, BotConfig.MaxProcessTime)
}
}
// 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()).Event.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)
}
// Block 在 pre, rules, mid 阶段阻止后续触发
func (ctx *Ctx) Break() {
ctx.ma.Break = true
}
// NoTimeout 处理时不设超时
func (ctx *Ctx) NoTimeout() {
ctx.ma.NoTimeout = 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
}