diff options
| author | Ken Swenson <[email protected]> | 2017-05-28 15:56:47 -0400 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-05-28 12:56:47 -0700 |
| commit | 46b79ddb45d03bfbe0eb10a9d5e1c53c9a15f55b (patch) | |
| tree | addb6c38cb0b5f81122841474deed95897e62d57 /src/model | |
| parent | impl From<char> for ReactionType (diff) | |
| download | serenity-46b79ddb45d03bfbe0eb10a9d5e1c53c9a15f55b.tar.xz serenity-46b79ddb45d03bfbe0eb10a9d5e1c53c9a15f55b.zip | |
Implement multiple attachments
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel/channel_id.rs | 80 | ||||
| -rw-r--r-- | src/model/channel/group.rs | 28 | ||||
| -rw-r--r-- | src/model/channel/guild_channel.rs | 28 | ||||
| -rw-r--r-- | src/model/channel/mod.rs | 28 | ||||
| -rw-r--r-- | src/model/channel/private_channel.rs | 28 |
5 files changed, 192 insertions, 0 deletions
diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index 121399a..086f749 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -8,6 +8,8 @@ use ::builder::{CreateMessage, EditChannel, GetMessages}; use ::CACHE; #[cfg(feature="model")] use ::http; +#[cfg(feature="model")] +use ::http::AttachmentType; #[cfg(feature="model")] impl ChannelId { @@ -393,6 +395,8 @@ impl ChannelId { /// [`GuildChannel`]: struct.GuildChannel.html /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + #[deprecated(since="0.2.0", note="Please use `send_files` instead.")] + #[allow(deprecated)] pub fn send_file<F, R>(&self, file: R, filename: &str, f: F) -> Result<Message> where F: FnOnce(CreateMessage) -> CreateMessage, R: Read { let mut map = f(CreateMessage::default()).0; @@ -410,6 +414,82 @@ impl ChannelId { http::send_file(self.0, file, filename, map) } + /// Sends a file along with optional message contents. The filename _must_ + /// be specified. + /// + /// Message contents may be passed by using the [`CreateMessage::content`] + /// method. + /// + /// An embed can _not_ be sent when sending a file. If you set one, it will + /// be automatically removed. + /// + /// The [Attach Files] and [Send Messages] permissions are required. + /// + /// **Note**: Message contents must be under 2000 unicode code points. + /// + /// # Examples + /// + /// Send files with the paths `/path/to/file.jpg` and `/path/to/file2.jpg`: + /// + /// ```rust,no_run + /// use serenity::model::ChannelId; + /// + /// let channel_id = ChannelId(7); + /// + /// let paths = vec!["/path/to/file.jpg", "path/to/file2.jpg"]; + /// + /// let _ = channel_id.send_files(paths, |m| m.content("a file")); + /// ``` + /// + /// Send files using `File`: + /// + /// ```rust,no_run + /// use serenity::model::ChannelId; + /// use std::fs::File; + /// + /// let channel_id = ChannelId(7); + /// + /// let f1 = File::open("my_file.jpg").unwrap(); + /// let f2 = File::open("my_file2.jpg").unwrap(); + /// + /// let files = vec![(f1, "my_file.jpg"), (f2, "my_file2.jpg")]; + /// + /// let _ = channel_id.send_files(files, |m| m.content("a file")); + /// ``` + /// + /// # Errors + /// + /// If the content of the message is over the above limit, then a + /// [`ClientError::MessageTooLong`] will be returned, containing the number + /// of unicode code points over the limit. + /// + /// Returns an + /// [`HttpError::InvalidRequest(PayloadTooLarge)`][`HttpError::InvalidRequest`] + /// if the file is too large to send. + /// + /// [`ClientError::MessageTooLong`]: ../client/enum.ClientError.html#variant.MessageTooLong + /// [`HttpError::InvalidRequest`]: ../http/enum.HttpError.html#variant.InvalidRequest + /// [`CreateMessage::content`]: ../utils/builder/struct.CreateMessage.html#method.content + /// [`GuildChannel`]: struct.GuildChannel.html + /// [Attach Files]: permissions/constant.ATTACH_FILES.html + /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage { + let mut map = f(CreateMessage::default()).0; + + if let Some(content) = map.get("content") { + if let Value::String(ref content) = *content { + if let Some(length_over) = Message::overflow_length(content) { + return Err(Error::Model(ModelError::MessageTooLong(length_over))); + } + } + } + + let _ = map.remove("embed"); + + http::send_files(self.0, files, map) + } + /// Sends a message to the channel. /// /// Refer to the documentation for [`CreateMessage`] for more information diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index aab3e3a..6adedc6 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -7,6 +7,8 @@ use ::model::*; use ::builder::{CreateMessage, GetMessages}; #[cfg(feature="model")] use ::http; +#[cfg(feature="model")] +use ::http::AttachmentType; /// A group channel - potentially including other [`User`]s - separate from a /// [`Guild`]. @@ -298,11 +300,37 @@ impl Group { /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + #[deprecated(since="0.2.0", note="Please use `send_files` instead.")] + #[allow(deprecated)] pub fn send_file<F, R>(&self, file: R, filename: &str, f: F) -> Result<Message> where F: FnOnce(CreateMessage) -> CreateMessage, R: Read { self.channel_id.send_file(file, filename, f) } + /// Sends (a) file(s) along with optional message contents. + /// + /// Refer to [`ChannelId::send_file`] for examples and more information. + /// + /// The [Attach Files] and [Send Messages] permissions are required. + /// + /// **Note**: Message contents must be under 2000 unicode code points. + /// + /// # Errors + /// + /// If the content of the message is over the above limit, then a + /// [`ClientError::MessageTooLong`] will be returned, containing the number + /// of unicode code points over the limit. + /// + /// [`ChannelId::send_file`]: struct.ChannelId.html#method.send_file + /// [`ClientError::MessageTooLong`]: ../client/enum.ClientError.html#variant.MessageTooLong + /// [Attach Files]: permissions/constant.ATTACH_FILES.html + /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + #[inline] + pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage { + self.channel_id.send_files(files, f) + } + /// Sends a message to the group with the given content. /// /// Refer to the documentation for [`CreateMessage`] for more information diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 8724de2..192206e 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -10,6 +10,8 @@ use ::builder::{CreateInvite, CreateMessage, EditChannel, GetMessages}; use ::CACHE; #[cfg(feature="model")] use ::http; +#[cfg(feature="model")] +use ::http::AttachmentType; #[cfg(all(feature="model", feature="utils"))] use ::utils as serenity_utils; @@ -557,11 +559,37 @@ impl GuildChannel { /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + #[deprecated(since="0.2.0", note="Please use `send_files` instead.")] + #[allow(deprecated)] pub fn send_file<F, R>(&self, file: R, filename: &str, f: F) -> Result<Message> where F: FnOnce(CreateMessage) -> CreateMessage, R: Read { self.id.send_file(file, filename, f) } + /// Sends (a) file(s) along with optional message contents. + /// + /// Refer to [`ChannelId::send_file`] for examples and more information. + /// + /// The [Attach Files] and [Send Messages] permissions are required. + /// + /// **Note**: Message contents must be under 2000 unicode code points. + /// + /// # Errors + /// + /// If the content of the message is over the above limit, then a + /// [`ClientError::MessageTooLong`] will be returned, containing the number + /// of unicode code points over the limit. + /// + /// [`ChannelId::send_file`]: struct.ChannelId.html#method.send_file + /// [`ClientError::MessageTooLong`]: ../client/enum.ClientError.html#variant.MessageTooLong + /// [Attach Files]: permissions/constant.ATTACH_FILES.html + /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + #[inline] + pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage { + self.id.send_files(files, f) + } + /// Sends a message to the channel with the given content. /// /// **Note**: This will only work when a [`Message`] is received. diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs index 300f402..93ce5cb 100644 --- a/src/model/channel/mod.rs +++ b/src/model/channel/mod.rs @@ -25,6 +25,8 @@ use ::model::*; #[cfg(feature="model")] use ::builder::{CreateMessage, GetMessages}; +#[cfg(feature="model")] +use ::http::AttachmentType; /// A container for any channel. #[derive(Clone, Debug)] @@ -293,11 +295,37 @@ impl Channel { /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + #[deprecated(since="0.2.0", note="Please use `send_files` instead.")] + #[allow(deprecated)] pub fn send_file<F, R>(&self, file: R, filename: &str, f: F) -> Result<Message> where F: FnOnce(CreateMessage) -> CreateMessage, R: Read { self.id().send_file(file, filename, f) } + /// Sends (a) file(s) along with optional message contents. + /// + /// Refer to [`ChannelId::send_file`] for examples and more information. + /// + /// The [Attach Files] and [Send Messages] permissions are required. + /// + /// **Note**: Message contents must be under 2000 unicode code points. + /// + /// # Errors + /// + /// If the content of the message is over the above limit, then a + /// [`ClientError::MessageTooLong`] will be returned, containing the number + /// of unicode code points over the limit. + /// + /// [`ChannelId::send_file`]: struct.ChannelId.html#method.send_file + /// [`ClientError::MessageTooLong`]: ../client/enum.ClientError.html#variant.MessageTooLong + /// [Attach Files]: permissions/constant.ATTACH_FILES.html + /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + #[inline] + pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage { + self.id().send_files(files, f) + } + /// Sends a message to the channel. /// /// Refer to the documentation for [`CreateMessage`] for more information diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs index d63efff..0432683 100644 --- a/src/model/channel/private_channel.rs +++ b/src/model/channel/private_channel.rs @@ -5,6 +5,8 @@ use ::model::*; #[cfg(feature="model")] use ::builder::{CreateMessage, GetMessages}; +#[cfg(feature="model")] +use ::http::AttachmentType; /// A Direct Message text channel with another user. #[derive(Clone, Debug, Deserialize)] @@ -244,11 +246,37 @@ impl PrivateChannel { /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + #[deprecated(since="0.2.0", note="Please use `send_files` instead.")] + #[allow(deprecated)] pub fn send_file<F, R>(&self, file: R, filename: &str, f: F) -> Result<Message> where F: FnOnce(CreateMessage) -> CreateMessage, R: Read { self.id.send_file(file, filename, f) } + /// Sends (a) file(s) along with optional message contents. + /// + /// Refer to [`ChannelId::send_file`] for examples and more information. + /// + /// The [Attach Files] and [Send Messages] permissions are required. + /// + /// **Note**: Message contents must be under 2000 unicode code points. + /// + /// # Errors + /// + /// If the content of the message is over the above limit, then a + /// [`ClientError::MessageTooLong`] will be returned, containing the number + /// of unicode code points over the limit. + /// + /// [`ChannelId::send_file`]: struct.ChannelId.html#method.send_file + /// [`ClientError::MessageTooLong`]: ../client/enum.ClientError.html#variant.MessageTooLong + /// [Attach Files]: permissions/constant.ATTACH_FILES.html + /// [Send Messages]: permissions/constant.SEND_MESSAGES.html + #[inline] + pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage { + self.id.send_files(files, f) + } + /// Sends a message to the channel with the given content. /// /// Refer to the documentation for [`CreateMessage`] for more information |