refactor(workers): 根据配置文件动态加载插件

This commit is contained in:
liyp 2024-10-25 23:15:20 +08:00
parent b33e44e07c
commit 1a146d4bfb
15 changed files with 150 additions and 67 deletions

View file

@ -48,11 +48,14 @@
package workers package workers
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "ping") {
RegisterWorkerFactory("ping", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("ping", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Ping{ return &Ping{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type Ping struct { type Ping struct {
@ -60,7 +63,10 @@ type Ping struct {
} }
``` ```
上面的例子是实现里`ping`这个插件的注册,在群组中以`!ping`指令激活。 上面的例子是实现里`ping`这个插件的注册,在群组中以`!ping`指令激活。
同时所有的功能需要写在实现的`GetMsg()`方法里,如`!ping` 命令的:
同时可以通过修改配置文件的`PLUGINS = ["ai","pkg"]`来决定启用哪些插件。
所有的功能需要写在实现的`GetMsg()`方法里,如`!ping` 命令的:
```go ```go
func (a *Ping) GetMsg() string { func (a *Ping) GetMsg() string {
return "Pong!" return "Pong!"

View file

@ -8,6 +8,8 @@ AllowRole = []
BlockGroup = [] # 群组黑名单 BlockGroup = [] # 群组黑名单
BlockUser = [] BlockUser = []
Master = [] Master = []
PLUGINS = ["ai","pkg"]
[OPENAI] [OPENAI]
MODEL = "gpt-4o" MODEL = "gpt-4o"
OPENAI_API_KEY = "sk-xxxx" OPENAI_API_KEY = "sk-xxxx"

View file

@ -34,6 +34,13 @@ func GetConfig() map[string]interface{} {
return config return config
} }
// GetConfigValue 获取配置值
func GetConfigValue(key string) interface{} {
mu.RLock()
defer mu.RUnlock()
return config[key]
}
// ReloadConfig 重新加载配置文件 // ReloadConfig 重新加载配置文件
func ReloadConfig() error { func ReloadConfig() error {
mu.Lock() mu.Lock()

View file

@ -127,6 +127,7 @@ func main() {
if !ok { if !ok {
logrus.Fatalf("加载配置失败!") logrus.Fatalf("加载配置失败!")
} }
// config.PrintConfig(cfg, "")
// 在 App 初始化后统一使用 app.Logger // 在 App 初始化后统一使用 app.Logger
if err := app.initDB(); err != nil { if err := app.initDB(); err != nil {

View file

@ -5,8 +5,10 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"go-bot/config"
"go-bot/tools" "go-bot/tools"
"os" "os"
"slices"
"io" "io"
"log" "log"
@ -19,11 +21,14 @@ import (
) )
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "ai") {
RegisterWorkerFactory("ai", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("ai", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &AI{ return &AI{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type AI struct { type AI struct {

View file

@ -3,6 +3,7 @@ package workers
import ( import (
"fmt" "fmt"
"go-bot/config" "go-bot/config"
"slices"
"github.com/imroc/req/v3" "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 { 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 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 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 s.RoleNotAllow
} }
return "ok" return "ok"
} }
func contains(slice []interface{}, value string) bool {
for _, item := range slice { // func contains[T comparable](slice []T, value T) bool {
if item == value { // for _, item := range slice {
return true // if item == value {
} // return true
} // }
return false // }
} // return false
// }
func (s *StdAns) GetMsg() string { func (s *StdAns) GetMsg() string {
// 暂时设置权限校验不通过什么都不提示 // 暂时设置权限校验不通过什么都不提示

View file

@ -1,11 +1,19 @@
package workers package workers
import (
"go-bot/config"
"slices"
)
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "emm") {
RegisterWorkerFactory("emm", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("emm", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Emm{ return &Emm{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type Emm struct { type Emm struct {

View file

@ -2,6 +2,8 @@ package workers
import ( import (
"fmt" "fmt"
"go-bot/config"
"slices"
"strings" "strings"
"time" "time"
@ -9,11 +11,14 @@ import (
) )
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "hhsh") {
RegisterWorkerFactory("hhsh", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("hhsh", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Hhsh{ return &Hhsh{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type Hhsh struct { type Hhsh struct {

View file

@ -3,7 +3,9 @@ package workers
import ( import (
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"go-bot/config"
"io" "io"
"slices"
"time" "time"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
@ -13,11 +15,14 @@ import (
) )
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "httpcat") {
RegisterWorkerFactory("httpcat", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("httpcat", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &HttpCat{ return &HttpCat{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type HttpCat struct { type HttpCat struct {

View file

@ -3,7 +3,9 @@ package workers
import ( import (
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"go-bot/config"
"io" "io"
"slices"
"time" "time"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
@ -13,11 +15,14 @@ import (
) )
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "httpdog") {
RegisterWorkerFactory("httpdog", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("httpdog", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &HttpDog{ return &HttpDog{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type HttpDog struct { type HttpDog struct {

View file

@ -2,7 +2,9 @@ package workers
import ( import (
"fmt" "fmt"
"go-bot/config"
"net" "net"
"slices"
"strings" "strings"
"time" "time"
@ -10,11 +12,14 @@ import (
) )
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "ip") {
RegisterWorkerFactory("ip", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("ip", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Ip{ return &Ip{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type Ip struct { type Ip struct {

View file

@ -2,6 +2,8 @@ package workers
import ( import (
"fmt" "fmt"
"go-bot/config"
"slices"
"github.com/goccy/go-json" "github.com/goccy/go-json"
@ -10,11 +12,14 @@ import (
) )
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "lsp") {
RegisterWorkerFactory("lsp", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("lsp", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Lsp{ return &Lsp{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type Lsp struct { type Lsp struct {

View file

@ -2,6 +2,8 @@ package workers
import ( import (
"fmt" "fmt"
"go-bot/config"
"slices"
"sort" "sort"
"strconv" "strconv"
"time" "time"
@ -10,11 +12,14 @@ import (
) )
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "mner") {
RegisterWorkerFactory("mner", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("mner", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Mner{ return &Mner{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type Mner struct { type Mner struct {

View file

@ -1,11 +1,19 @@
package workers package workers
import (
"go-bot/config"
"slices"
)
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "ping") {
RegisterWorkerFactory("ping", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("ping", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Ping{ return &Ping{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type Ping struct { type Ping struct {

View file

@ -2,6 +2,8 @@ package workers
import ( import (
"fmt" "fmt"
"go-bot/config"
"slices"
"strings" "strings"
"time" "time"
@ -9,11 +11,14 @@ import (
) )
func init() { func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "pkg") {
RegisterWorkerFactory("pkg", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { RegisterWorkerFactory("pkg", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Pkg{ return &Pkg{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
} }
}) })
}
} }
type Pkg struct { type Pkg struct {