Browse Source

Support for multiple embeds in message routes (#1000)

xIceArcher 2 years ago
parent
commit
842ddb357a
2 changed files with 60 additions and 11 deletions
  1. 17 5
      message.go
  2. 43 6
      restapi.go

+ 17 - 5
message.go

@@ -85,8 +85,7 @@ type Message struct {
 	// A list of components attached to the message.
 	Components []MessageComponent `json:"-"`
 
-	// A list of embeds present in the message. Multiple
-	// embeds can currently only be sent by webhooks.
+	// A list of embeds present in the message.
 	Embeds []*MessageEmbed `json:"embeds"`
 
 	// A list of users mentioned in the message.
@@ -192,7 +191,7 @@ type File struct {
 // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
 type MessageSend struct {
 	Content         string                  `json:"content,omitempty"`
-	Embed           *MessageEmbed           `json:"embed,omitempty"`
+	Embeds          []*MessageEmbed         `json:"embeds,omitempty"`
 	TTS             bool                    `json:"tts"`
 	Components      []MessageComponent      `json:"components"`
 	Files           []*File                 `json:"-"`
@@ -201,6 +200,9 @@ type MessageSend struct {
 
 	// TODO: Remove this when compatibility is not required.
 	File *File `json:"-"`
+
+	// TODO: Remove this when compatibility is not required.
+	Embed *MessageEmbed `json:"-"`
 }
 
 // MessageEdit is used to chain parameters via ChannelMessageEditComplex, which
@@ -208,11 +210,14 @@ type MessageSend struct {
 type MessageEdit struct {
 	Content         *string                 `json:"content,omitempty"`
 	Components      []MessageComponent      `json:"components"`
-	Embed           *MessageEmbed           `json:"embed,omitempty"`
+	Embeds          []*MessageEmbed         `json:"embeds,omitempty"`
 	AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
 
 	ID      string
 	Channel string
+
+	// TODO: Remove this when compatibility is not required.
+	Embed *MessageEmbed `json:"-"`
 }
 
 // NewMessageEdit returns a MessageEdit struct, initialized
@@ -234,7 +239,14 @@ func (m *MessageEdit) SetContent(str string) *MessageEdit {
 // SetEmbed is a convenience function for setting the embed,
 // so you can chain commands.
 func (m *MessageEdit) SetEmbed(embed *MessageEmbed) *MessageEdit {
-	m.Embed = embed
+	m.Embeds = []*MessageEmbed{embed}
+	return m
+}
+
+// SetEmbeds is a convenience function for setting the embeds,
+// so you can chain commands.
+func (m *MessageEdit) SetEmbeds(embeds []*MessageEmbed) *MessageEdit {
+	m.Embeds = embeds
 	return m
 }
 

+ 43 - 6
restapi.go

@@ -1552,10 +1552,21 @@ var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
 // channelID : The ID of a Channel.
 // data      : The message struct to send.
 func (s *Session) ChannelMessageSendComplex(channelID string, data *MessageSend) (st *Message, err error) {
-	if data.Embed != nil && data.Embed.Type == "" {
-		data.Embed.Type = "rich"
+	// TODO: Remove this when compatibility is not required.
+	if data.Embed != nil {
+		if data.Embeds == nil {
+			data.Embeds = []*MessageEmbed{data.Embed}
+		} else {
+			err = fmt.Errorf("cannot specify both Embed and Embeds")
+			return
+		}
 	}
 
+	for _, embed := range data.Embeds {
+		if embed.Type == "" {
+			embed.Type = "rich"
+		}
+	}
 	endpoint := EndpointChannelMessages(channelID)
 
 	// TODO: Remove this when compatibility is not required.
@@ -1602,8 +1613,15 @@ func (s *Session) ChannelMessageSendTTS(channelID string, content string) (*Mess
 // channelID : The ID of a Channel.
 // embed     : The embed data to send.
 func (s *Session) ChannelMessageSendEmbed(channelID string, embed *MessageEmbed) (*Message, error) {
+	return s.ChannelMessageSendEmbeds(channelID, []*MessageEmbed{embed})
+}
+
+// ChannelMessageSendEmbeds sends a message to the given channel with multiple embedded data.
+// channelID : The ID of a Channel.
+// embeds    : The embeds data to send.
+func (s *Session) ChannelMessageSendEmbeds(channelID string, embeds []*MessageEmbed) (*Message, error) {
 	return s.ChannelMessageSendComplex(channelID, &MessageSend{
-		Embed: embed,
+		Embeds: embeds,
 	})
 }
 
@@ -1633,10 +1651,21 @@ func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (*Mes
 // ChannelMessageEditComplex edits an existing message, replacing it entirely with
 // the given MessageEdit struct
 func (s *Session) ChannelMessageEditComplex(m *MessageEdit) (st *Message, err error) {
-	if m.Embed != nil && m.Embed.Type == "" {
-		m.Embed.Type = "rich"
+	// TODO: Remove this when compatibility is not required.
+	if m.Embed != nil {
+		if m.Embeds == nil {
+			m.Embeds = []*MessageEmbed{m.Embed}
+		} else {
+			err = fmt.Errorf("cannot specify both Embed and Embeds")
+			return
+		}
 	}
 
+	for _, embed := range m.Embeds {
+		if embed.Type == "" {
+			embed.Type = "rich"
+		}
+	}
 	response, err := s.RequestWithBucketID("PATCH", EndpointChannelMessage(m.Channel, m.ID), m, EndpointChannelMessage(m.Channel, ""))
 	if err != nil {
 		return
@@ -1651,7 +1680,15 @@ func (s *Session) ChannelMessageEditComplex(m *MessageEdit) (st *Message, err er
 // messageID : The ID of a Message
 // embed     : The embed data to send
 func (s *Session) ChannelMessageEditEmbed(channelID, messageID string, embed *MessageEmbed) (*Message, error) {
-	return s.ChannelMessageEditComplex(NewMessageEdit(channelID, messageID).SetEmbed(embed))
+	return s.ChannelMessageEditEmbeds(channelID, messageID, []*MessageEmbed{embed})
+}
+
+// ChannelMessageEditEmbeds edits an existing message with multiple embedded data.
+// channelID : The ID of a Channel
+// messageID : The ID of a Message
+// embeds    : The embeds data to send
+func (s *Session) ChannelMessageEditEmbeds(channelID, messageID string, embeds []*MessageEmbed) (*Message, error) {
+	return s.ChannelMessageEditComplex(NewMessageEdit(channelID, messageID).SetEmbeds(embeds))
 }
 
 // ChannelMessageDelete deletes a message from the Channel.