control 增加还原;调整禁用优先级

This commit is contained in:
fumiama 2021-10-26 00:05:45 +08:00
parent a3685e2e83
commit 38935937ae
2 changed files with 73 additions and 46 deletions

View File

@ -42,6 +42,7 @@ zerobot -h -t token -u url [-d|w] [-g] qq1 qq2 qq3 ...
- [x] /禁用 xxx (在发送的群/用户禁用xxx)
- [x] /全局启用 xxx
- [x] /全局禁用 xxx
- [x] /还原 xxx (在发送的群/用户还原xxx的开启状态到初始状态)
- [x] /用法 xxx
- [x] /服务列表
- **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat"`

View File

@ -15,8 +15,6 @@ import (
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
)
const ALL int64 = 0
var (
db = &sql.Sqlite{DBPath: "data/control/plugins.db"}
// managers 每个插件对应的管理
@ -57,10 +55,10 @@ func newctrl(service string, o *Options) *Control {
func (m *Control) Enable(groupID int64) {
m.Lock()
err := db.Insert(m.service, &grpcfg{groupID, 0})
m.Unlock()
if err != nil {
logrus.Errorf("[control] %v", err)
}
m.Unlock()
}
// Disable disables a group to pass the Manager.
@ -68,36 +66,44 @@ func (m *Control) Enable(groupID int64) {
func (m *Control) Disable(groupID int64) {
m.Lock()
err := db.Insert(m.service, &grpcfg{groupID, 1})
m.Unlock()
if err != nil {
logrus.Errorf("[control] %v", err)
}
}
// Reset resets the default config of a group.
// groupID == 0 (ALL) is not allowed.
func (m *Control) Reset(groupID int64) {
if groupID != 0 {
m.Lock()
err := db.Del(m.service, "WHERE gid = "+strconv.FormatInt(groupID, 10))
m.Unlock()
if err != nil {
logrus.Errorf("[control] %v", err)
}
}
}
// IsEnabledIn 开启群
func (m *Control) IsEnabledIn(gid int64) bool {
m.RLock()
var c grpcfg
err := db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(ALL, 10))
if err == nil {
logrus.Debugf("[control] plugin %s of all : %d", m.service, c.GroupID, c.Disable)
if c.Disable != 0 {
m.RUnlock()
return false
}
}
var err error
if gid != 0 {
m.RLock()
err = db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10))
if err == nil {
m.RUnlock()
if err == nil {
logrus.Debugf("[control] plugin %s of grp %d : %d", m.service, c.GroupID, c.Disable)
return c.Disable == 0
}
logrus.Errorf("[control] %v", err)
}
m.RLock()
err = db.Find(m.service, &c, "WHERE gid = 0")
m.RUnlock()
if m.options.DisableOnDefault {
m.Disable(gid)
} else {
m.Enable(gid)
if err == nil {
logrus.Debugf("[control] plugin %s of all : %d", m.service, c.GroupID, c.Disable)
return c.Disable == 0
}
return !m.options.DisableOnDefault
}
@ -161,8 +167,7 @@ func init() {
return zero.AdminPermission(ctx)
}
return zero.OnlyToMe(ctx)
}).
Handle(func(ctx *zero.Ctx) {
}).Handle(func(ctx *zero.Ctx) {
model := extension.CommandModel{}
_ = ctx.Parse(&model)
service, ok := Lookup(model.Args)
@ -186,6 +191,27 @@ func init() {
}
})
zero.OnCommandGroup([]string{"还原", "reset"}, func(ctx *zero.Ctx) bool {
if zero.OnlyGroup(ctx) {
return zero.AdminPermission(ctx)
}
return zero.OnlyToMe(ctx)
}).Handle(func(ctx *zero.Ctx) {
model := extension.CommandModel{}
_ = ctx.Parse(&model)
service, ok := Lookup(model.Args)
if !ok {
ctx.SendChain(message.Text("没有找到指定服务!"))
}
grp := ctx.Event.GroupID
if grp == 0 {
// 个人用户
grp = -ctx.Event.UserID
}
service.Reset(grp)
ctx.SendChain(message.Text("已还原服务的默认启用状态: " + model.Args))
})
zero.OnCommandGroup([]string{"用法", "usage"}, zero.AdminPermission, zero.OnlyGroup).
Handle(func(ctx *zero.Ctx) {
model := extension.CommandModel{}