diff options
| author | Zeyla Hellyer <[email protected]> | 2018-01-18 10:36:50 -0800 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2018-01-18 10:36:50 -0800 |
| commit | edb76627d3fe292688b3c072c3d3f40db52e8aef (patch) | |
| tree | b37affd2a928d1845bdebe23a5456f7e3e878fd0 /src | |
| parent | Update readme example (diff) | |
| download | serenity-edb76627d3fe292688b3c072c3d3f40db52e8aef.tar.xz serenity-edb76627d3fe292688b3c072c3d3f40db52e8aef.zip | |
Add an `EditMessage` builder
When editing messages, not all fields are applicable. Attachments, TTS,
and reactions are not applicable. To help with this distinction,
separate message editing into a different builder.
Diffstat (limited to 'src')
| -rw-r--r-- | src/builder/edit_message.rs | 45 | ||||
| -rw-r--r-- | src/builder/mod.rs | 2 | ||||
| -rw-r--r-- | src/model/channel/channel_id.rs | 12 | ||||
| -rw-r--r-- | src/model/channel/group.rs | 10 | ||||
| -rw-r--r-- | src/model/channel/guild_channel.rs | 10 | ||||
| -rw-r--r-- | src/model/channel/message.rs | 16 | ||||
| -rw-r--r-- | src/model/channel/mod.rs | 10 | ||||
| -rw-r--r-- | src/model/channel/private_channel.rs | 10 |
8 files changed, 79 insertions, 36 deletions
diff --git a/src/builder/edit_message.rs b/src/builder/edit_message.rs new file mode 100644 index 0000000..29da46a --- /dev/null +++ b/src/builder/edit_message.rs @@ -0,0 +1,45 @@ +use internal::prelude::*; +use std::fmt::Display; +use super::CreateEmbed; +use utils::{self, VecMap}; + +/// A builder to specify the fields to edit in an existing message. +/// +/// # Examples +/// +/// Editing the content of a [`Message`] to `"hello"`: +/// +/// ```rust,no_run +/// # use serenity::model::id::{ChannelId, MessageId}; +/// # +/// # let mut message = ChannelId(7).message(MessageId(8)).unwrap(); +/// # +/// +/// let _ = message.edit(|m| m.content("hello")); +/// ``` +/// +/// [`Message`]: ../model/channel/struct.Message.html +#[derive(Clone, Debug, Default)] +pub struct EditMessage(pub VecMap<&'static str, Value>); + +impl EditMessage { + /// Set the content of the message. + /// + /// **Note**: Message contents must be under 2000 unicode code points. + pub fn content<D: Display>(mut self, content: D) -> Self { + self.0.insert("content", Value::String(content.to_string())); + + self + } + + /// Set an embed for the message. + pub fn embed<F>(mut self, f: F) -> Self + where F: FnOnce(CreateEmbed) -> CreateEmbed { + let map = utils::vecmap_to_json_map(f(CreateEmbed::default()).0); + let embed = Value::Object(map); + + self.0.insert("embed", embed); + + self + } +} diff --git a/src/builder/mod.rs b/src/builder/mod.rs index 3c59c0d..c372e70 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -11,6 +11,7 @@ mod create_message; mod edit_channel; mod edit_guild; mod edit_member; +mod edit_message; mod edit_profile; mod edit_role; mod execute_webhook; @@ -22,6 +23,7 @@ pub use self::create_message::CreateMessage; pub use self::edit_channel::EditChannel; pub use self::edit_guild::EditGuild; pub use self::edit_member::EditMember; +pub use self::edit_message::EditMessage; pub use self::edit_profile::EditProfile; pub use self::edit_role::EditRole; pub use self::execute_webhook::ExecuteWebhook; diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index 7093dfb..5cb8763 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -6,7 +6,7 @@ use std::borrow::Cow; #[cfg(feature = "model")] use std::fmt::Write as FmtWrite; #[cfg(feature = "model")] -use builder::{CreateMessage, EditChannel, GetMessages}; +use builder::{CreateMessage, EditChannel, EditMessage, GetMessages}; #[cfg(all(feature = "cache", feature = "model"))] use CACHE; #[cfg(feature = "model")] @@ -200,7 +200,7 @@ impl ChannelId { /// /// Message editing preserves all unchanged message data. /// - /// Refer to the documentation for [`CreateMessage`] for more information + /// Refer to the documentation for [`EditMessage`] for more information /// regarding message restrictions and requirements. /// /// **Note**: Requires that the current user be the author of the message. @@ -212,12 +212,12 @@ impl ChannelId { /// over the limit. /// /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong - /// [`CreateMessage`]: ../builder/struct.CreateMessage.html + /// [`EditMessage`]: ../builder/struct.EditMessage.html /// [`Message`]: struct.Message.html - /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content + /// [`the limit`]: ../builder/struct.EditMessage.html#method.content pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { - let msg = f(CreateMessage::default()); + where F: FnOnce(EditMessage) -> EditMessage, M: Into<MessageId> { + let msg = f(EditMessage::default()); if let Some(content) = msg.0.get(&"content") { if let Value::String(ref content) = *content { diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index 69e9cc7..0ee2a71 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -2,7 +2,7 @@ use chrono::{DateTime, FixedOffset}; use model::prelude::*; #[cfg(feature = "model")] -use builder::{CreateMessage, GetMessages}; +use builder::{CreateMessage, EditMessage, GetMessages}; #[cfg(feature = "model")] use http::{self, AttachmentType}; #[cfg(feature = "model")] @@ -134,7 +134,7 @@ impl Group { /// /// Message editing preserves all unchanged message data. /// - /// Refer to the documentation for [`CreateMessage`] for more information + /// Refer to the documentation for [`EditMessage`] for more information /// regarding message restrictions and requirements. /// /// **Note**: Requires that the current user be the author of the message. @@ -146,12 +146,12 @@ impl Group { /// over the limit. /// /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong - /// [`CreateMessage`]: ../builder/struct.CreateMessage.html + /// [`EditMessage`]: ../builder/struct.EditMessage.html /// [`Message`]: struct.Message.html - /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content + /// [`the limit`]: ../builder/struct.EditMessage.html#method.content #[inline] pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + where F: FnOnce(EditMessage) -> EditMessage, M: Into<MessageId> { self.channel_id.edit_message(message_id, f) } diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 3653cb6..ef14d07 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -4,7 +4,7 @@ use model::prelude::*; #[cfg(all(feature = "cache", feature = "model"))] use CACHE; #[cfg(feature = "model")] -use builder::{CreateInvite, CreateMessage, EditChannel, GetMessages}; +use builder::{CreateInvite, CreateMessage, EditChannel, EditMessage, GetMessages}; #[cfg(feature = "model")] use http::{self, AttachmentType}; #[cfg(all(feature = "cache", feature = "model"))] @@ -333,7 +333,7 @@ impl GuildChannel { /// /// Message editing preserves all unchanged message data. /// - /// Refer to the documentation for [`CreateMessage`] for more information + /// Refer to the documentation for [`EditMessage`] for more information /// regarding message restrictions and requirements. /// /// **Note**: Requires that the current user be the author of the message. @@ -345,12 +345,12 @@ impl GuildChannel { /// over the limit. /// /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong - /// [`CreateMessage`]: ../builder/struct.CreateMessage.html + /// [`EditMessage`]: ../builder/struct.EditMessage.html /// [`Message`]: struct.Message.html - /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content + /// [`the limit`]: ../builder/struct.EditMessage.html#method.content #[inline] pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + where F: FnOnce(EditMessage) -> EditMessage, M: Into<MessageId> { self.id.edit_message(message_id, f) } diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index f3834bd..d8abf3d 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -5,7 +5,7 @@ use model::prelude::*; use serde_json::Value; #[cfg(feature = "model")] -use builder::{CreateEmbed, CreateMessage}; +use builder::{CreateEmbed, EditMessage}; #[cfg(all(feature = "cache", feature = "model"))] use CACHE; #[cfg(all(feature = "cache", feature = "model"))] @@ -178,7 +178,7 @@ impl Message { /// /// Message editing preserves all unchanged message data. /// - /// Refer to the documentation for [`CreateMessage`] for more information + /// Refer to the documentation for [`EditMessage`] for more information /// regarding message restrictions and requirements. /// /// **Note**: Requires that the current user be the author of the message. @@ -204,10 +204,10 @@ impl Message { /// /// [`ModelError::InvalidUser`]: enum.ModelError.html#variant.InvalidUser /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong - /// [`CreateMessage`]: ../builder/struct.CreateMessage.html - /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content + /// [`EditMessage`]: ../builder/struct.EditMessage.html + /// [`the limit`]: ../builder/struct.EditMessage.html#method.content pub fn edit<F>(&mut self, f: F) -> Result<()> - where F: FnOnce(CreateMessage) -> CreateMessage { + where F: FnOnce(EditMessage) -> EditMessage { #[cfg(feature = "cache")] { if self.author.id != CACHE.read().user.id { @@ -215,7 +215,7 @@ impl Message { } } - let mut builder = CreateMessage::default(); + let mut builder = EditMessage::default(); if !self.content.is_empty() { builder = builder.content(&self.content); @@ -225,10 +225,6 @@ impl Message { builder = builder.embed(|_| CreateEmbed::from(embed.clone())); } - if self.tts { - builder = builder.tts(true); - } - let map = serenity_utils::vecmap_to_json_map(f(builder).0); match http::edit_message(self.channel_id.0, self.id.0, &Value::Object(map)) { diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs index 1a6bcd5..eadc7bb 100644 --- a/src/model/channel/mod.rs +++ b/src/model/channel/mod.rs @@ -28,7 +28,7 @@ use serde_json; use super::utils::deserialize_u64; #[cfg(feature = "model")] -use builder::{CreateMessage, GetMessages}; +use builder::{CreateMessage, EditMessage, GetMessages}; #[cfg(feature = "model")] use http::AttachmentType; #[cfg(feature = "model")] @@ -277,7 +277,7 @@ impl Channel { /// /// Message editing preserves all unchanged message data. /// - /// Refer to the documentation for [`CreateMessage`] for more information + /// Refer to the documentation for [`EditMessage`] for more information /// regarding message restrictions and requirements. /// /// **Note**: Requires that the current user be the author of the message. @@ -289,14 +289,14 @@ impl Channel { /// over the limit. /// /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong - /// [`CreateMessage`]: ../builder/struct.CreateMessage.html + /// [`EditMessage`]: ../builder/struct.EditMessage.html /// [`Message`]: struct.Message.html - /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content + /// [`the limit`]: ../builder/struct.EditMessage.html#method.content #[cfg(feature = "model")] #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + where F: FnOnce(EditMessage) -> EditMessage, M: Into<MessageId> { self.id().edit_message(message_id, f) } diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs index 39dcac3..3bae61d 100644 --- a/src/model/channel/private_channel.rs +++ b/src/model/channel/private_channel.rs @@ -4,7 +4,7 @@ use std::fmt::{Display, Formatter, Result as FmtResult}; use super::deserialize_single_recipient; #[cfg(feature = "model")] -use builder::{CreateMessage, GetMessages}; +use builder::{CreateMessage, EditMessage, GetMessages}; #[cfg(feature = "model")] use http::AttachmentType; #[cfg(feature = "model")] @@ -116,7 +116,7 @@ impl PrivateChannel { /// /// Message editing preserves all unchanged message data. /// - /// Refer to the documentation for [`CreateMessage`] for more information + /// Refer to the documentation for [`EditMessage`] for more information /// regarding message restrictions and requirements. /// /// **Note**: Requires that the current user be the author of the message. @@ -128,12 +128,12 @@ impl PrivateChannel { /// over the limit. /// /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong - /// [`CreateMessage`]: ../builder/struct.CreateMessage.html + /// [`EditMessage`]: ../builder/struct.EditMessage.html /// [`Message`]: struct.Message.html - /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content + /// [`the limit`]: ../builder/struct.EditMessage.html#method.content #[inline] pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + where F: FnOnce(EditMessage) -> EditMessage, M: Into<MessageId> { self.id.edit_message(message_id, f) } |