github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/message/elements.go (about)

     1  package message
     2  
     3  // from https://github.com/Mrs4s/MiraiGo/blob/master/message/elements.go
     4  
     5  import (
     6  	"os"
     7  	"strconv"
     8  )
     9  
    10  type (
    11  	TextElement struct {
    12  		Content string
    13  	}
    14  
    15  	AtElement struct {
    16  		Target  uint32
    17  		UID     string
    18  		Display string
    19  		SubType AtType
    20  	}
    21  
    22  	FaceElement struct {
    23  		FaceID      uint16
    24  		isLargeFace bool
    25  	}
    26  
    27  	ReplyElement struct {
    28  		ReplySeq int32
    29  		Sender   uint64
    30  		GroupID  uint64 // 私聊回复群聊时
    31  		Time     int32
    32  		Elements []IMessageElement
    33  	}
    34  
    35  	VoiceElement struct {
    36  		Name string
    37  		Md5  []byte
    38  		Size uint32
    39  		Url  string
    40  
    41  		// --- sending ---
    42  		Data []byte
    43  	}
    44  	ShortVideoElement struct {
    45  		Name      string
    46  		Uuid      []byte
    47  		Size      int32
    48  		ThumbSize int32
    49  		Md5       []byte
    50  		ThumbMd5  []byte
    51  		Url       string
    52  	}
    53  
    54  	AtType int
    55  )
    56  
    57  const (
    58  	AtTypeGroupMember = 0 // At群成员
    59  )
    60  
    61  func NewText(s string) *TextElement {
    62  	return &TextElement{Content: s}
    63  }
    64  
    65  func NewAt(target uint32, display ...string) *AtElement {
    66  	dis := "@" + strconv.FormatInt(int64(target), 10)
    67  	if target == 0 {
    68  		dis = "@全体成员"
    69  	}
    70  	if len(display) != 0 {
    71  		dis = display[0]
    72  	}
    73  	return &AtElement{
    74  		Target:  target,
    75  		Display: dis,
    76  	}
    77  }
    78  
    79  func NewGroupImage(data []byte) *GroupImageElement {
    80  	return &GroupImageElement{
    81  		Stream: data,
    82  	}
    83  }
    84  
    85  func NewGroupImageByFile(path string) (*GroupImageElement, error) {
    86  	data, err := os.ReadFile(path)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	return &GroupImageElement{
    91  		Stream: data,
    92  	}, nil
    93  }
    94  
    95  func (e *TextElement) Type() ElementType {
    96  	return Text
    97  }
    98  
    99  func (e *AtElement) Type() ElementType {
   100  	return At
   101  }
   102  
   103  func (e *FaceElement) Type() ElementType {
   104  	return Face
   105  }
   106  
   107  func (e *GroupImageElement) Type() ElementType {
   108  	return Image
   109  }
   110  
   111  func (e *FriendImageElement) Type() ElementType {
   112  	return Image
   113  }
   114  
   115  func (e *ReplyElement) Type() ElementType {
   116  	return Reply
   117  }
   118  
   119  func (e *VoiceElement) Type() ElementType {
   120  	return Voice
   121  }
   122  
   123  func (e *ShortVideoElement) Type() ElementType {
   124  	return Video
   125  }