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/builder | |
| 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/builder')
| -rw-r--r-- | src/builder/edit_message.rs | 45 | ||||
| -rw-r--r-- | src/builder/mod.rs | 2 |
2 files changed, 47 insertions, 0 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; |