From 5cc4af9aa2a03c0f2accc317d013c739a397291d Mon Sep 17 00:00:00 2001 From: legolord208 Date: Fri, 28 Apr 2017 21:24:38 +0200 Subject: [PATCH] Chaining ChannelMessageEditComplex (#365) * Chaining ChannelMessageEditComplex * Removed .Do() --- message.go | 40 ++++++++++++++++++++++++++++------------ restapi.go | 34 +++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/message.go b/message.go index 1138323..d3523ab 100644 --- a/message.go +++ b/message.go @@ -31,28 +31,44 @@ type Message struct { 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. type MessageSend struct { - MessageParams - Tts bool `json:"tts"` + Content string `json:"content,omitempty"` + 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 { - 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, // except it doesn't take a pointer. -// Only a conveniance function. -func (m *MessageParams) SetContent(str string) { +func (m *MessageEdit) SetContent(str string) *MessageEdit { 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. diff --git a/restapi.go b/restapi.go index c439ff6..ca53eac 100644 --- a/restapi.go +++ b/restapi.go @@ -1281,7 +1281,9 @@ func (s *Session) ChannelMessageAck(channelID, messageID, lastToken string) (st // channelID : The ID of a Channel. // content : The message to send. 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. @@ -1305,36 +1307,38 @@ func (s *Session) ChannelMessageSendComplex(channelID string, data *MessageSend) // channelID : The ID of a Channel. // content : The message to send. 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. // channelID : The ID of a Channel. // embed : The embed data to send. 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 // the given content. -// channeld : The ID of a Channel -// messageID : The ID of a Message -// content : The contents of the message +// channelID : The ID of a Channel +// messageID : The ID of a Message +// content : The contents of the message 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 // the given MessageEdit struct -// channeld : The ID of a Channel -// messageID : The ID of a Message -// data : The MessageEdit struct to send -func (s *Session) ChannelMessageEditComplex(channelID, messageID string, data *MessageEdit) (st *Message, err error) { - if data.Embed != nil && data.Embed.Type == "" { - data.Embed.Type = "rich" +func (s *Session) ChannelMessageEditComplex(m *MessageEdit) (st *Message, err error) { + if m.Embed != nil && m.Embed.Type == "" { + m.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 { return } @@ -1348,7 +1352,7 @@ func (s *Session) ChannelMessageEditComplex(channelID, messageID string, data *M // 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(channelID, messageID, &MessageEdit{MessageParams: MessageParams{Embed: embed}}) + return s.ChannelMessageEditComplex(NewMessageEdit(channelID, messageID).SetEmbed(embed)) } // ChannelMessageDelete deletes a message from the Channel.