mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-13 02:30:26 +00:00
✨ control 支持全局禁用&个人用户
This commit is contained in:
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user