Chaining ChannelMessageEditComplex (#365)

* Chaining ChannelMessageEditComplex

* Removed .Do()
This commit is contained in:
legolord208 2017-04-28 21:24:38 +02:00 committed by Chris Rhodes
parent 43c8b518ad
commit 5cc4af9aa2
2 changed files with 47 additions and 27 deletions

View file

@ -31,28 +31,44 @@ type Message struct {
Reactions []*MessageReactions `json:"reactions"` Reactions []*MessageReactions `json:"reactions"`
} }
// MessageParams is used in MessageSend and MessageEdit to share common parameters.
type MessageParams struct {
Content *string `json:"content,omitempty"`
Embed *MessageEmbed `json:"embed,omitempty"`
}
// MessageSend stores all parameters you can send with ChannelMessageSendComplex. // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
type MessageSend struct { type MessageSend struct {
MessageParams Content string `json:"content,omitempty"`
Tts bool `json:"tts"` Embed *MessageEmbed `json:"embed,omitempty"`
Tts bool `json:"tts"`
} }
// MessageEdit stores all parameters you can send with ChannelMessageSendComplex. // MessageEdit is used to chain parameters via ChannelMessageEditComplex, which
// is also where you should get the instance from.
type MessageEdit struct { type MessageEdit struct {
MessageParams Content *string `json:"content,omitempty"`
Embed *MessageEmbed `json:"embed,omitempty"`
ID string
Channel string
}
// NewMessageEdit returns a MessageEdit struct, initialized
// with the Channel and ID.
func NewMessageEdit(channelID string, messageID string) *MessageEdit {
return &MessageEdit{
Channel: channelID,
ID: messageID,
}
} }
// SetContent is the same as setting the variable Content, // SetContent is the same as setting the variable Content,
// except it doesn't take a pointer. // except it doesn't take a pointer.
// Only a conveniance function. func (m *MessageEdit) SetContent(str string) *MessageEdit {
func (m *MessageParams) SetContent(str string) {
m.Content = &str m.Content = &str
return m
}
// SetEmbed is a convenience function for setting the embed,
// so you can chain commands.
func (m *MessageEdit) SetEmbed(embed *MessageEmbed) *MessageEdit {
m.Embed = embed
return m
} }
// A MessageAttachment stores data for message attachments. // A MessageAttachment stores data for message attachments.

View file

@ -1281,7 +1281,9 @@ func (s *Session) ChannelMessageAck(channelID, messageID, lastToken string) (st
// channelID : The ID of a Channel. // channelID : The ID of a Channel.
// content : The message to send. // content : The message to send.
func (s *Session) ChannelMessageSend(channelID string, content string) (*Message, error) { func (s *Session) ChannelMessageSend(channelID string, content string) (*Message, error) {
return s.ChannelMessageSendComplex(channelID, &MessageSend{MessageParams: MessageParams{Content: &content}}) return s.ChannelMessageSendComplex(channelID, &MessageSend{
Content: content,
})
} }
// ChannelMessageSendComplex sends a message to the given channel. // ChannelMessageSendComplex sends a message to the given channel.
@ -1305,36 +1307,38 @@ func (s *Session) ChannelMessageSendComplex(channelID string, data *MessageSend)
// channelID : The ID of a Channel. // channelID : The ID of a Channel.
// content : The message to send. // content : The message to send.
func (s *Session) ChannelMessageSendTTS(channelID string, content string) (*Message, error) { func (s *Session) ChannelMessageSendTTS(channelID string, content string) (*Message, error) {
return s.ChannelMessageSendComplex(channelID, &MessageSend{MessageParams: MessageParams{Content: &content}, Tts: true}) return s.ChannelMessageSendComplex(channelID, &MessageSend{
Content: content,
Tts: true,
})
} }
// ChannelMessageSendEmbed sends a message to the given channel with embedded data. // ChannelMessageSendEmbed sends a message to the given channel with embedded data.
// 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.ChannelMessageSendComplex(channelID, &MessageSend{MessageParams: MessageParams{Embed: embed}}) return s.ChannelMessageSendComplex(channelID, &MessageSend{
Embed: embed,
})
} }
// ChannelMessageEdit edits an existing message, replacing it entirely with // ChannelMessageEdit edits an existing message, replacing it entirely with
// the given content. // the given content.
// channeld : The ID of a Channel // channelID : The ID of a Channel
// messageID : The ID of a Message // messageID : The ID of a Message
// content : The contents of the message // content : The contents of the message
func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (*Message, error) { func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (*Message, error) {
return s.ChannelMessageEditComplex(channelID, messageID, &MessageEdit{MessageParams: MessageParams{Content: &content}}) return s.ChannelMessageEditComplex(NewMessageEdit(channelID, messageID).SetContent(content))
} }
// 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
// channeld : The ID of a Channel func (s *Session) ChannelMessageEditComplex(m *MessageEdit) (st *Message, err error) {
// messageID : The ID of a Message if m.Embed != nil && m.Embed.Type == "" {
// data : The MessageEdit struct to send m.Embed.Type = "rich"
func (s *Session) ChannelMessageEditComplex(channelID, messageID string, data *MessageEdit) (st *Message, err error) {
if data.Embed != nil && data.Embed.Type == "" {
data.Embed.Type = "rich"
} }
response, err := s.RequestWithBucketID("PATCH", EndpointChannelMessage(channelID, messageID), data, EndpointChannelMessage(channelID, "")) response, err := s.RequestWithBucketID("PATCH", EndpointChannelMessage(m.Channel, m.ID), m, EndpointChannelMessage(m.Channel, ""))
if err != nil { if err != nil {
return return
} }
@ -1348,7 +1352,7 @@ func (s *Session) ChannelMessageEditComplex(channelID, messageID string, data *M
// 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(channelID, messageID, &MessageEdit{MessageParams: MessageParams{Embed: embed}}) return s.ChannelMessageEditComplex(NewMessageEdit(channelID, messageID).SetEmbed(embed))
} }
// ChannelMessageDelete deletes a message from the Channel. // ChannelMessageDelete deletes a message from the Channel.