Skip to content

Commit 7e66086

Browse files
committed
reply
1 parent f748f77 commit 7e66086

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

service/bot/proto2mirai.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/Mrs4s/MiraiGo/message"
88
"github.com/ProtobufBot/Go-Mirai-Client/pkg/util"
99
"github.com/ProtobufBot/Go-Mirai-Client/proto_gen/onebot"
10+
"github.com/ProtobufBot/Go-Mirai-Client/service/cache"
1011
log "github.com/sirupsen/logrus"
1112
)
1213

@@ -16,6 +17,7 @@ func EmptyText() *message.TextElement {
1617

1718
// 消息列表,不自动把code变成msg
1819
func ProtoMsgToMiraiMsg(msgList []*onebot.Message, notConvertText bool) []message.IMessageElement {
20+
containReply := false // 每条消息只能包含一个reply
1921
messageChain := make([]message.IMessageElement, 0)
2022
for _, protoMsg := range msgList {
2123
switch protoMsg.Type {
@@ -46,6 +48,11 @@ func ProtoMsgToMiraiMsg(msgList []*onebot.Message, notConvertText bool) []messag
4648
messageChain = append(messageChain, ProtoLightAppToMiraiLightApp(protoMsg.Data))
4749
case "service":
4850
messageChain = append(messageChain, ProtoServiceToMiraiService(protoMsg.Data))
51+
case "reply":
52+
if replyElement := ProtoReplyToMiraiReply(protoMsg.Data); replyElement != nil && !containReply {
53+
containReply = true
54+
messageChain = append([]message.IMessageElement{replyElement}, messageChain...)
55+
}
4956
default:
5057
log.Errorf("不支持的消息类型 %+v", protoMsg)
5158
}
@@ -195,3 +202,40 @@ func ProtoServiceToMiraiService(data map[string]string) message.IMessageElement
195202
SubType: subType,
196203
}
197204
}
205+
206+
func ProtoReplyToMiraiReply(data map[string]string) *message.ReplyElement {
207+
messageIdStr, ok := data["message_id"]
208+
if !ok {
209+
return nil
210+
}
211+
messageIdInt, err := strconv.Atoi(messageIdStr)
212+
if err != nil {
213+
return nil
214+
}
215+
messageId := int32(messageIdInt)
216+
eventInterface, ok := cache.GroupMessageLru.Get(messageId)
217+
if ok {
218+
groupMessage, ok := eventInterface.(*message.GroupMessage)
219+
if ok {
220+
return &message.ReplyElement{
221+
ReplySeq: groupMessage.Id,
222+
Sender: groupMessage.Sender.Uin,
223+
Time: groupMessage.Time,
224+
Elements: groupMessage.Elements,
225+
}
226+
}
227+
}
228+
eventInterface, ok = cache.PrivateMessageLru.Get(messageId)
229+
if ok {
230+
privateMessage, ok := eventInterface.(*message.PrivateMessage)
231+
if ok {
232+
return &message.ReplyElement{
233+
ReplySeq: privateMessage.Id,
234+
Sender: privateMessage.Sender.Uin,
235+
Time: privateMessage.Time,
236+
Elements: privateMessage.Elements,
237+
}
238+
}
239+
}
240+
return nil
241+
}

service/bot/raw2mirai.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Node struct {
1818
var re = regexp.MustCompile("<[\\s\\S]+?/>")
1919

2020
func RawMsgToMiraiMsg(str string) []message.IMessageElement {
21+
containReply := false
2122
var node Node
2223
textList := re.Split(str, -1)
2324
codeList := re.FindAllString(str, -1)
@@ -63,6 +64,11 @@ func RawMsgToMiraiMsg(str string) []message.IMessageElement {
6364
elemList = append(elemList, ProtoLightAppToMiraiLightApp(attrMap))
6465
case "service":
6566
elemList = append(elemList, ProtoServiceToMiraiService(attrMap))
67+
case "reply":
68+
if replyElement := ProtoReplyToMiraiReply(attrMap); replyElement != nil && !containReply {
69+
containReply = true
70+
elemList = append([]message.IMessageElement{replyElement}, elemList...)
71+
}
6672
default:
6773
log.Warnf("不支持的类型 %s", code)
6874
elemList = append(elemList, message.NewText(code))

0 commit comments

Comments
 (0)