diff options
| -rw-r--r-- | src/client/context.rs | 12 | ||||
| -rw-r--r-- | src/model/channel.rs | 22 |
2 files changed, 27 insertions, 7 deletions
diff --git a/src/client/context.rs b/src/client/context.rs index fe69a57..1046361 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -6,6 +6,7 @@ use super::connection::Connection; use super::http; use super::login_type::LoginType; use ::utils::builder::{ + CreateEmbed, CreateInvite, CreateMessage, EditChannel, @@ -575,10 +576,17 @@ impl Context { http::edit_role(guild_id.0, role_id.0, map) } - pub fn edit_message<C, M>(&self, channel_id: C, message_id: M, text: &str) - -> Result<Message> where C: Into<ChannelId>, M: Into<MessageId> { + /// Edit a message given its Id and the Id of the channel it belongs to. + /// + /// Pass an empty string (`""`) to `text` if you are editing a message with + /// an embed but no content. Otherwise, `text` must be given. + pub fn edit_message<C, F, M>(&self, channel_id: C, message_id: M, text: &str, f: F) + -> Result<Message> where C: Into<ChannelId>, + F: FnOnce(CreateEmbed) -> CreateEmbed, + M: Into<MessageId> { let map = ObjectBuilder::new() .insert("content", text) + .insert("embed", Value::Object(f(CreateEmbed::default()).0)) .build(); http::edit_message(channel_id.into().0, message_id.into().0, map) diff --git a/src/model/channel.rs b/src/model/channel.rs index 7348a1c..0534ff3 100644 --- a/src/model/channel.rs +++ b/src/model/channel.rs @@ -472,6 +472,13 @@ impl Message { /// Edits this message, replacing the original content with new content. /// + /// If editing a message and not using an embed, just return the embed + /// builder directly, via: + /// + /// ```rust,ignore + /// message.edit("new content", |f| f); + /// ``` + /// /// **Note**: You must be the author of the message to be able to do this. /// /// **Note**: Messages must be under 2000 unicode code points. @@ -488,7 +495,8 @@ impl Message { /// [`ClientError::InvalidUser`]: ../client/enum.ClientError.html#variant.InvalidUser /// [`ClientError::MessageTooLong`]: enum.ClientError.html#variant.MessageTooLong #[cfg(feature = "methods")] - pub fn edit(&mut self, new_content: &str) -> Result<()> { + pub fn edit<F>(&mut self, new_content: &str, embed: F) -> Result<()> + where F: FnOnce(CreateEmbed) -> CreateEmbed { if let Some(length_over) = Message::overflow_length(new_content) { return Err(Error::Client(ClientError::MessageTooLong(length_over))); } @@ -497,11 +505,15 @@ impl Message { return Err(Error::Client(ClientError::InvalidUser)); } - let map = ObjectBuilder::new() - .insert("content", new_content) - .build(); + let mut map = ObjectBuilder::new().insert("content", new_content); + + let embed = embed(CreateEmbed::default()).0; + + if !embed.is_empty() { + map = map.insert("embed", Value::Object(embed)); + } - match http::edit_message(self.channel_id.0, self.id.0, map) { + match http::edit_message(self.channel_id.0, self.id.0, map.build()) { Ok(edited) => { mem::replace(self, edited); |