2024-04-01 09:42:29 +08:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2024-09-22 20:39:44 +08:00
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
"fmt"
|
2024-07-20 15:49:03 +08:00
|
|
|
|
"go-bot/config"
|
2024-09-22 20:39:44 +08:00
|
|
|
|
"go-bot/tools"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
"go-bot/workers"
|
2024-07-14 21:38:39 +08:00
|
|
|
|
"regexp"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
"strings"
|
2024-09-22 20:39:44 +08:00
|
|
|
|
"time"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-09-22 20:39:44 +08:00
|
|
|
|
type GroupMessage struct {
|
|
|
|
|
GroupID float64 `json:"group_id"`
|
|
|
|
|
UserID float64 `json:"user_id"`
|
|
|
|
|
Message string `json:"raw_message"`
|
|
|
|
|
Role string `json:"role"`
|
|
|
|
|
Time string `json:"time"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 09:42:29 +08:00
|
|
|
|
func Router(data map[string]interface{}) {
|
|
|
|
|
|
|
|
|
|
// 输出格式化后的JSON
|
2024-04-01 11:37:03 +08:00
|
|
|
|
// fmt.Println(string(jsonString))
|
2024-04-01 09:42:29 +08:00
|
|
|
|
// 读取字段值
|
|
|
|
|
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)
|
2024-10-20 16:29:19 +08:00
|
|
|
|
|
|
|
|
|
// 将消息存入redis
|
2024-09-22 20:39:44 +08:00
|
|
|
|
redisClient := tools.GetRedisClient()
|
|
|
|
|
if redisClient != nil {
|
|
|
|
|
var group_message GroupMessage
|
|
|
|
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
group_message.GroupID = gid
|
|
|
|
|
group_message.UserID = uid
|
|
|
|
|
group_message.Message = data["raw_message"].(string)
|
|
|
|
|
group_message.Role = role
|
|
|
|
|
group_message.Time = (time.Unix(int64(data["time"].(float64)), 0).In(loc)).Format("2006-01-02 15:04:05")
|
|
|
|
|
jsonString, err := json.Marshal(group_message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-10-20 20:58:25 +08:00
|
|
|
|
key := fmt.Sprintf("group_message:%d:%d", int64(gid), int64(uid))
|
|
|
|
|
exists, err := tools.CheckKeyExists(key)
|
2024-09-22 20:39:44 +08:00
|
|
|
|
if err != nil || !exists {
|
2024-10-20 16:29:19 +08:00
|
|
|
|
redisClient.RPush(context.Background(), key, jsonString, 0)
|
2024-09-22 20:39:44 +08:00
|
|
|
|
// tools.SetValue(key, string(jsonString), 10*time.Second)
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
tools.AddToContext(key, jsonString, 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-02 16:37:26 +08:00
|
|
|
|
|
2024-04-20 12:49:16 +08:00
|
|
|
|
//包含发送消息的'!'
|
|
|
|
|
raw_msg := data["raw_message"].(string)
|
2024-07-14 21:38:39 +08:00
|
|
|
|
|
|
|
|
|
// 匹配回复消息
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-20 15:49:03 +08:00
|
|
|
|
prefix, err := config.GetConfig()["Prefix"].(string)
|
|
|
|
|
if !err {
|
2024-09-01 14:28:52 +08:00
|
|
|
|
panic("Prefix参数设置异常!")
|
2024-07-14 21:38:39 +08:00
|
|
|
|
|
2024-07-20 15:49:03 +08:00
|
|
|
|
}
|
|
|
|
|
// fmt.Println("raw_msg:", string(raw_msg))
|
2024-10-20 16:29:19 +08:00
|
|
|
|
if len(raw_msg) > 1 {
|
|
|
|
|
if string(raw_msg[0]) == prefix {
|
|
|
|
|
// 去除'!'
|
|
|
|
|
raw_msg = raw_msg[1:]
|
|
|
|
|
parms := strings.Fields(raw_msg)
|
2024-04-20 12:49:16 +08:00
|
|
|
|
|
2024-10-20 16:29:19 +08:00
|
|
|
|
worker := workers.NewWorker(parms, fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
|
2024-04-20 12:49:16 +08:00
|
|
|
|
|
2024-10-20 16:29:19 +08:00
|
|
|
|
// 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" {
|
2024-07-02 13:21:29 +08:00
|
|
|
|
|
2024-10-20 16:29:19 +08:00
|
|
|
|
message = worker.GetMsg()
|
|
|
|
|
worker.SendMsg(message)
|
2024-04-20 12:49:16 +08:00
|
|
|
|
|
2024-10-20 16:29:19 +08:00
|
|
|
|
} else {
|
|
|
|
|
println("权限校验失败")
|
|
|
|
|
}
|
2024-07-02 13:21:29 +08:00
|
|
|
|
} else {
|
2024-10-20 16:29:19 +08:00
|
|
|
|
// 实现触发器功能
|
|
|
|
|
|
2024-04-20 12:49:16 +08:00
|
|
|
|
}
|
2024-04-02 20:29:25 +08:00
|
|
|
|
}
|
2024-04-01 09:42:29 +08:00
|
|
|
|
|
|
|
|
|
}
|