Skip to content

Commit 3893593

Browse files
committed
✨ control 增加还原;调整禁用优先级
1 parent a3685e2 commit 3893593

File tree

2 files changed

+72
-45
lines changed

2 files changed

+72
-45
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ zerobot -h -t token -u url [-d|w] [-g] qq1 qq2 qq3 ...
4242
- [x] /禁用 xxx (在发送的群/用户禁用xxx)
4343
- [x] /全局启用 xxx
4444
- [x] /全局禁用 xxx
45+
- [x] /还原 xxx (在发送的群/用户还原xxx的开启状态到初始状态)
4546
- [x] /用法 xxx
4647
- [x] /服务列表
4748
- **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat"`

control/rule.go

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
1616
)
1717

18-
const ALL int64 = 0
19-
2018
var (
2119
db = &sql.Sqlite{DBPath: "data/control/plugins.db"}
2220
// managers 每个插件对应的管理
@@ -57,48 +55,56 @@ func newctrl(service string, o *Options) *Control {
5755
func (m *Control) Enable(groupID int64) {
5856
m.Lock()
5957
err := db.Insert(m.service, &grpcfg{groupID, 0})
58+
m.Unlock()
6059
if err != nil {
6160
logrus.Errorf("[control] %v", err)
6261
}
63-
m.Unlock()
6462
}
6563

6664
// Disable disables a group to pass the Manager.
6765
// groupID == 0 (ALL) will operate on all grps.
6866
func (m *Control) Disable(groupID int64) {
6967
m.Lock()
7068
err := db.Insert(m.service, &grpcfg{groupID, 1})
69+
m.Unlock()
7170
if err != nil {
7271
logrus.Errorf("[control] %v", err)
7372
}
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+
}
7586
}
7687

7788
// IsEnabledIn 开启群
7889
func (m *Control) IsEnabledIn(gid int64) bool {
79-
m.RLock()
8090
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
8799
}
88100
}
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()
90104
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)
93106
return c.Disable == 0
94107
}
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-
}
102108
return !m.options.DisableOnDefault
103109
}
104110

@@ -161,30 +167,50 @@ func init() {
161167
return zero.AdminPermission(ctx)
162168
}
163169
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+
})
188214

189215
zero.OnCommandGroup([]string{"用法", "usage"}, zero.AdminPermission, zero.OnlyGroup).
190216
Handle(func(ctx *zero.Ctx) {

0 commit comments

Comments
 (0)