diff --git a/README.md b/README.md index 9fadb8c..7630c65 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,14 @@ package workers func init() { - RegisterWorkerFactory("ping", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &Ping{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "ping") { + RegisterWorkerFactory("ping", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &Ping{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type Ping struct { @@ -60,7 +63,10 @@ type Ping struct { } ``` 上面的例子是实现里`ping`这个插件的注册,在群组中以`!ping`指令激活。 -同时所有的功能需要写在实现的`GetMsg()`方法里,如`!ping` 命令的: + +同时可以通过修改配置文件的`PLUGINS = ["ai","pkg"]`来决定启用哪些插件。 + +所有的功能需要写在实现的`GetMsg()`方法里,如`!ping` 命令的: ```go func (a *Ping) GetMsg() string { return "Pong!" diff --git a/config example.toml b/config example.toml index d077651..d6418a2 100644 --- a/config example.toml +++ b/config example.toml @@ -8,6 +8,8 @@ AllowRole = [] BlockGroup = [] # 群组黑名单 BlockUser = [] Master = [] +PLUGINS = ["ai","pkg"] + [OPENAI] MODEL = "gpt-4o" OPENAI_API_KEY = "sk-xxxx" diff --git a/config/config.go b/config/config.go index be15b14..989a1b7 100644 --- a/config/config.go +++ b/config/config.go @@ -34,6 +34,13 @@ func GetConfig() map[string]interface{} { return config } +// GetConfigValue 获取配置值 +func GetConfigValue(key string) interface{} { + mu.RLock() + defer mu.RUnlock() + return config[key] +} + // ReloadConfig 重新加载配置文件 func ReloadConfig() error { mu.Lock() diff --git a/main.go b/main.go index 2f34735..85def00 100644 --- a/main.go +++ b/main.go @@ -127,6 +127,7 @@ func main() { if !ok { logrus.Fatalf("加载配置失败!") } + // config.PrintConfig(cfg, "") // 在 App 初始化后统一使用 app.Logger if err := app.initDB(); err != nil { diff --git a/workers/ai.go b/workers/ai.go index 490432d..ced1011 100644 --- a/workers/ai.go +++ b/workers/ai.go @@ -5,8 +5,10 @@ import ( "encoding/base64" "encoding/json" "fmt" + "go-bot/config" "go-bot/tools" "os" + "slices" "io" "log" @@ -19,11 +21,14 @@ import ( ) func init() { - RegisterWorkerFactory("ai", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &AI{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "ai") { + RegisterWorkerFactory("ai", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &AI{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type AI struct { diff --git a/workers/core.go b/workers/core.go index 0f1e10a..7bc6087 100644 --- a/workers/core.go +++ b/workers/core.go @@ -3,6 +3,7 @@ package workers import ( "fmt" "go-bot/config" + "slices" "github.com/imroc/req/v3" ) @@ -84,26 +85,36 @@ func NewStdAns(parms []string, uid, gid, role, mid, rawMsg string) *StdAns { } } +// 将 []interface{} 转换为 []string 的辅助函数 +func toStringSlice(slice []interface{}) []string { + strSlice := make([]string, len(slice)) + for i, v := range slice { + strSlice[i] = v.(string) // 假设切片中的元素都是 string + } + return strSlice +} + func (s *StdAns) CheckPermission() string { - if (len(s.AllowGroup) > 0 && !contains(s.AllowGroup, s.GID)) || contains(s.BlockGroup, s.GID) { + if (len(s.AllowGroup) > 0 && !slices.Contains(toStringSlice(s.AllowGroup), s.GID)) || slices.Contains(toStringSlice(s.BlockGroup), s.GID) { return s.GroupNotAllow } - if (len(s.AllowUser) > 0 && !contains(s.AllowUser, s.UID)) || contains(s.BlockUser, s.UID) { + if (len(s.AllowUser) > 0 && !slices.Contains(toStringSlice(s.AllowUser), s.UID)) || slices.Contains(toStringSlice(s.BlockUser), s.UID) { return s.UserNotAllow } - if len(s.AllowRole) > 0 && !contains(s.AllowRole, s.Role) { + if len(s.AllowRole) > 0 && !slices.Contains(toStringSlice(s.AllowRole), s.Role) { return s.RoleNotAllow } return "ok" } -func contains(slice []interface{}, value string) bool { - for _, item := range slice { - if item == value { - return true - } - } - return false -} + +// func contains[T comparable](slice []T, value T) bool { +// for _, item := range slice { +// if item == value { +// return true +// } +// } +// return false +// } func (s *StdAns) GetMsg() string { // 暂时设置权限校验不通过什么都不提示 diff --git a/workers/emm.go b/workers/emm.go index 41dd0e8..e899ba3 100644 --- a/workers/emm.go +++ b/workers/emm.go @@ -1,11 +1,19 @@ package workers +import ( + "go-bot/config" + "slices" +) + func init() { - RegisterWorkerFactory("emm", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &Emm{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "emm") { + RegisterWorkerFactory("emm", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &Emm{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type Emm struct { diff --git a/workers/hhsh.go b/workers/hhsh.go index eeb2371..da84040 100644 --- a/workers/hhsh.go +++ b/workers/hhsh.go @@ -2,6 +2,8 @@ package workers import ( "fmt" + "go-bot/config" + "slices" "strings" "time" @@ -9,11 +11,14 @@ import ( ) func init() { - RegisterWorkerFactory("hhsh", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &Hhsh{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "hhsh") { + RegisterWorkerFactory("hhsh", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &Hhsh{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type Hhsh struct { diff --git a/workers/httpcat.go b/workers/httpcat.go index 6b8593f..05334c7 100644 --- a/workers/httpcat.go +++ b/workers/httpcat.go @@ -3,7 +3,9 @@ package workers import ( "compress/gzip" "fmt" + "go-bot/config" "io" + "slices" "time" "github.com/PuerkitoBio/goquery" @@ -13,11 +15,14 @@ import ( ) func init() { - RegisterWorkerFactory("httpcat", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &HttpCat{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "httpcat") { + RegisterWorkerFactory("httpcat", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &HttpCat{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type HttpCat struct { diff --git a/workers/httpdog.go b/workers/httpdog.go index 8a90372..abfd957 100644 --- a/workers/httpdog.go +++ b/workers/httpdog.go @@ -3,7 +3,9 @@ package workers import ( "compress/gzip" "fmt" + "go-bot/config" "io" + "slices" "time" "github.com/PuerkitoBio/goquery" @@ -13,11 +15,14 @@ import ( ) func init() { - RegisterWorkerFactory("httpdog", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &HttpDog{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "httpdog") { + RegisterWorkerFactory("httpdog", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &HttpDog{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type HttpDog struct { diff --git a/workers/ip.go b/workers/ip.go index e17527b..93e815c 100644 --- a/workers/ip.go +++ b/workers/ip.go @@ -2,7 +2,9 @@ package workers import ( "fmt" + "go-bot/config" "net" + "slices" "strings" "time" @@ -10,11 +12,14 @@ import ( ) func init() { - RegisterWorkerFactory("ip", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &Ip{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "ip") { + RegisterWorkerFactory("ip", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &Ip{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type Ip struct { diff --git a/workers/lsp.go b/workers/lsp.go index f2f3c40..2c38288 100644 --- a/workers/lsp.go +++ b/workers/lsp.go @@ -2,6 +2,8 @@ package workers import ( "fmt" + "go-bot/config" + "slices" "github.com/goccy/go-json" @@ -10,11 +12,14 @@ import ( ) func init() { - RegisterWorkerFactory("lsp", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &Lsp{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "lsp") { + RegisterWorkerFactory("lsp", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &Lsp{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type Lsp struct { diff --git a/workers/mner.go b/workers/mner.go index b975142..b805f4f 100644 --- a/workers/mner.go +++ b/workers/mner.go @@ -2,6 +2,8 @@ package workers import ( "fmt" + "go-bot/config" + "slices" "sort" "strconv" "time" @@ -10,11 +12,14 @@ import ( ) func init() { - RegisterWorkerFactory("mner", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &Mner{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "mner") { + RegisterWorkerFactory("mner", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &Mner{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type Mner struct { diff --git a/workers/ping.go b/workers/ping.go index 1da36c0..e15a245 100644 --- a/workers/ping.go +++ b/workers/ping.go @@ -1,11 +1,19 @@ package workers +import ( + "go-bot/config" + "slices" +) + func init() { - RegisterWorkerFactory("ping", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &Ping{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "ping") { + RegisterWorkerFactory("ping", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &Ping{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type Ping struct { diff --git a/workers/pkg.go b/workers/pkg.go index 768f1a1..a862fb8 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -2,6 +2,8 @@ package workers import ( "fmt" + "go-bot/config" + "slices" "strings" "time" @@ -9,11 +11,14 @@ import ( ) func init() { - RegisterWorkerFactory("pkg", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { - return &Pkg{ - StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), - } - }) + plugins := config.GetConfig()["PLUGINS"].([]interface{}) + if slices.Contains(plugins, "pkg") { + RegisterWorkerFactory("pkg", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { + return &Pkg{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } + }) + } } type Pkg struct {