@@ -15,8 +15,6 @@ import (
15
15
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
16
16
)
17
17
18
- const ALL int64 = 0
19
-
20
18
var (
21
19
db = & sql.Sqlite {DBPath : "data/control/plugins.db" }
22
20
// managers 每个插件对应的管理
@@ -57,48 +55,56 @@ func newctrl(service string, o *Options) *Control {
57
55
func (m * Control ) Enable (groupID int64 ) {
58
56
m .Lock ()
59
57
err := db .Insert (m .service , & grpcfg {groupID , 0 })
58
+ m .Unlock ()
60
59
if err != nil {
61
60
logrus .Errorf ("[control] %v" , err )
62
61
}
63
- m .Unlock ()
64
62
}
65
63
66
64
// Disable disables a group to pass the Manager.
67
65
// groupID == 0 (ALL) will operate on all grps.
68
66
func (m * Control ) Disable (groupID int64 ) {
69
67
m .Lock ()
70
68
err := db .Insert (m .service , & grpcfg {groupID , 1 })
69
+ m .Unlock ()
71
70
if err != nil {
72
71
logrus .Errorf ("[control] %v" , err )
73
72
}
74
- m .Unlock ()
73
+ }
74
+
75
+ // Reset resets the default config of a group.
76
+ // groupID == 0 (ALL) is not allowed.
77
+ func (m * Control ) Reset (groupID int64 ) {
78
+ if groupID != 0 {
79
+ m .Lock ()
80
+ err := db .Del (m .service , "WHERE gid = " + strconv .FormatInt (groupID , 10 ))
81
+ m .Unlock ()
82
+ if err != nil {
83
+ logrus .Errorf ("[control] %v" , err )
84
+ }
85
+ }
75
86
}
76
87
77
88
// IsEnabledIn 开启群
78
89
func (m * Control ) IsEnabledIn (gid int64 ) bool {
79
- m .RLock ()
80
90
var c grpcfg
81
- err := db .Find (m .service , & c , "WHERE gid = " + strconv .FormatInt (ALL , 10 ))
82
- if err == nil {
83
- logrus .Debugf ("[control] plugin %s of all : %d" , m .service , c .GroupID , c .Disable )
84
- if c .Disable != 0 {
85
- m .RUnlock ()
86
- return false
91
+ var err error
92
+ if gid != 0 {
93
+ m .RLock ()
94
+ err = db .Find (m .service , & c , "WHERE gid = " + strconv .FormatInt (gid , 10 ))
95
+ m .RUnlock ()
96
+ if err == nil {
97
+ logrus .Debugf ("[control] plugin %s of grp %d : %d" , m .service , c .GroupID , c .Disable )
98
+ return c .Disable == 0
87
99
}
88
100
}
89
- err = db .Find (m .service , & c , "WHERE gid = " + strconv .FormatInt (gid , 10 ))
101
+ m .RLock ()
102
+ err = db .Find (m .service , & c , "WHERE gid = 0" )
103
+ m .RUnlock ()
90
104
if err == nil {
91
- m .RUnlock ()
92
- logrus .Debugf ("[control] plugin %s of grp %d : %d" , m .service , c .GroupID , c .Disable )
105
+ logrus .Debugf ("[control] plugin %s of all : %d" , m .service , c .GroupID , c .Disable )
93
106
return c .Disable == 0
94
107
}
95
- logrus .Errorf ("[control] %v" , err )
96
- m .RUnlock ()
97
- if m .options .DisableOnDefault {
98
- m .Disable (gid )
99
- } else {
100
- m .Enable (gid )
101
- }
102
108
return ! m .options .DisableOnDefault
103
109
}
104
110
@@ -161,30 +167,50 @@ func init() {
161
167
return zero .AdminPermission (ctx )
162
168
}
163
169
return zero .OnlyToMe (ctx )
164
- }).
165
- Handle (func (ctx * zero.Ctx ) {
166
- model := extension.CommandModel {}
167
- _ = ctx .Parse (& model )
168
- service , ok := Lookup (model .Args )
169
- if ! ok {
170
- ctx .SendChain (message .Text ("没有找到指定服务!" ))
171
- }
172
- grp := ctx .Event .GroupID
173
- if grp == 0 {
174
- // 个人用户
175
- grp = - ctx .Event .UserID
176
- }
177
- if strings .Contains (model .Command , "全局" ) || strings .Contains (model .Command , "all" ) {
178
- grp = 0
179
- }
180
- if strings .Contains (model .Command , "启用" ) || strings .Contains (model .Command , "enable" ) {
181
- service .Enable (grp )
182
- ctx .SendChain (message .Text ("已启用服务: " + model .Args ))
183
- } else {
184
- service .Disable (grp )
185
- ctx .SendChain (message .Text ("已禁用服务: " + model .Args ))
186
- }
187
- })
170
+ }).Handle (func (ctx * zero.Ctx ) {
171
+ model := extension.CommandModel {}
172
+ _ = ctx .Parse (& model )
173
+ service , ok := Lookup (model .Args )
174
+ if ! ok {
175
+ ctx .SendChain (message .Text ("没有找到指定服务!" ))
176
+ }
177
+ grp := ctx .Event .GroupID
178
+ if grp == 0 {
179
+ // 个人用户
180
+ grp = - ctx .Event .UserID
181
+ }
182
+ if strings .Contains (model .Command , "全局" ) || strings .Contains (model .Command , "all" ) {
183
+ grp = 0
184
+ }
185
+ if strings .Contains (model .Command , "启用" ) || strings .Contains (model .Command , "enable" ) {
186
+ service .Enable (grp )
187
+ ctx .SendChain (message .Text ("已启用服务: " + model .Args ))
188
+ } else {
189
+ service .Disable (grp )
190
+ ctx .SendChain (message .Text ("已禁用服务: " + model .Args ))
191
+ }
192
+ })
193
+
194
+ zero .OnCommandGroup ([]string {"还原" , "reset" }, func (ctx * zero.Ctx ) bool {
195
+ if zero .OnlyGroup (ctx ) {
196
+ return zero .AdminPermission (ctx )
197
+ }
198
+ return zero .OnlyToMe (ctx )
199
+ }).Handle (func (ctx * zero.Ctx ) {
200
+ model := extension.CommandModel {}
201
+ _ = ctx .Parse (& model )
202
+ service , ok := Lookup (model .Args )
203
+ if ! ok {
204
+ ctx .SendChain (message .Text ("没有找到指定服务!" ))
205
+ }
206
+ grp := ctx .Event .GroupID
207
+ if grp == 0 {
208
+ // 个人用户
209
+ grp = - ctx .Event .UserID
210
+ }
211
+ service .Reset (grp )
212
+ ctx .SendChain (message .Text ("已还原服务的默认启用状态: " + model .Args ))
213
+ })
188
214
189
215
zero .OnCommandGroup ([]string {"用法" , "usage" }, zero .AdminPermission , zero .OnlyGroup ).
190
216
Handle (func (ctx * zero.Ctx ) {
0 commit comments