forked from yqchilde/wxbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.go
207 lines (193 loc) · 5.55 KB
/
rule.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
package control
import (
"fmt"
"strings"
"sync"
"github.com/miracle0609/wxbot/engine/pkg/log"
"github.com/miracle0609/wxbot/engine/robot"
)
var (
once = sync.Once{}
managers = NewManager("data/manager/manager.db")
)
func newControl(service string, o *Options) robot.Rule {
c := managers.NewControl(service, o)
return func(ctx *robot.Ctx) bool {
ctx.State["manager"] = c
return c.Handler(ctx.Event.FromGroup, ctx.Event.FromWxId)
}
}
func init() {
robot.RegisterApi(&controlApi{})
once.Do(func() {
// 记录聊天文本消息
robot.OnMessage().SetBlock(false).Handle(func(ctx *robot.Ctx) {
if !ctx.IsText() {
return
}
var msgType string
if ctx.IsEventGroupChat() {
msgType = "group"
} else if ctx.IsEventPrivateChat() {
msgType = "private"
} else {
return
}
if err := managers.D.Table("__message").Create(&MessageRecord{
&robot.MessageRecord{
Type: msgType,
FromWxId: ctx.Event.FromUniqueID,
FromNick: ctx.Event.FromUniqueName,
SenderWxId: ctx.Event.FromWxId,
SenderNick: ctx.Event.FromName,
Content: ctx.MessageString(),
},
}).Error; err != nil {
log.Errorf("记录消息失败: %v", err)
}
})
// 响应或沉默某个群或某个私聊,沉默后在该群或私聊中的消息不会被机器人响应
robot.OnCommandGroup([]string{"响应", "沉默"}, robot.UserOrGroupAdmin).SetBlock(true).FirstPriority().Handle(func(ctx *robot.Ctx) {
args := ctx.State["args"].(string)
if args == "" {
args = ctx.Event.FromUniqueID
}
switch ctx.State["command"].(string) {
case "响应":
if err := managers.Response(args); err != nil {
ctx.ReplyTextAndAt("ERROR: " + err.Error())
} else {
ctx.ReplyTextAndAt(fmt.Sprintf("开始响应[%s]消息啦~", args))
}
case "沉默":
if err := managers.Silence(args); err != nil {
ctx.ReplyTextAndAt("ERROR: " + err.Error())
} else {
ctx.ReplyTextAndAt(fmt.Sprintf("已经沉默[%s]消息啦!", args))
}
}
})
// 启用、禁用某个插件在某个群或某个私聊
robot.OnCommandGroup([]string{"启用", "禁用"}, robot.UserOrGroupAdmin).SetBlock(true).FirstPriority().Handle(func(ctx *robot.Ctx) {
args := ctx.State["args"].(string)
if args == "" {
return
}
service, ok := managers.Lookup(args)
if !ok {
ctx.ReplyTextAndAt("没有找到对应插件服务")
return
}
grp := ctx.Event.FromUniqueID
switch ctx.State["command"].(string) {
case "启用":
if err := service.Enable(grp); err != nil {
ctx.ReplyText(err.Error())
return
}
if service.Options.OnEnable != nil {
service.Options.OnEnable(ctx)
} else {
ctx.ReplyText("启用成功")
}
case "禁用":
if err := service.Disable(grp); err != nil {
ctx.ReplyText(err.Error())
return
}
if service.Options.OnDisable != nil {
service.Options.OnDisable(ctx)
} else {
ctx.ReplyText("禁用成功")
}
}
})
// todo 启用、禁用全部插件在某个群或某个私聊
robot.OnCommandGroup([]string{"启用全部", "禁用全部"}, robot.UserOrGroupAdmin).SetBlock(true).FirstPriority().Handle(func(ctx *robot.Ctx) {
})
// 启用、禁用某个插件在所有群和所有私聊
robot.OnCommandGroup([]string{"全局启用", "全局禁用"}, robot.UserOrGroupAdmin).SetBlock(true).FirstPriority().Handle(func(ctx *robot.Ctx) {
args := ctx.State["args"].(string)
if args == "" {
return
}
service, ok := managers.Lookup(args)
if !ok {
ctx.ReplyTextAndAt("没有找到对应插件服务")
return
}
switch ctx.State["command"].(string) {
case "全局启用":
if service.Enable("all") != nil {
ctx.ReplyText("全局启用失败")
return
}
if service.Options.OnEnable != nil {
service.Options.OnEnable(ctx)
} else {
ctx.ReplyText("全局启用成功")
}
case "全局禁用":
if service.Disable("all") != nil {
ctx.ReplyText("全局禁用失败")
return
}
if service.Options.OnDisable != nil {
service.Options.OnDisable(ctx)
} else {
ctx.ReplyText("全局禁用成功")
}
}
})
// 开启、关闭某个插件的全局模式
robot.OnCommand("关闭全局模式", robot.UserOrGroupAdmin).SetBlock(true).FirstPriority().Handle(func(ctx *robot.Ctx) {
args := ctx.State["args"].(string)
if args == "" {
return
}
service, ok := managers.Lookup(args)
if !ok {
ctx.ReplyTextAndAt("没有找到对应插件服务")
return
}
if err := service.CloseGlobalMode(); err != nil {
ctx.ReplyText(err.Error())
return
} else {
ctx.ReplyText("关闭全局模式成功")
}
})
// 在某个群ban、unban某个用户
robot.OnCommandGroup([]string{"ban", "unban"}, robot.UserOrGroupAdmin).SetBlock(true).FirstPriority().Handle(func(ctx *robot.Ctx) {
args := strings.Split(ctx.State["args"].(string), " ")
if len(args) == 0 {
return
}
serv, wxId, grp := args[0], "", ctx.Event.FromUniqueID
if ctx.IsReference() {
wxId = ctx.Event.ReferenceMessage.ChatUser
} else {
wxId = args[1]
}
service, ok := managers.Lookup(serv)
if !ok {
ctx.ReplyTextAndAt("没有找到对应插件服务")
return
}
switch ctx.State["command"].(string) {
case "ban":
if err := service.Ban(wxId, grp); err != nil {
ctx.ReplyText(err.Error())
return
}
ctx.ReplyText("ban成功")
case "unban":
if err := service.UnBan(wxId, grp); err != nil {
ctx.ReplyText(err.Error())
return
}
ctx.ReplyText("unban成功")
}
})
})
}