Skip to content

Commit

Permalink
Support for multiple embeds in message routes (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
xIceArcher committed Oct 10, 2021
1 parent 0fad116 commit 842ddb3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
22 changes: 17 additions & 5 deletions message.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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:"-"`
Expand All @@ -201,18 +200,24 @@ 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
// is also where you should get the instance from.
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
Expand All @@ -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
}

Expand Down
49 changes: 43 additions & 6 deletions restapi.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 842ddb3

Please sign in to comment.