Skip to content

Commit

Permalink
refactor: 移除option对ctx泛型处理
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Feb 12, 2023
1 parent 05f77ef commit bcf3e53
Show file tree
Hide file tree
Showing 23 changed files with 65 additions and 64 deletions.
6 changes: 3 additions & 3 deletions engine/control/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
var banCache = make(map[string]struct{})

// Ban 禁止某人在某群使用本插件
func (m *Control[CTX]) Ban(uid, gid string) {
func (m *Control) Ban(uid, gid string) {
var err error
if gid != "" {
label := fmt.Sprintf("%s_%s_%s", m.Service, uid, gid)
Expand All @@ -34,7 +34,7 @@ func (m *Control[CTX]) Ban(uid, gid string) {
}

// Permit 允许某人在某群使用本插件
func (m *Control[CTX]) Permit(uid, gid string) {
func (m *Control) Permit(uid, gid string) {
if gid != "" {
label := fmt.Sprintf("%s_%s_%s", m.Service, uid, gid)
m.Manager.Lock()
Expand All @@ -54,7 +54,7 @@ func (m *Control[CTX]) Permit(uid, gid string) {
}

// IsBannedIn 某人是否在某群被ban
func (m *Control[CTX]) IsBannedIn(uid, gid string) bool {
func (m *Control) IsBannedIn(uid, gid string) bool {
var b PluginBanConfig
var err error
if len(gid) != 0 {
Expand Down
8 changes: 4 additions & 4 deletions engine/control/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ package control

var blockCache = make(map[string]bool)

func (manager *Manager[CTX]) initBlock() error {
func (manager *Manager) initBlock() error {
return manager.D.Table("__block").AutoMigrate(&BotBlockConfig{})
}

func (manager *Manager[CTX]) DoBlock(uid string) error {
func (manager *Manager) DoBlock(uid string) error {
manager.Lock()
defer manager.Unlock()
blockCache[uid] = true
return manager.D.Table("__block").Create(&BotBlockConfig{UserID: uid}).Error
}

func (manager *Manager[CTX]) DoUnblock(uid string) error {
func (manager *Manager) DoUnblock(uid string) error {
manager.Lock()
defer manager.Unlock()
blockCache[uid] = false
return manager.D.Table("__block").Where("uid = ?", uid).Delete(&BotBlockConfig{}).Error
}

func (manager *Manager[CTX]) IsBlocked(uid string) bool {
func (manager *Manager) IsBlocked(uid string) bool {
manager.RLock()
isBlock, ok := blockCache[uid]
manager.RUnlock()
Expand Down
24 changes: 13 additions & 11 deletions engine/control/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package control

type Options[CTX any] struct {
Alias string // 插件别名
Help string // 插件帮助信息
Priority uint64 // 优先级,只读
DisableOnDefault bool // 默认禁用状态
HideMenu bool // 是否隐藏在菜单中,默认显示,可用于隐藏一些不希望展示的插件
CacheFolder string // 缓存文件夹
DataFolder string // 数据文件夹
OnEnable func(CTX) // 自定义启用插件后执行的操作
OnDisable func(CTX) // 自定义禁用插件后执行的操作
OnCronjob func(CTX) // 自定义定时任务,用于被定时调用
import "github.com/yqchilde/wxbot/engine/robot"

type Options struct {
Alias string // 插件别名
Help string // 插件帮助信息
Priority uint64 // 优先级,只读
DisableOnDefault bool // 默认禁用状态
HideMenu bool // 是否隐藏在菜单中,默认显示,可用于隐藏一些不希望展示的插件
CacheFolder string // 缓存文件夹
DataFolder string // 数据文件夹
OnEnable func(ctx *robot.Ctx) // 自定义启用插件后执行的操作
OnDisable func(ctx *robot.Ctx) // 自定义禁用插件后执行的操作
OnCronjob func(ctx *robot.Ctx) // 自定义定时任务,用于被定时调用
}

// PluginConfig 插件配置表
Expand Down
26 changes: 13 additions & 13 deletions engine/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
)

// Control 插件控制器
type Control[CTX any] struct {
type Control struct {
Service string // Service 插件服务名
Cache map[string]bool // Cache 缓存
Options Options[CTX] // Options 插件配置
Manager *Manager[CTX] // Manager 插件管理器
Options Options // Options 插件配置
Manager *Manager // Manager 插件管理器
}

func (manager *Manager[CTX]) NewControl(service string, o *Options[CTX]) *Control[CTX] {
m := &Control[CTX]{
func (manager *Manager) NewControl(service string, o *Options) *Control {
m := &Control{
Service: service,
Cache: make(map[string]bool),
Options: func() Options[CTX] {
Options: func() Options {
if o == nil {
return Options[CTX]{}
return Options{}
}
return *o
}(),
Expand All @@ -43,7 +43,7 @@ func (manager *Manager[CTX]) NewControl(service string, o *Options[CTX]) *Contro
}

// Handler 返回预处理器
func (m *Control[CTX]) Handler(gid, uid string) bool {
func (m *Control) Handler(gid, uid string) bool {
if m.Manager.IsBlocked(uid) {
return false
}
Expand All @@ -57,7 +57,7 @@ func (m *Control[CTX]) Handler(gid, uid string) bool {
}

// Enable 使插件在某个群中启用
func (m *Control[CTX]) Enable(groupID string) error {
func (m *Control) Enable(groupID string) error {
if groupID != "all" {
if isEnable, ok := m.IsEnabledAll(true); ok {
if isEnable {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (m *Control[CTX]) Enable(groupID string) error {
}

// Disable 使插件在某个群中禁用
func (m *Control[CTX]) Disable(groupID string) error {
func (m *Control) Disable(groupID string) error {
if groupID != "all" {
if isEnable, ok := m.IsEnabledAll(false); ok {
if isEnable {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (m *Control[CTX]) Disable(groupID string) error {
}

// CloseGlobalMode 关闭全局模式
func (m *Control[CTX]) CloseGlobalMode() error {
func (m *Control) CloseGlobalMode() error {
if err := m.Manager.D.Table(m.Service).Delete(&PluginConfig{}, "gid = ?", "all").Error; err != nil {
log.Errorf("(plugin) %s close global failed: %v", m.Service, err)
return errors.New("关闭失败")
Expand All @@ -129,7 +129,7 @@ func (m *Control[CTX]) CloseGlobalMode() error {
}

// IsEnabledIn 查询开启群组
func (m *Control[CTX]) IsEnabledIn(gid string) bool {
func (m *Control) IsEnabledIn(gid string) bool {
m.Manager.RLock()
isEnable, ok := m.Cache["all"]
m.Manager.RUnlock()
Expand Down Expand Up @@ -157,7 +157,7 @@ func (m *Control[CTX]) IsEnabledIn(gid string) bool {
}

// IsEnabledAll 查询是否全局开启
func (m *Control[CTX]) IsEnabledAll(enable bool) (isEnable bool, ok bool) {
func (m *Control) IsEnabledAll(enable bool) (isEnable bool, ok bool) {
m.Manager.RLock()
isEnable, ok = m.Cache["all"]
m.Manager.RUnlock()
Expand Down
2 changes: 1 addition & 1 deletion engine/control/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Engine struct {
var dataFolderFilter = make(map[string]string)
var cacheFolderFilter = make(map[string]string)

func newEngine(service string, o *Options[*robot.Ctx]) (e *Engine) {
func newEngine(service string, o *Options) (e *Engine) {
e = &Engine{
en: robot.New(),
priority: priority,
Expand Down
14 changes: 7 additions & 7 deletions engine/control/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"github.com/yqchilde/wxbot/engine/pkg/sqlite"
)

type Manager[CTX any] struct {
type Manager struct {
sync.RWMutex
M map[string]*Control[CTX]
M map[string]*Control
D *gorm.DB
}

func NewManager[CTX any](dbpath string) (m Manager[CTX]) {
func NewManager(dbpath string) (m Manager) {
i := strings.LastIndex(dbpath, "/")
if i > 0 {
if err := os.MkdirAll(dbpath[:i], 0755); err != nil {
Expand All @@ -29,8 +29,8 @@ func NewManager[CTX any](dbpath string) (m Manager[CTX]) {
if err := sqlite.Open(dbpath, &db, &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}); err != nil {
log.Fatal("open plugins database failed: ", err)
}
m = Manager[CTX]{
M: map[string]*Control[CTX]{},
m = Manager{
M: map[string]*Control{},
D: db.Orm,
}
if err := m.initBlock(); err != nil {
Expand All @@ -43,14 +43,14 @@ func NewManager[CTX any](dbpath string) (m Manager[CTX]) {
}

// Lookup 查找插件管理器
func (manager *Manager[CTX]) Lookup(service string) (*Control[CTX], bool) {
func (manager *Manager) Lookup(service string) (*Control, bool) {
manager.RLock()
m, ok := manager.M[service]
manager.RUnlock()
return m, ok
}

// LookupAll 查找全部插件管理器
func (manager *Manager[CTX]) LookupAll() map[string]*Control[CTX] {
func (manager *Manager) LookupAll() map[string]*Control {
return manager.M
}
7 changes: 3 additions & 4 deletions engine/control/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"sync/atomic"

"github.com/yqchilde/wxbot/engine/pkg/log"
"github.com/yqchilde/wxbot/engine/robot"
)

var (
Expand All @@ -13,7 +12,7 @@ var (
)

// Register 注册插件控制器
func Register(service string, o *Options[*robot.Ctx]) *Engine {
func Register(service string, o *Options) *Engine {
atomic.AddUint64(&priority, 10)
s, ok := priorityMap[priority]
if ok {
Expand All @@ -25,10 +24,10 @@ func Register(service string, o *Options[*robot.Ctx]) *Engine {
}

// GetOptionsOnCronjob 获取定时任务插件控制器配置
func GetOptionsOnCronjob() map[string]*Control[*robot.Ctx] {
func GetOptionsOnCronjob() map[string]*Control {
var (
services = managers.LookupAll()
servicesClone = make(map[string]*Control[*robot.Ctx])
servicesClone = make(map[string]*Control)
)

for i := range services {
Expand Down
8 changes: 4 additions & 4 deletions engine/control/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

var respCache = make(map[string]bool)

func (manager *Manager[CTX]) initResponse() error {
func (manager *Manager) initResponse() error {
return manager.D.Table("__resp").AutoMigrate(&BotResponseConfig{})
}

func (manager *Manager[CTX]) Response(gid string) error {
func (manager *Manager) Response(gid string) error {
if manager.CanResponse(gid) {
return fmt.Errorf("group-%s is already response", gid)
}
Expand All @@ -20,7 +20,7 @@ func (manager *Manager[CTX]) Response(gid string) error {
return manager.D.Table("__resp").Create(&BotResponseConfig{GroupID: gid}).Error
}

func (manager *Manager[CTX]) Silence(gid string) error {
func (manager *Manager) Silence(gid string) error {
if !manager.CanResponse(gid) {
return fmt.Errorf("group-%s is already silence", gid)
}
Expand All @@ -30,7 +30,7 @@ func (manager *Manager[CTX]) Silence(gid string) error {
return manager.D.Table("__resp").Where("gid = ?", gid).Delete(&BotResponseConfig{}).Error
}

func (manager *Manager[CTX]) CanResponse(gid string) bool {
func (manager *Manager) CanResponse(gid string) bool {
manager.RLock()
isResp, ok := respCache["all"]
manager.RUnlock()
Expand Down
4 changes: 2 additions & 2 deletions engine/control/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

var (
once = sync.Once{}
managers = NewManager[*robot.Ctx]("data/manager/plugins.db")
managers = NewManager("data/manager/plugins.db")
)

func newControl(service string, o *Options[*robot.Ctx]) robot.Rule {
func newControl(service string, o *Options) robot.Rule {
c := managers.NewControl(service, o)
return func(ctx *robot.Ctx) bool {
ctx.State["manager"] = c
Expand Down
2 changes: 1 addition & 1 deletion plugins/baidubaike/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func init() {
engine := control.Register("baidubaike", &control.Options[*robot.Ctx]{
engine := control.Register("baidubaike", &control.Options{
Alias: "百度百科",
Help: "用法:百度百科 XX\n" +
"示例:百度百科 okr",
Expand Down
2 changes: 1 addition & 1 deletion plugins/chatgpt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type ApiKey struct {
}

func init() {
engine := control.Register("chatgpt", &control.Options[*robot.Ctx]{
engine := control.Register("chatgpt", &control.Options{
Alias: "ChatGPT",
Help: "输入 {开始会话} => 进行ChatGPT连续会话",
DataFolder: "chatgpt",
Expand Down
2 changes: 1 addition & 1 deletion plugins/crazykfc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var sentence []string
func init() {
go getCrazyKFCSentence()
rand.Seed(time.Now().UnixNano())
engine := control.Register("kfccrazy", &control.Options[*robot.Ctx]{
engine := control.Register("kfccrazy", &control.Options{
Alias: "kfc骚话",
Help: "输入 {kfc骚话} => 获取肯德基疯狂星期四骚话",
})
Expand Down
2 changes: 1 addition & 1 deletion plugins/ghmonitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Monitor struct {
}

func init() {
engine := control.Register("ghmonitor", &control.Options[*robot.Ctx]{
engine := control.Register("ghmonitor", &control.Options{
Alias: "公众号监控",
Help: "监控公众号 (gh_.*) 转发到 (.*)\n监控公众号关键词 (.*) 转发到 (.*)",
DataFolder: "ghmonitor",
Expand Down
2 changes: 1 addition & 1 deletion plugins/jingdong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func init() {
engine := control.Register("jdbean", &control.Options[*robot.Ctx]{
engine := control.Register("jdbean", &control.Options{
Alias: "京豆上车",
Help: "输入 {京东上车} => 快上车和我一起挂京豆",
DisableOnDefault: true,
Expand Down
4 changes: 2 additions & 2 deletions plugins/manager/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Command struct {
}

func registerCommand() {
engine := control.Register("command", &control.Options[*robot.Ctx]{
engine := control.Register("command", &control.Options{
HideMenu: true,
})

Expand All @@ -37,7 +37,7 @@ func registerCommand() {

// 菜单输出
engine.OnFullMatchGroup([]string{"menu", "菜单"}).SetBlock(true).Handle(func(ctx *robot.Ctx) {
c := ctx.State["manager"].(*control.Control[*robot.Ctx])
c := ctx.State["manager"].(*control.Control)
options := MenuOptions{WxId: ctx.Event.FromUniqueID}
for _, m := range c.Manager.M {
if m.Options.HideMenu {
Expand Down
2 changes: 1 addition & 1 deletion plugins/manager/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type CronJob struct {
}

func registerCronjob() {
engine := control.Register("cronjob", &control.Options[*robot.Ctx]{
engine := control.Register("cronjob", &control.Options{
Alias: "定时任务",
Help: "管理员设置定时任务",
DataFolder: "manager",
Expand Down
2 changes: 1 addition & 1 deletion plugins/manager/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package manager

func registerEvent() {
// 该插件按需引入,自行修改
//engine := control.Register("event", &control.Options[*robot.Ctx]{
//engine := control.Register("event", &control.Options{
// HideMenu: true,
//})

Expand Down
2 changes: 1 addition & 1 deletion plugins/memepicture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func init() {
engine := control.Register("memepicture", &control.Options[*robot.Ctx]{
engine := control.Register("memepicture", &control.Options{
Alias: "表情原图",
Help: "输入 {表情原图} => 30s内发送表情获取表情原图",
})
Expand Down
2 changes: 1 addition & 1 deletion plugins/moyuban/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func init() {
engine := control.Register("moyu", &control.Options[*robot.Ctx]{
engine := control.Register("moyu", &control.Options{
Alias: "摸鱼日历",
Help: "输入 {摸鱼日历|摸鱼} => 获取摸鱼办日历",
})
Expand Down
Loading

0 comments on commit bcf3e53

Please sign in to comment.