增加动态加载,添加默认禁用

This commit is contained in:
fumiama
2021-10-11 21:44:46 +08:00
parent 43735722bf
commit 173925b57a
5 changed files with 66 additions and 1 deletions

49
dyloader/scan.go Normal file
View File

@@ -0,0 +1,49 @@
//go:build !windows
// +build !windows
package dyloader
import (
"io/fs"
"path/filepath"
"plugin"
"strings"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
func init() {
_ = scan()
zero.OnCommand("刷新插件", zero.SuperUserPermission).SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
err := scan()
if err != nil {
ctx.SendChain(message.Text("Error: " + err.Error()))
}
})
}
func scan() error {
return filepath.WalkDir("plugins/", load)
}
func load(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if strings.HasSuffix(d.Name(), ".so") {
_, err = plugin.Open(path)
if err == nil {
logrus.Infoln("[dyloader]加载插件", path, "成功")
}
if err != nil {
logrus.Errorln("[dyloader]加载插件", path, "错误:", err)
}
}
return nil
}

4
dyloader/winign.go Normal file
View File

@@ -0,0 +1,4 @@
//go:build windows
// +build windows
package dyloader