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
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!"

View file

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

View file

@ -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()

View file

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

View file

@ -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 {

View file

@ -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 {
// 暂时设置权限校验不通过什么都不提示

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {