forked from ProjectWIND/ProjectWIND
优化了插件加载管理,修改了部分插件规范
This commit is contained in:
parent
c63f4c2dd8
commit
5a5c3a4c48
@ -2,15 +2,21 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"ProjectWIND/LOG"
|
"ProjectWIND/LOG"
|
||||||
|
"ProjectWIND/typed"
|
||||||
"ProjectWIND/wba"
|
"ProjectWIND/wba"
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var CmdMap = make(map[string]wba.Cmd)
|
var CmdMap = make([]map[string]wba.Cmd, 4)
|
||||||
|
var AppMap = make(map[typed.AppKey]wba.AppInfo)
|
||||||
|
|
||||||
func ReloadApps() (total int, success int) {
|
func ReloadApps() (total int, success int) {
|
||||||
|
// 清空AppMap和CmdMap
|
||||||
|
CmdMap = make([]map[string]wba.Cmd, 4)
|
||||||
|
AppMap = make(map[typed.AppKey]wba.AppInfo)
|
||||||
appsDir := "./data/app/"
|
appsDir := "./data/app/"
|
||||||
appFiles, err := os.ReadDir(appsDir)
|
appFiles, err := os.ReadDir(appsDir)
|
||||||
total = 0
|
total = 0
|
||||||
@ -25,7 +31,7 @@ func ReloadApps() (total int, success int) {
|
|||||||
total += totalDelta
|
total += totalDelta
|
||||||
success += successDelta
|
success += successDelta
|
||||||
}
|
}
|
||||||
CmdMap = mergeMaps(CmdMap, AppCore.CmdMap)
|
CmdMap[0] = AppCore.CmdMap
|
||||||
return total, success
|
return total, success
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,8 +61,6 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
|||||||
wsp := runtime.NewObject()
|
wsp := runtime.NewObject()
|
||||||
_ = runtime.Set("wba", wbaObj)
|
_ = runtime.Set("wba", wbaObj)
|
||||||
_ = wbaObj.Set("NewApp", wba.NewApp)
|
_ = wbaObj.Set("NewApp", wba.NewApp)
|
||||||
_ = wbaObj.Set("NewCmd", wba.NewCmd)
|
|
||||||
_ = wbaObj.Set("NewScheduledTask", wba.NewScheduledTask)
|
|
||||||
_ = wbaObj.Set("WithName", wba.WithName)
|
_ = wbaObj.Set("WithName", wba.WithName)
|
||||||
_ = wbaObj.Set("WithAuthor", wba.WithAuthor)
|
_ = wbaObj.Set("WithAuthor", wba.WithAuthor)
|
||||||
_ = wbaObj.Set("WithVersion", wba.WithVersion)
|
_ = wbaObj.Set("WithVersion", wba.WithVersion)
|
||||||
@ -163,8 +167,10 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
|||||||
return 1, 0
|
return 1, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AppMap[typed.AppKey{AppName: appInfo.Name, AppType: appInfo.AppType, AppVersion: appInfo.Version, AppLevel: checkAppLevel(appInfo)}] = appInfo
|
||||||
|
cmdIndex := AppTypeToInt(appInfo.AppType)
|
||||||
// 合并命令
|
// 合并命令
|
||||||
CmdMap = mergeMaps(CmdMap, appInfo.CmdMap)
|
CmdMap[cmdIndex] = mergeMaps(CmdMap[cmdIndex], appInfo.CmdMap)
|
||||||
|
|
||||||
// 注册定时任务
|
// 注册定时任务
|
||||||
for _, task := range appInfo.ScheduledTasks {
|
for _, task := range appInfo.ScheduledTasks {
|
||||||
@ -188,3 +194,19 @@ func mergeMaps(map1, map2 map[string]wba.Cmd) map[string]wba.Cmd {
|
|||||||
}
|
}
|
||||||
return map3
|
return map3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AppTypeToInt(appType string) int32 {
|
||||||
|
appType = strings.ToLower(appType)
|
||||||
|
switch appType {
|
||||||
|
case "system":
|
||||||
|
return 1
|
||||||
|
case "rule":
|
||||||
|
return 2
|
||||||
|
default:
|
||||||
|
return 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkAppLevel(appInfo wba.AppInfo) int32 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
@ -21,7 +21,7 @@ func (app *AppInfo) Run(cmd string, args []string, msg wba.MessageEventInfo) err
|
|||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("cmd not found")
|
return errors.New("cmd not found")
|
||||||
}
|
}
|
||||||
app.CmdMap[cmd].SOLVE(args, msg)
|
app.CmdMap[cmd].Solve(args, msg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,9 +35,9 @@ func (app *AppInfo) GetCmd() map[string]wba.Cmd {
|
|||||||
|
|
||||||
func NewCmd(name string, help string, solve func(args []string, msg wba.MessageEventInfo)) wba.Cmd {
|
func NewCmd(name string, help string, solve func(args []string, msg wba.MessageEventInfo)) wba.Cmd {
|
||||||
return wba.Cmd{
|
return wba.Cmd{
|
||||||
NAME: name,
|
Name: name,
|
||||||
DESC: help,
|
Desc: help,
|
||||||
SOLVE: solve,
|
Solve: solve,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,13 +16,16 @@ func HandleMessage(msgJson []byte) {
|
|||||||
}
|
}
|
||||||
// 处理消息
|
// 处理消息
|
||||||
LOG.Info("收到消息:(来自:%v-%v:%v-%v)%v", msg.MessageType, msg.GroupId, msg.UserId, msg.Sender.Nickname, msg.RawMessage)
|
LOG.Info("收到消息:(来自:%v-%v:%v-%v)%v", msg.MessageType, msg.GroupId, msg.UserId, msg.Sender.Nickname, msg.RawMessage)
|
||||||
//如果消息文本内容为bot,发送框架信息。
|
|
||||||
cmd, args := CmdSplit(msg)
|
cmd, args := CmdSplit(msg)
|
||||||
_, ok := CmdMap[cmd]
|
for _, cmdList := range CmdMap {
|
||||||
if ok {
|
_, ok := cmdList[cmd]
|
||||||
LOG.Debug("执行命令:%v %v", cmd, args)
|
if ok {
|
||||||
CmdMap[cmd].SOLVE(args, msg)
|
LOG.Debug("执行命令:%v %v", cmd, args)
|
||||||
|
cmdList[cmd].Solve(args, msg)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: 处理消息内容
|
// TODO: 处理消息内容
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,15 +65,17 @@ func CmdSplit(msg wba.MessageEventInfo) (string, []string) {
|
|||||||
return "", []string{}
|
return "", []string{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//检查有无application.CmdList中的命令前缀
|
//检查有无application.CmdMap中的命令前缀
|
||||||
for _, prefix := range cmdPrefix {
|
for _, prefix := range cmdPrefix {
|
||||||
if strings.HasPrefix(text, prefix) {
|
if strings.HasPrefix(text, prefix) {
|
||||||
text = strings.TrimPrefix(text, prefix)
|
text = strings.TrimPrefix(text, prefix)
|
||||||
for cmd := range CmdMap {
|
for cmdList := range CmdMap {
|
||||||
if strings.HasPrefix(text, cmd) {
|
for cmd := range CmdMap[cmdList] {
|
||||||
text = strings.TrimPrefix(text, cmd)
|
if strings.HasPrefix(text, cmd) {
|
||||||
text = strings.TrimPrefix(text, " ")
|
text = strings.TrimPrefix(text, cmd)
|
||||||
return cmd, strings.Split(text, " ")
|
text = strings.TrimPrefix(text, " ")
|
||||||
|
return cmd, strings.Split(text, " ")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,3 +15,10 @@ type Protocol struct {
|
|||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Enable bool `json:"enable"`
|
Enable bool `json:"enable"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AppKey struct {
|
||||||
|
AppName string `json:"app_name"`
|
||||||
|
AppType string `json:"app_type"`
|
||||||
|
AppLevel int32 `json:"app_level"`
|
||||||
|
AppVersion string `json:"app_version"`
|
||||||
|
}
|
||||||
|
18
wba/wind.go
18
wba/wind.go
@ -483,15 +483,16 @@ func NewApp(opts ...AppInfoOption) AppInfo {
|
|||||||
return Ext
|
return Ext
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCmd(name string, description string, solve func(args []string, msg MessageEventInfo)) Cmd {
|
func (ai *AppInfo) NewCmd(name string, description string, solve func(args []string, msg MessageEventInfo)) Cmd {
|
||||||
return Cmd{
|
return Cmd{
|
||||||
NAME: name,
|
Name: name,
|
||||||
DESC: description,
|
Desc: description,
|
||||||
SOLVE: solve,
|
Solve: solve,
|
||||||
|
Rule: ai.Rule,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewScheduledTask(name string, description string, cron string, task func()) ScheduledTaskInfo {
|
func (ai *AppInfo) NewScheduledTask(name string, description string, cron string, task func()) ScheduledTaskInfo {
|
||||||
return ScheduledTaskInfo{
|
return ScheduledTaskInfo{
|
||||||
Name: name,
|
Name: name,
|
||||||
Desc: description,
|
Desc: description,
|
||||||
@ -501,9 +502,10 @@ func NewScheduledTask(name string, description string, cron string, task func())
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Cmd struct {
|
type Cmd struct {
|
||||||
NAME string
|
Name string
|
||||||
DESC string
|
Desc string
|
||||||
SOLVE func(args []string, msg MessageEventInfo)
|
Solve func(args []string, msg MessageEventInfo)
|
||||||
|
Rule string
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageEventInfo struct {
|
type MessageEventInfo struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user