From 609f523c98d2780e444b945571af855091743000 Mon Sep 17 00:00:00 2001 From: Redmomn <109732988+Redmomn@users.noreply.github.com> Date: Sat, 20 Apr 2024 11:13:46 +0800 Subject: [PATCH] fix: message element interface & sendingMessage func --- message/message.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/message/message.go b/message/message.go index 5e3d02eb..67a69a72 100644 --- a/message/message.go +++ b/message/message.go @@ -83,7 +83,6 @@ type ( IMessageElement interface { Type() ElementType - BuildElement() *message.Elem } ElementType int @@ -297,6 +296,10 @@ func NewSendingMessage() *SendingMessage { return &SendingMessage{} } +func (msg *SendingMessage) GetElems() []IMessageElement { + return msg.Elements +} + // Append 要传入msg的引用 func (msg *SendingMessage) Append(e IMessageElement) *SendingMessage { v := reflect.ValueOf(e) @@ -306,14 +309,35 @@ func (msg *SendingMessage) Append(e IMessageElement) *SendingMessage { return msg } +func (msg *SendingMessage) FirstOrNil(f func(element IMessageElement) bool) IMessageElement { + for _, elem := range msg.Elements { + if f(elem) { + return elem + } + } + return nil +} + func BuildMessageElements(msgElems []IMessageElement) (msgBody *message.MessageBody) { if len(msgElems) == 0 { return } elems := make([]*message.Elem, 0, len(msgElems)) for _, elem := range msgElems { - // TODO 涉及到uid的东西应该在这里处理一下 - elems = append(elems, elem.BuildElement()) + var pb *message.Elem + switch e := elem.(type) { + case *TextElement: + pb = e.BuildElement() + case *AtElement: + pb = e.BuildElement() + case *ReplyElement: + pb = e.BuildElement() + case *FaceElement: + pb = e.BuildElement() + default: + continue + } + elems = append(elems, pb) } msgBody = &message.MessageBody{ RichText: &message.RichText{Elems: elems},