diff --git a/core/app_admin.go b/core/app_admin.go index 2ee5df7..e4ae3da 100644 --- a/core/app_admin.go +++ b/core/app_admin.go @@ -2,15 +2,21 @@ package core import ( "ProjectWIND/LOG" + "ProjectWIND/typed" "ProjectWIND/wba" "github.com/dop251/goja" "os" "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) { + // 清空AppMap和CmdMap + CmdMap = make([]map[string]wba.Cmd, 4) + AppMap = make(map[typed.AppKey]wba.AppInfo) appsDir := "./data/app/" appFiles, err := os.ReadDir(appsDir) total = 0 @@ -25,7 +31,7 @@ func ReloadApps() (total int, success int) { total += totalDelta success += successDelta } - CmdMap = mergeMaps(CmdMap, AppCore.CmdMap) + CmdMap[0] = AppCore.CmdMap return total, success } @@ -56,8 +62,6 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i wsd := runtime.NewObject() _ = runtime.Set("wba", wbaObj) _ = wbaObj.Set("NewApp", wba.NewApp) - _ = wbaObj.Set("NewCmd", wba.NewCmd) - _ = wbaObj.Set("NewScheduledTask", wba.NewScheduledTask) _ = wbaObj.Set("WithName", wba.WithName) _ = wbaObj.Set("WithAuthor", wba.WithAuthor) _ = wbaObj.Set("WithVersion", wba.WithVersion) @@ -182,8 +186,10 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i 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 { @@ -207,3 +213,19 @@ func mergeMaps(map1, map2 map[string]wba.Cmd) map[string]wba.Cmd { } 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 +} diff --git a/core/app_core.go b/core/app_core.go index 402a7db..964a4fb 100644 --- a/core/app_core.go +++ b/core/app_core.go @@ -21,7 +21,7 @@ func (app *AppInfo) Run(cmd string, args []string, msg wba.MessageEventInfo) err if !ok { return errors.New("cmd not found") } - app.CmdMap[cmd].SOLVE(args, msg) + app.CmdMap[cmd].Solve(args, msg) return nil } @@ -39,9 +39,9 @@ func (app *AppInfo) GetCmd() map[string]wba.Cmd { func NewCmd(name string, help string, solve func(args []string, msg wba.MessageEventInfo)) wba.Cmd { return wba.Cmd{ - NAME: name, - DESC: help, - SOLVE: solve, + Name: name, + Desc: help, + Solve: solve, } } diff --git a/core/events_handler.go b/core/events_handler.go index 66c701b..37b721b 100644 --- a/core/events_handler.go +++ b/core/events_handler.go @@ -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) - //如果消息文本内容为bot,发送框架信息。 cmd, args := CmdSplit(msg) - _, ok := CmdMap[cmd] - if ok { - LOG.Debug("执行命令:%v %v", cmd, args) - CmdMap[cmd].SOLVE(args, msg) + for _, cmdList := range CmdMap { + _, ok := cmdList[cmd] + if ok { + LOG.Debug("执行命令:%v %v", cmd, args) + cmdList[cmd].Solve(args, msg) + break + } } + // TODO: 处理消息内容 } @@ -62,15 +65,17 @@ func CmdSplit(msg wba.MessageEventInfo) (string, []string) { return "", []string{} } } - //检查有无application.CmdList中的命令前缀 + //检查有无application.CmdMap中的命令前缀 for _, prefix := range cmdPrefix { if strings.HasPrefix(text, prefix) { text = strings.TrimPrefix(text, prefix) - for cmd := range CmdMap { - if strings.HasPrefix(text, cmd) { - text = strings.TrimPrefix(text, cmd) - text = strings.TrimPrefix(text, " ") - return cmd, strings.Split(text, " ") + for cmdList := range CmdMap { + for cmd := range CmdMap[cmdList] { + if strings.HasPrefix(text, cmd) { + text = strings.TrimPrefix(text, cmd) + text = strings.TrimPrefix(text, " ") + return cmd, strings.Split(text, " ") + } } } } diff --git a/typed/typed.go b/typed/typed.go index 929ed8c..0f621d7 100644 --- a/typed/typed.go +++ b/typed/typed.go @@ -15,3 +15,10 @@ type Protocol struct { Token string `json:"token"` 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"` +} diff --git a/wba/wind.go b/wba/wind.go index d6d58a0..7606833 100644 --- a/wba/wind.go +++ b/wba/wind.go @@ -461,15 +461,16 @@ func NewApp(opts ...AppInfoOption) AppInfo { 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{ - NAME: name, - DESC: description, - SOLVE: solve, + Name: name, + Desc: description, + 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{ Name: name, Desc: description, @@ -479,9 +480,10 @@ func NewScheduledTask(name string, description string, cron string, task func()) } type Cmd struct { - NAME string - DESC string - SOLVE func(args []string, msg MessageEventInfo) + Name string + Desc string + Solve func(args []string, msg MessageEventInfo) + Rule string } type MessageEventInfo struct {