Skip to content

Commit

Permalink
Merge pull request #3 from BoyChai/plugin_adaptation
Browse files Browse the repository at this point in the history
Add plugin adaptation
  • Loading branch information
BoyChai authored Oct 13, 2022
2 parents 43cf23a + 0a1ad6b commit 9904903
Show file tree
Hide file tree
Showing 16 changed files with 350 additions and 15 deletions.
24 changes: 22 additions & 2 deletions action.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package CoralBot

import (
"fmt"
"github.com/dullgiulio/pingo"
)

// Task 创建一个任务
type Task struct {
Condition []Condition
Run func()
Condition []Condition
Run func()
plugin bool
pingoServer *pingo.Plugin
RunName string
info PluginInfo
}

// Condition 触发条件
Expand All @@ -17,5 +26,16 @@ var Tasks []Task

// NewTask 创建一个动作
func NewTask(task Task) {
task.plugin = false
Tasks = append(Tasks, task)
}

// NewPluginTask 创建一个插件动作
func NewPluginTask(task Task) {
task.plugin = true
if task.RunName == "" {
fmt.Println("创建插件动作错误:RunName值为空")
return
}
Tasks = append(Tasks, task)
}
108 changes: 108 additions & 0 deletions docs/Plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 插件相关

## 编写插件

```go
package main

import (
"github.com/BoyChai/CoralBot"
"github.com/dullgiulio/pingo"
)

var H CoralBot.Handle

var Info CoralBot.PluginInfo

type MyPlugin struct{}

func (p *MyPlugin) PluginInfo(n string, info *CoralBot.PluginInfo) error {
*info = Info
return nil
}

func (p *MyPlugin) GetPlugin(e *CoralBot.Event, Task *[]CoralBot.Task) error {
CoralBot.NewPluginTask(CoralBot.Task{
Condition: []CoralBot.Condition{
{
Key: &e.Message,
Value: "插件测试",
},
},
RunName: "MyPlugin.Run1",
})
*Task = CoralBot.Tasks
return nil
}

func (p *MyPlugin) Run1(e CoralBot.Event, n *string) error {
h := CoralBot.Handle{
Host: "127.0.0.1:5700",
Agreement: "http",
}
h.Reply(e, CoralBot.Msg{
Message: "插件激活成功",
})
return nil
}
func main() {
H.Host = "127.0.0.1:5700"
H.Agreement = "http"
Info = CoralBot.PluginInfo{
Name: "test_demo",
Summary: "测试插件demo",
Version: "v0.0.1",
Developer: "BoyChai",
Email: "[email protected]",
}

plugin := &MyPlugin{}

pingo.Register(plugin)
pingo.Run()
}
```

编写需注意:

- 插件结构体必须叫做"MyPlugin"
- 必须拥有以下方法
- PluginInfo:用来获取插件信息
- GetPlugin:用来获取插件任务
- Task.RunName的值应该为"MyPlugin.方法"
- 当任务触发成功之后就会执行Task.RunName指定的方法
- 编译之后后缀需要改为coral
- 插件的动作执行器(Handle)需要自己创建,目前不可以使用主程序的

## 使用插件

把编译好的插件放入"./plugin"文件夹,之后代码如下

```go
package main

import (
"github.com/BoyChai/CoralBot"
)

func main() {
var e CoralBot.Event
CoralBot.RunCoralBot(":8080", &e)
}
```

运行之后会进行加载插件,并输出插件信息

```bash
2022/10/13 17:43:16 tcp
===============已加载插件===============
插件名称: test_demo
插件版本: v0.0.1
插件概述: 测试插件demo
插件作者: BoyChai
作者邮箱: [email protected]
```

运行之后执行,发送消息"插件测试"即可,效果如下:

![效果](./img/20221013173153.png)
13 changes: 7 additions & 6 deletions docs/_navbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
* GUIDE
* [简介](/INFO)
* [快速开始](/Start)
* [插件相关](/Plugin)
* [FAQ](/FAQ)
* [go-cqhttp](https://docs.go-cqhttp.org)
* [GitHub](https://github.com/BoyChai/CoralBot)
[//]: # (* Hanlder)
[//]: # "* Hanlder"

[//]: # ( * [常规行为](/Hanlder))
[//]: # " * [常规行为](/Hanlder)"

[//]: # ( * [频道行为](/ChannelHanlder))
[//]: # " * [频道行为](/ChannelHanlder)"

[//]: # (* Event)
[//]: # "* Event"

[//]: # ( * [常规事件](/Event))
[//]: # " * [常规事件](/Event)"

[//]: # ( * [频道事件](/ChannelEvent))
[//]: # " * [频道事件](/ChannelEvent)"
Binary file added docs/img/20221013173153.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 65 additions & 4 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type ChannelInfo struct {
}

// explain 解析命令函数
// 将任务以此拿出来进行判断
func (e *Event) explain(bodyData []byte) {
for i := 0; i < len(Tasks); i++ {
task := Tasks[i]
Expand All @@ -117,20 +118,32 @@ func (e *Event) explain(bodyData []byte) {
if err != nil {
fmt.Println("command parsing error,please feedback to the developer.error:", err)
}
status := e.filterStart(task)
if status == nil {
return
// 判断是否为插件
if task.plugin {
status := e.pluginFilterStart(task)
// 返回值如果等于空则代此事件已经达成了任务条件并已经执行
if status == nil {
return
}
} else {
status := e.filterStart(task)
if status == nil {
// 返回值如果等于空则代此事件已经达成了任务条件并已经执行
return
}
}

}
}

// 过滤
func (e *Event) filterStart(task Task) error {
for t := 1; t <= len(task.Condition); t++ {
//fmt.Println(*task.Condition[t-1].Key.(reflect.TypeOf(task.Condition[t-1].Key))
conditionKey, _ := e.typeAsserts(task.Condition[t-1].Key)
// 如果这是此任务的最后一个判断
if t == len(task.Condition) {
if task.Condition[t-1].Regex == true {
// 正则判断
key, _ := regexp.MatchString(task.Condition[t-1].Value, fmt.Sprint(conditionKey))
if key {
task.Run()
Expand All @@ -155,6 +168,54 @@ func (e *Event) filterStart(task Task) error {
return errors.New("1")
}

func (e *Event) pluginFilterStart(task Task) error {
var pts []Task
//fmt.Println("错误信息为", task.pingoServer)
err := task.pingoServer.Call("MyPlugin.GetPlugin", &e, &pts)
if err != nil {
fmt.Println("插件任务获取失败:插件名称", task.info.Name, "\n", "错误信息为:", err)
} else {
// 判断插件条件
// 依次判断任务
for ti := 0; ti < len(pts); ti++ {
// 依次判断任务条件
for ci := 0; ci < len(pts[ti].Condition); ci++ {
if ci+1 == len(pts[ti].Condition) {
// 正则判断
if pts[ti].Condition[ci].Regex == true {
key, _ := regexp.MatchString(pts[ti].Condition[ci].Value, fmt.Sprint(pts[ti].Condition[ci].Key))
if key {
err := task.pingoServer.Call(pts[ti].RunName, e, nil)
if err != nil {
fmt.Println("插件任务调用错误:", err)
}
return nil
}
}
if fmt.Sprint(pts[ti].Condition[ci].Key) == pts[ti].Condition[ci].Value {
err := task.pingoServer.Call(pts[ti].RunName, e, nil)
if err != nil {
fmt.Println("插件任务调用错误:", err)
}
return nil
}
}
if pts[ti].Condition[ci].Regex == true {
key, _ := regexp.MatchString(pts[ti].Condition[ci].Value, fmt.Sprint(pts[ti].Condition[ci].Key))
if key != true {
return errors.New("1")
}
}
if fmt.Sprint(pts[ti].Condition[ci].Key) != pts[ti].Condition[ci].Value {
return errors.New("1")
}
}
}
return errors.New("1")
}
return errors.New("1")
}

// 类型断言
func (e *Event) typeAsserts(key interface{}) (interface{}, error) {
switch key.(type) {
Expand Down
3 changes: 1 addition & 2 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func main() {
coral.NewTask(coral.Task{
Condition: c1,
Run: func() {
h.SendMsg(coral.Msg{
GroupId: e.GroupID,
h.Reply(e, coral.Msg{
Message: "你好",
})
},
Expand Down
Binary file added example/plugin/use/plugin/write.coral
Binary file not shown.
10 changes: 10 additions & 0 deletions example/plugin/use/use.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/BoyChai/CoralBot"
)

func main() {
var e CoralBot.Event
CoralBot.RunCoralBot(":8080", &e)
}
Binary file added example/plugin/write/write.coral
Binary file not shown.
Binary file added example/plugin/write/write.exe
Binary file not shown.
58 changes: 58 additions & 0 deletions example/plugin/write/write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"github.com/BoyChai/CoralBot"
"github.com/dullgiulio/pingo"
)

var H CoralBot.Handle

var Info CoralBot.PluginInfo

type MyPlugin struct{}

func (p *MyPlugin) PluginInfo(n string, info *CoralBot.PluginInfo) error {
*info = Info
return nil
}

func (p *MyPlugin) GetPlugin(e *CoralBot.Event, Task *[]CoralBot.Task) error {
CoralBot.NewPluginTask(CoralBot.Task{
Condition: []CoralBot.Condition{
{
Key: &e.Message,
Value: "插件测试",
},
},
RunName: "MyPlugin.Run1",
})
*Task = CoralBot.Tasks
return nil
}

func (p *MyPlugin) Run1(e CoralBot.Event, n *string) error {
h := CoralBot.Handle{
Host: "127.0.0.1:5700",
Agreement: "http",
}
h.Reply(e, CoralBot.Msg{
Message: "插件激活成功",
})
return nil
}
func main() {
H.Host = "127.0.0.1:5700"
H.Agreement = "http"
Info = CoralBot.PluginInfo{
Name: "test_demo",
Summary: "测试插件demo",
Version: "v0.0.1",
Developer: "BoyChai",
Email: "[email protected]",
}

plugin := &MyPlugin{}

pingo.Register(plugin)
pingo.Run()
}
9 changes: 8 additions & 1 deletion gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func RunCoralBot(port string, e *Event) {
e,
)
}))

// 加载插件
err := e.loadPlugin()
if err != nil {
fmt.Println("插件加载失败:", err)
}
// 接收上报
g.POST("/", func(c *gin.Context) {
dataReader := c.Request.Body
bodyData, err := ioutil.ReadAll(dataReader)
Expand All @@ -43,7 +50,7 @@ func RunCoralBot(port string, e *Event) {
//e.bodyData = bodyData
e.explain(bodyData)
})
err := g.Run(port)
err = g.Run(port)
if err != nil {
fmt.Printf("gin:%v", err)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/BoyChai/CoralBot
go 1.18

require (
github.com/dullgiulio/pingo v0.0.0-20151111190101-8b1949e35b5a
github.com/gin-gonic/gin v1.8.1
github.com/tidwall/gjson v1.14.3
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dullgiulio/pingo v0.0.0-20151111190101-8b1949e35b5a h1:hxUGjnGTjWlrH+mG7wqwJYKtmKcZmmSYCjSf+nWgeTc=
github.com/dullgiulio/pingo v0.0.0-20151111190101-8b1949e35b5a/go.mod h1:M1VZSWV1CHnqoST00aa9Ha4VL5btiDWraNL0yQwpUc8=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
Expand Down
Loading

0 comments on commit 9904903

Please sign in to comment.