Compare commits
3 Commits
cdc0316c52
...
5659914298
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5659914298 | ||
![]() |
8e604fd397 | ||
![]() |
5a5c3a4c48 |
@ -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)
|
||||
@ -66,8 +70,8 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
_ = wbaObj.Set("WithLicense", wba.WithLicense)
|
||||
_ = wbaObj.Set("WithAppType", wba.WithAppType)
|
||||
_ = wbaObj.Set("WithRule", wba.WithRule)
|
||||
_ = wbaObj.Set("WSP", wsp)
|
||||
_ = wbaObj.Set("WSD", wsd)
|
||||
_ = wbaObj.Set("wsp", wsp)
|
||||
_ = wbaObj.Set("wsd", wsd)
|
||||
_ = wsp.Set("UnsafelySendMsg", AppApi.UnsafelySendMsg)
|
||||
_ = wsp.Set("UnsafelySendPrivateMsg", AppApi.UnsafelySendPrivateMsg)
|
||||
_ = wsp.Set("UnsafelySendGroupMsg", AppApi.UnsafelySendGroupMsg)
|
||||
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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, " ")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,3 +15,20 @@ 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"`
|
||||
}
|
||||
|
||||
type SessionWorkSpace struct {
|
||||
SessionId string `json:"session_id"`
|
||||
SessionType string `json:"session_type"`
|
||||
Rule string `json:"rule"`
|
||||
Enable bool `json:"enable"`
|
||||
AppEnable map[AppKey]bool `json:"app_enable"`
|
||||
CmdEnable map[string]bool `json:"cmd_enable"`
|
||||
WorkLevel int32 `json:"work_level"`
|
||||
}
|
||||
|
18
wba/wind.go
18
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user