Support for multiple embeds in message routes (#1000)

This commit is contained in:
xIceArcher 2021-10-10 22:56:47 +08:00 committed by GitHub
parent 0fad116c6c
commit 842ddb357a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 11 deletions

View file

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

View file

@ -1552,10 +1552,21 @@ var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
// channelID : The ID of a Channel. // channelID : The ID of a Channel.
// data : The message struct to send. // data : The message struct to send.
func (s *Session) ChannelMessageSendComplex(channelID string, data *MessageSend) (st *Message, err error) { func (s *Session) ChannelMessageSendComplex(channelID string, data *MessageSend) (st *Message, err error) {
if data.Embed != nil && data.Embed.Type == "" { // TODO: Remove this when compatibility is not required.
data.Embed.Type = "rich" 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) endpoint := EndpointChannelMessages(channelID)
// TODO: Remove this when compatibility is not required. // 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. // channelID : The ID of a Channel.
// embed : The embed data to send. // embed : The embed data to send.
func (s *Session) ChannelMessageSendEmbed(channelID string, embed *MessageEmbed) (*Message, error) { 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{ 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 // ChannelMessageEditComplex edits an existing message, replacing it entirely with
// the given MessageEdit struct // the given MessageEdit struct
func (s *Session) ChannelMessageEditComplex(m *MessageEdit) (st *Message, err error) { func (s *Session) ChannelMessageEditComplex(m *MessageEdit) (st *Message, err error) {
if m.Embed != nil && m.Embed.Type == "" { // TODO: Remove this when compatibility is not required.
m.Embed.Type = "rich" 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, "")) response, err := s.RequestWithBucketID("PATCH", EndpointChannelMessage(m.Channel, m.ID), m, EndpointChannelMessage(m.Channel, ""))
if err != nil { if err != nil {
return return
@ -1651,7 +1680,15 @@ func (s *Session) ChannelMessageEditComplex(m *MessageEdit) (st *Message, err er
// messageID : The ID of a Message // messageID : The ID of a Message
// embed : The embed data to send // embed : The embed data to send
func (s *Session) ChannelMessageEditEmbed(channelID, messageID string, embed *MessageEmbed) (*Message, error) { 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. // ChannelMessageDelete deletes a message from the Channel.