Compare commits
3 Commits
cdc0316c52
...
5659914298
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5659914298 | ||
![]() |
8e604fd397 | ||
![]() |
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,8 +62,6 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
|||||||
wsd := runtime.NewObject()
|
wsd := 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)
|
||||||
@ -66,8 +70,8 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
|||||||
_ = wbaObj.Set("WithLicense", wba.WithLicense)
|
_ = wbaObj.Set("WithLicense", wba.WithLicense)
|
||||||
_ = wbaObj.Set("WithAppType", wba.WithAppType)
|
_ = wbaObj.Set("WithAppType", wba.WithAppType)
|
||||||
_ = wbaObj.Set("WithRule", wba.WithRule)
|
_ = wbaObj.Set("WithRule", wba.WithRule)
|
||||||
_ = wbaObj.Set("WSP", wsp)
|
_ = wbaObj.Set("wsp", wsp)
|
||||||
_ = wbaObj.Set("WSD", wsd)
|
_ = wbaObj.Set("wsd", wsd)
|
||||||
_ = wsp.Set("UnsafelySendMsg", AppApi.UnsafelySendMsg)
|
_ = wsp.Set("UnsafelySendMsg", AppApi.UnsafelySendMsg)
|
||||||
_ = wsp.Set("UnsafelySendPrivateMsg", AppApi.UnsafelySendPrivateMsg)
|
_ = wsp.Set("UnsafelySendPrivateMsg", AppApi.UnsafelySendPrivateMsg)
|
||||||
_ = wsp.Set("UnsafelySendGroupMsg", AppApi.UnsafelySendGroupMsg)
|
_ = wsp.Set("UnsafelySendGroupMsg", AppApi.UnsafelySendGroupMsg)
|
||||||
@ -182,8 +186,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 {
|
||||||
@ -207,3 +213,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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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 {
|
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 {
|
||||||
|
_, ok := cmdList[cmd]
|
||||||
if ok {
|
if ok {
|
||||||
LOG.Debug("执行命令:%v %v", cmd, args)
|
LOG.Debug("执行命令:%v %v", cmd, args)
|
||||||
CmdMap[cmd].SOLVE(args, msg)
|
cmdList[cmd].Solve(args, msg)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: 处理消息内容
|
// TODO: 处理消息内容
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,11 +65,12 @@ 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 {
|
||||||
|
for cmd := range CmdMap[cmdList] {
|
||||||
if strings.HasPrefix(text, cmd) {
|
if strings.HasPrefix(text, cmd) {
|
||||||
text = strings.TrimPrefix(text, cmd)
|
text = strings.TrimPrefix(text, cmd)
|
||||||
text = strings.TrimPrefix(text, " ")
|
text = strings.TrimPrefix(text, " ")
|
||||||
@ -75,6 +79,7 @@ func CmdSplit(msg wba.MessageEventInfo) (string, []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return "", []string{}
|
return "", []string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,3 +15,20 @@ 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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
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,
|
||||||
@ -479,9 +480,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