diff --git a/README.md b/README.md index 13419334..1f459f9f 100644 --- a/README.md +++ b/README.md @@ -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"` diff --git a/control/rule.go b/control/rule.go index cb036720..397ca2b8 100644 --- a/control/rule.go +++ b/control/rule.go @@ -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) } - 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 开启群 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)) + m.RUnlock() + 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)) - if err == nil { - 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.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,30 +167,50 @@ func init() { 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 - } - 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)) - } - }) + }).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)) + } + }) + + 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) {