forked from wdvxdr1123/ZeroBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
189 lines (171 loc) · 4.6 KB
/
bot.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
package zero
import (
"runtime/debug"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
)
// Config is config of zero bot
type Config struct {
NickName []string `json:"nickname"` // 机器人名称
CommandPrefix string `json:"command_prefix"` // 触发命令
SuperUsers []string `json:"super_users"` // 超级用户
SelfID string `json:"self_id"` // 机器人账号
Driver []Driver `json:"-"` // 通信驱动
}
// APICallers 所有的APICaller列表, 通过self-ID映射
var APICallers callerMap
// APICaller is the interface of CallApi
type APICaller interface {
CallApi(request APIRequest) (APIResponse, error)
}
// Driver 与OneBot通信的驱动,使用driver.DefaultWebSocketDriver
type Driver interface {
Connect()
Listen(func([]byte, APICaller))
}
// BotConfig 运行中bot的配置,是Run函数的参数的拷贝
var BotConfig Config
// Run 主函数初始化
func Run(op Config) {
BotConfig = op
for _, driver := range op.Driver {
driver.Connect()
go driver.Listen(processEvent)
}
}
// processEvent 处理事件
func processEvent(response []byte, caller APICaller) {
defer func() {
if pa := recover(); pa != nil {
log.Errorf("handle event err: %v\n%v", pa, string(debug.Stack()))
}
}()
var event Event
_ = json.Unmarshal(response, &event)
event.RawEvent = gjson.Parse(helper.BytesToString(response))
switch event.PostType { // process DetailType
case "message", "message_sent":
event.DetailType = event.MessageType
case "notice":
event.DetailType = event.NoticeType
preprocessNoticeEvent(&event)
case "request":
event.DetailType = event.RequestType
}
if event.PostType == "message" {
preprocessMessageEvent(&event)
}
ctx := &Ctx{
Event: &event,
State: State{},
caller: caller,
}
loop:
for _, matcher := range matcherList {
if !matcher.Type(ctx) {
continue
}
for k := range ctx.State { // Clear State
delete(ctx.State, k)
}
matcherLock.RLock()
m := matcher.copy()
matcherLock.RUnlock()
ctx.ma = m
for _, rule := range m.Rules {
if rule != nil && !rule(ctx) { // 有 Rule 的条件未满足
continue loop
}
}
// pre handler
if m.Engine != nil {
for _, handler := range m.Engine.preHandler {
if !handler(ctx) { // 有 pre handler 未满足
continue loop
}
}
}
if m.Handler != nil {
m.Handler(ctx) // 处理事件
}
if matcher.Temp { // 临时 Matcher 删除
matcher.Delete()
}
if m.Engine != nil {
// post handler
for _, handler := range m.Engine.postHandler {
handler(ctx)
}
}
if m.Block { // 阻断后续
break loop
}
}
}
// preprocessMessageEvent 返回信息事件
func preprocessMessageEvent(e *Event) {
e.Message = message.ParseMessage(e.NativeMessage)
if e.DetailType == "group" {
log.Infof("收到群(%v)消息 %v : %v", e.GroupID, e.Sender.String(), e.RawMessage)
func() { // 处理是否at机器人
e.IsToMe = false
for i, m := range e.Message {
if m.Type == "at" {
qq, _ := strconv.ParseInt(m.Data["qq"], 10, 64)
if qq == e.SelfID {
e.IsToMe = true
e.Message = append(e.Message[:i], e.Message[i+1:]...)
return
}
}
}
if e.Message == nil || len(e.Message) == 0 || e.Message[0].Type != "text" {
return
}
e.Message[0].Data["text"] = strings.TrimLeft(e.Message[0].Data["text"], " ") // Trim!
text := e.Message[0].Data["text"]
for _, nickname := range BotConfig.NickName {
if strings.HasPrefix(text, nickname) {
e.IsToMe = true
e.Message[0].Data["text"] = text[len(nickname):]
return
}
}
}()
} else {
e.IsToMe = true // 私聊也判断为at
log.Infof("收到私聊消息 %v : %v", e.Sender.String(), e.RawMessage)
}
if e.Message == nil || len(e.Message) == 0 || e.Message[0].Type != "text" {
return
}
e.Message[0].Data["text"] = strings.TrimLeft(e.Message[0].Data["text"], " ") // Trim Again!
}
// preprocessNoticeEvent 更新事件
func preprocessNoticeEvent(e *Event) {
if e.SubType == "poke" || e.SubType == "lucky_king" {
e.IsToMe = e.TargetID == e.SelfID
} else {
e.IsToMe = e.UserID == e.SelfID
}
}
// GetBot 获取指定的bot (Ctx)实例
func GetBot(id int64) *Ctx {
caller, ok := APICallers.Load(id)
if !ok {
return nil
}
return &Ctx{caller: caller}
}
// RangeBot 遍历所有bot (Ctx)实例
//
// 单次操作返回 true 则继续遍历,否则退出
func RangeBot(iter func(id int64, ctx *Ctx) bool) {
APICallers.Range(func(key int64, value APICaller) bool {
return iter(key, &Ctx{caller: value})
})
}