diff --git a/.gitignore b/.gitignore index 9480c4ef..d8e6bd6c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ data/manager data/acgimage data/fortune data/hs +plugins/*.so .idea/ .DS_Store .vscode diff --git a/README.md b/README.md index fb97b460..6d8f0030 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,17 @@ zerobot -h -t token -u url [-d|w] [-g] qq1 qq2 qq3 ... > 在编译时,以下功能除插件控制外,均可通过注释`main.go`中的相应`import`而物理禁用,减小插件体积。 > 通过插件控制,还可动态管理某个功能在某个群的打开/关闭。 - **web管理** + - 因为开启后可执行文件大约增加 5M ,默认注释不开启。 - 需要配合 [webgui](https://github.com/FloatTech/bot-manager) 使用 +- **动态加载插件** + - [x] /刷新插件 + - 仅 Linux, FreeBSD, macOS 可用,默认注释不开启。 + - 开启后`zbp`可执行文件约增大 2M ,每个插件的`.so`文件约 4 ~ 20 M ,如非必要建议不开启。 + - 动态加载的插件需放置在`plugins/`下,编译命令如下。插件包名必须为`main`。 + ```bash + go build -ldflags "-s -w" -buildmode=plugin + ``` + - 插件一经加载,无法再卸载,只能通过`control`控制。 - **插件控制** - [x] /启用 xxx - [x] /禁用 xxx diff --git a/dyloader/scan.go b/dyloader/scan.go new file mode 100644 index 00000000..ea425cac --- /dev/null +++ b/dyloader/scan.go @@ -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 +} diff --git a/dyloader/winign.go b/dyloader/winign.go new file mode 100644 index 00000000..f47b5180 --- /dev/null +++ b/dyloader/winign.go @@ -0,0 +1,4 @@ +//go:build windows +// +build windows + +package dyloader diff --git a/main.go b/main.go index 0ad4876b..49aa88ed 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( // 下列插件可与 wdvxdr1123/ZeroBot v1.1.2 以上配合单独使用 // 插件控制 - _ "github.com/FloatTech/ZeroBot-Plugin/control/web" // web 后端控制 + //_ "github.com/FloatTech/ZeroBot-Plugin/control/web" // web 后端控制 // 词库类 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_atri" // ATRI词库 @@ -51,6 +51,7 @@ import ( "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/driver" + //_ "github.com/FloatTech/ZeroBot-Plugin/dyloader" ) var (