mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-20 06:20:08 +08:00
✨ 增加动态加载,添加默认禁用
This commit is contained in:
parent
43735722bf
commit
173925b57a
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,6 +5,7 @@ data/manager
|
||||
data/acgimage
|
||||
data/fortune
|
||||
data/hs
|
||||
plugins/*.so
|
||||
.idea/
|
||||
.DS_Store
|
||||
.vscode
|
||||
|
||||
10
README.md
10
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
|
||||
|
||||
49
dyloader/scan.go
Normal file
49
dyloader/scan.go
Normal 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
4
dyloader/winign.go
Normal file
@ -0,0 +1,4 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package dyloader
|
||||
3
main.go
3
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 (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user