go-bot/utils/router.go
liyp f160de4320 feat(readme): 更新项目介绍和部署指南
更新了README,加入了使用Go语言重新实现sihuan/XZZ机器人项目的介绍。由于原项目使用的go-cqhttp不再维护,本项目转向使用napcat实现。同时,更新了部署服务的步骤和配置文件示例,方便用户进行部署和使用。
2024-07-20 15:49:03 +08:00

63 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"fmt"
"go-bot/config"
"go-bot/workers"
"regexp"
"strings"
)
func Router(data map[string]interface{}) {
// 输出格式化后的JSON
// fmt.Println(string(jsonString))
// 读取字段值
uid := data["user_id"].(float64)
gid := data["group_id"].(float64)
sender := data["sender"].(map[string]interface{})
role := sender["role"].(string)
mid := data["message_id"].(float64)
//包含发送消息的'!'
raw_msg := data["raw_message"].(string)
// 匹配回复消息
if strings.HasPrefix(raw_msg, "[CQ:reply,id=") {
pattern := `^\[CQ:reply,id=(-?\d+)\]`
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(raw_msg)
if len(matches) > 0 {
fullMatch := matches[0]
raw_msg = re.ReplaceAllString(raw_msg, "")
raw_msg = raw_msg + " " + fullMatch
}
}
prefix, err := config.GetConfig()["Prefix"].(string)
if !err {
panic("Prefix设置异常")
}
// fmt.Println("raw_msg:", string(raw_msg))
if len(raw_msg) > 1 && string(raw_msg[0]) == prefix {
// 去除'!'
raw_msg = raw_msg[1:]
parms := strings.Fields(raw_msg)
worker := workers.NewWorker(parms, fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
// fmt.Println("router:", parms[0])
// fmt.Println("CheckPermission:", worker.CheckPermission())
// ans := NewStdAns(parms[0], fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
message := worker.CheckPermission()
if message == "ok" {
message = worker.GetMsg()
worker.SendMsg(message)
} else {
println("权限校验失败")
}
}
}