mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-20 06:20:08 +08:00
✨ control 增加还原;调整禁用优先级
This commit is contained in:
parent
a3685e2e83
commit
38935937ae
@ -42,6 +42,7 @@ zerobot -h -t token -u url [-d|w] [-g] qq1 qq2 qq3 ...
|
|||||||
- [x] /禁用 xxx (在发送的群/用户禁用xxx)
|
- [x] /禁用 xxx (在发送的群/用户禁用xxx)
|
||||||
- [x] /全局启用 xxx
|
- [x] /全局启用 xxx
|
||||||
- [x] /全局禁用 xxx
|
- [x] /全局禁用 xxx
|
||||||
|
- [x] /还原 xxx (在发送的群/用户还原xxx的开启状态到初始状态)
|
||||||
- [x] /用法 xxx
|
- [x] /用法 xxx
|
||||||
- [x] /服务列表
|
- [x] /服务列表
|
||||||
- **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat"`
|
- **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat"`
|
||||||
|
|||||||
118
control/rule.go
118
control/rule.go
@ -15,8 +15,6 @@ import (
|
|||||||
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ALL int64 = 0
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db = &sql.Sqlite{DBPath: "data/control/plugins.db"}
|
db = &sql.Sqlite{DBPath: "data/control/plugins.db"}
|
||||||
// managers 每个插件对应的管理
|
// managers 每个插件对应的管理
|
||||||
@ -57,10 +55,10 @@ func newctrl(service string, o *Options) *Control {
|
|||||||
func (m *Control) Enable(groupID int64) {
|
func (m *Control) Enable(groupID int64) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
err := db.Insert(m.service, &grpcfg{groupID, 0})
|
err := db.Insert(m.service, &grpcfg{groupID, 0})
|
||||||
|
m.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("[control] %v", err)
|
logrus.Errorf("[control] %v", err)
|
||||||
}
|
}
|
||||||
m.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disable disables a group to pass the Manager.
|
// Disable disables a group to pass the Manager.
|
||||||
@ -68,36 +66,44 @@ func (m *Control) Enable(groupID int64) {
|
|||||||
func (m *Control) Disable(groupID int64) {
|
func (m *Control) Disable(groupID int64) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
err := db.Insert(m.service, &grpcfg{groupID, 1})
|
err := db.Insert(m.service, &grpcfg{groupID, 1})
|
||||||
|
m.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("[control] %v", err)
|
logrus.Errorf("[control] %v", err)
|
||||||
}
|
}
|
||||||
m.Unlock()
|
}
|
||||||
|
|
||||||
|
// 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 开启群
|
// IsEnabledIn 开启群
|
||||||
func (m *Control) IsEnabledIn(gid int64) bool {
|
func (m *Control) IsEnabledIn(gid int64) bool {
|
||||||
m.RLock()
|
|
||||||
var c grpcfg
|
var c grpcfg
|
||||||
err := db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(ALL, 10))
|
var err error
|
||||||
if err == nil {
|
if gid != 0 {
|
||||||
logrus.Debugf("[control] plugin %s of all : %d", m.service, c.GroupID, c.Disable)
|
m.RLock()
|
||||||
if c.Disable != 0 {
|
err = db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10))
|
||||||
m.RUnlock()
|
m.RUnlock()
|
||||||
return false
|
if err == nil {
|
||||||
|
logrus.Debugf("[control] plugin %s of grp %d : %d", m.service, c.GroupID, c.Disable)
|
||||||
|
return c.Disable == 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10))
|
m.RLock()
|
||||||
if err == nil {
|
err = db.Find(m.service, &c, "WHERE gid = 0")
|
||||||
m.RUnlock()
|
|
||||||
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.RUnlock()
|
m.RUnlock()
|
||||||
if m.options.DisableOnDefault {
|
if err == nil {
|
||||||
m.Disable(gid)
|
logrus.Debugf("[control] plugin %s of all : %d", m.service, c.GroupID, c.Disable)
|
||||||
} else {
|
return c.Disable == 0
|
||||||
m.Enable(gid)
|
|
||||||
}
|
}
|
||||||
return !m.options.DisableOnDefault
|
return !m.options.DisableOnDefault
|
||||||
}
|
}
|
||||||
@ -161,30 +167,50 @@ func init() {
|
|||||||
return zero.AdminPermission(ctx)
|
return zero.AdminPermission(ctx)
|
||||||
}
|
}
|
||||||
return zero.OnlyToMe(ctx)
|
return zero.OnlyToMe(ctx)
|
||||||
}).
|
}).Handle(func(ctx *zero.Ctx) {
|
||||||
Handle(func(ctx *zero.Ctx) {
|
model := extension.CommandModel{}
|
||||||
model := extension.CommandModel{}
|
_ = ctx.Parse(&model)
|
||||||
_ = ctx.Parse(&model)
|
service, ok := Lookup(model.Args)
|
||||||
service, ok := Lookup(model.Args)
|
if !ok {
|
||||||
if !ok {
|
ctx.SendChain(message.Text("没有找到指定服务!"))
|
||||||
ctx.SendChain(message.Text("没有找到指定服务!"))
|
}
|
||||||
}
|
grp := ctx.Event.GroupID
|
||||||
grp := ctx.Event.GroupID
|
if grp == 0 {
|
||||||
if grp == 0 {
|
// 个人用户
|
||||||
// 个人用户
|
grp = -ctx.Event.UserID
|
||||||
grp = -ctx.Event.UserID
|
}
|
||||||
}
|
if strings.Contains(model.Command, "全局") || strings.Contains(model.Command, "all") {
|
||||||
if strings.Contains(model.Command, "全局") || strings.Contains(model.Command, "all") {
|
grp = 0
|
||||||
grp = 0
|
}
|
||||||
}
|
if strings.Contains(model.Command, "启用") || strings.Contains(model.Command, "enable") {
|
||||||
if strings.Contains(model.Command, "启用") || strings.Contains(model.Command, "enable") {
|
service.Enable(grp)
|
||||||
service.Enable(grp)
|
ctx.SendChain(message.Text("已启用服务: " + model.Args))
|
||||||
ctx.SendChain(message.Text("已启用服务: " + model.Args))
|
} else {
|
||||||
} else {
|
service.Disable(grp)
|
||||||
service.Disable(grp)
|
ctx.SendChain(message.Text("已禁用服务: " + model.Args))
|
||||||
ctx.SendChain(message.Text("已禁用服务: " + model.Args))
|
}
|
||||||
}
|
})
|
||||||
})
|
|
||||||
|
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).
|
zero.OnCommandGroup([]string{"用法", "usage"}, zero.AdminPermission, zero.OnlyGroup).
|
||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user