control 支持全局禁用&个人用户

This commit is contained in:
fumiama 2021-10-16 14:02:44 +08:00
parent 350ef86dd6
commit 0b7b35cdcc
2 changed files with 47 additions and 18 deletions

View File

@ -38,8 +38,10 @@ zerobot -h -t token -u url [-d|w] [-g] qq1 qq2 qq3 ...
- **动态加载插件** `import _ github.com/FloatTech/ZeroBot-Plugin-Dynamic/dyloader`
- 本功能需要`cgo`,故已分离出主线。详见[ZeroBot-Plugin-Dynamic](https://github.com/FloatTech/ZeroBot-Plugin-Dynamic)
- **插件控制**
- [x] /启用 xxx
- [x] /禁用 xxx
- [x] /启用 xxx (在发送的群/用户启用xxx)
- [x] /禁用 xxx (在发送的群/用户禁用xxx)
- [x] /全局启用 xxx
- [x] /全局禁用 xxx
- [x] /用法 xxx
- [x] /服务列表
- **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat"`

View File

@ -4,6 +4,7 @@ package control
import (
"os"
"strconv"
"strings"
"sync"
"github.com/sirupsen/logrus"
@ -14,6 +15,8 @@ import (
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
)
const ALL int64 = 0
var (
db = &sql.Sqlite{DBPath: "data/control/plugins.db"}
// managers 每个插件对应的管理
@ -50,6 +53,7 @@ func newctrl(service string, o *Options) *Control {
}
// Enable enables a group to pass the Manager.
// groupID == 0 (ALL) will operate on all grps.
func (m *Control) Enable(groupID int64) {
m.Lock()
err := db.Insert(m.service, &grpcfg{groupID, 0})
@ -60,6 +64,7 @@ func (m *Control) Enable(groupID int64) {
}
// Disable disables a group to pass the Manager.
// groupID == 0 (ALL) will operate on all grps.
func (m *Control) Disable(groupID int64) {
m.Lock()
err := db.Insert(m.service, &grpcfg{groupID, 1})
@ -73,7 +78,15 @@ func (m *Control) Disable(groupID int64) {
func (m *Control) IsEnabledIn(gid int64) bool {
m.RLock()
var c grpcfg
err := db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10))
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
}
}
err = db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10))
if err == nil {
m.RUnlock()
logrus.Debugf("[control] plugin %s of grp %d : %d", m.service, c.GroupID, c.Disable)
@ -93,7 +106,12 @@ func (m *Control) IsEnabledIn(gid int64) bool {
func (m *Control) Handler() zero.Rule {
return func(ctx *zero.Ctx) bool {
ctx.State["manager"] = m
return m.IsEnabledIn(ctx.Event.GroupID)
grp := ctx.Event.GroupID
if grp == 0 {
// 个人用户
grp = -ctx.Event.UserID
}
return m.IsEnabledIn(grp)
}
}
@ -135,7 +153,15 @@ func init() {
panic(err)
} else {
hasinit = true
zero.OnCommandGroup([]string{"启用", "enable"}, zero.AdminPermission, zero.OnlyGroup).
zero.OnCommandGroup([]string{
"启用", "enable", "禁用", "disable",
"全局启用", "enableall", "全局禁用", "disableall",
}, 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)
@ -143,20 +169,21 @@ func init() {
if !ok {
ctx.SendChain(message.Text("没有找到指定服务!"))
}
service.Enable(ctx.Event.GroupID)
ctx.SendChain(message.Text("已启用服务: " + model.Args))
})
zero.OnCommandGroup([]string{"禁用", "disable"}, zero.AdminPermission, zero.OnlyGroup).
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
}
if strings.Contains(model.Command, "全局") || strings.Contains(model.Command, "all") {
grp = 0
}
if strings.Contains(model.Command, "启用") || strings.Contains(model.Command, "enable") {
service.Enable(grp)
ctx.SendChain(message.Text("已启用服务: " + model.Args))
} else {
service.Disable(grp)
ctx.SendChain(message.Text("已禁用服务: " + model.Args))
}
service.Disable(ctx.Event.GroupID)
ctx.SendChain(message.Text("已关闭服务: " + model.Args))
})
zero.OnCommandGroup([]string{"用法", "usage"}, zero.AdminPermission, zero.OnlyGroup).