Skip to content

Commit

Permalink
fix cq code
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoshinonyaruko committed Nov 3, 2023
1 parent 530ff96 commit 155a701
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions handlers/message_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"

"github.com/hoshinonyaruko/gensokyo/callapi"
Expand Down Expand Up @@ -160,15 +161,19 @@ func parseMessageContent(paramsMessage callapi.ParamsContent) (string, map[strin

// at处理和链接处理
func transformMessageText(messageText string) string {
// 首先,将AppID替换为BotID
messageText = strings.ReplaceAll(messageText, AppID, BotID)

// 使用正则表达式来查找所有[CQ:at,qq=数字]的模式
re := regexp.MustCompile(`\[CQ:at,qq=(\d+)\]`)
messageText = re.ReplaceAllStringFunc(messageText, func(m string) string {
submatches := re.FindStringSubmatch(m)
if len(submatches) > 1 {
realUserID, err := idmap.RetrieveRowByIDv2(submatches[1])
if err != nil {
// 如果出错,也替换成相应的格式,但使用原始QQ号
mylog.Printf("Error retrieving user ID: %v", err)
return m // 如果出错,返回原始匹配
return "<@!" + submatches[1] + ">"
}
return "<@!" + realUserID + ">"
}
Expand Down Expand Up @@ -208,11 +213,21 @@ func RevertTransformedText(data interface{}) string {

// 使用正则表达式来查找所有<@!数字>的模式
re := regexp.MustCompile(`<@!(\d+)>`)
// 使用正则表达式来替换找到的模式为[CQ:at,qq=数字]
// 使用正则表达式来替换找到的模式为[CQ:at,qq=用户ID]
messageText = re.ReplaceAllStringFunc(messageText, func(m string) string {
submatches := re.FindStringSubmatch(m)
if len(submatches) > 1 {
return "[CQ:at,qq=" + submatches[1] + "]"
//映射用户id
userID64, err := idmap.StoreIDv2(submatches[1])
if err != nil {
//如果储存失败(数据库损坏)返回原始值
mylog.Printf("Error storing ID: %v", err)
return "[CQ:at,qq=" + submatches[1] + "]"
}
//类型转换
userIDStr := strconv.FormatInt(userID64, 10)
//经过转换的cq码
return "[CQ:at,qq=" + userIDStr + "]"
}
return m
})
Expand Down

0 comments on commit 155a701

Please sign in to comment.