feat: 重构消息处理模块,引入统一的消息接口和类型安全的消息解析
This commit is contained in:
39
message/reply.go
Normal file
39
message/reply.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 回复消息结构体
|
||||
type ReplyMessage struct {
|
||||
Type string `json:"type"`
|
||||
Data ReplyData `json:"data"`
|
||||
}
|
||||
|
||||
// 回复消息数据结构体
|
||||
type ReplyData struct {
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
func (msg *ReplyMessage) ToCQString() string {
|
||||
return fmt.Sprintf(`\[CQ:reply,id=%d\]`, msg.Data.ID)
|
||||
}
|
||||
|
||||
func (msg *ReplyMessage) ParseMessage(data string) error {
|
||||
// 使用正则表达式提取ID
|
||||
re := regexp.MustCompile(`\[CQ:reply,id=(\d+)\]`)
|
||||
matches := re.FindStringSubmatch(data)
|
||||
if len(matches) < 2 {
|
||||
return fmt.Errorf("数据格式不正确")
|
||||
}
|
||||
|
||||
id, _ := strconv.Atoi(matches[1])
|
||||
|
||||
// 返回解析后的结构体
|
||||
msg.Data = ReplyData{
|
||||
ID: id,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user