aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaid Dog <[email protected]>2018-03-11 03:00:01 -0500
committerKen Swenson <[email protected]>2018-11-06 20:27:11 -0500
commit9d9cd2ead2d9216e226b424d086a24ea282af588 (patch)
tree20bbd564a48f96a08afa1675e6bc91fa79fe9a9a /src
parentVoice fixes, better API adherence, bitrate control, documentation (diff)
downloadserenity-9d9cd2ead2d9216e226b424d086a24ea282af588.tar.xz
serenity-9d9cd2ead2d9216e226b424d086a24ea282af588.zip
Permit sending files through the `CreateMessage` builder.
Diffstat (limited to 'src')
-rw-r--r--src/builder/create_message.rs35
-rw-r--r--src/http/mod.rs1
-rw-r--r--src/model/channel/channel_id.rs15
3 files changed, 44 insertions, 7 deletions
diff --git a/src/builder/create_message.rs b/src/builder/create_message.rs
index a5ede5a..000ad09 100644
--- a/src/builder/create_message.rs
+++ b/src/builder/create_message.rs
@@ -1,4 +1,5 @@
use internal::prelude::*;
+use http::AttachmentType;
use model::channel::ReactionType;
use std::fmt::Display;
use super::CreateEmbed;
@@ -40,9 +41,9 @@ use utils::{self, VecMap};
/// [`embed`]: #method.embed
/// [`http::send_message`]: ../http/fn.send_message.html
#[derive(Clone, Debug)]
-pub struct CreateMessage(pub VecMap<&'static str, Value>, pub Option<Vec<ReactionType>>);
+pub struct CreateMessage<'a>(pub VecMap<&'static str, Value>, pub Option<Vec<ReactionType>>, pub Vec<AttachmentType<'a>>);
-impl CreateMessage {
+impl<'a> CreateMessage<'a> {
/// Set the content of the message.
///
/// **Note**: Message contents must be under 2000 unicode code points.
@@ -90,18 +91,42 @@ impl CreateMessage {
self
}
+
+ /// Appends a file to the message.
+ pub fn add_file<T: Into<AttachmentType<'a>>>(mut self, file: T) -> Self {
+ self.2.push(file.into());
+
+ self
+ }
+
+ /// Appends a list of files to the message.
+ pub fn add_files<T: Into<AttachmentType<'a>>, It: IntoIterator<Item=T>>(mut self, files: It) -> Self {
+ self.2.extend(files.into_iter().map(|f| f.into()));
+
+ self
+ }
+
+ /// Sets a list of files to include in the message.
+ ///
+ /// Calling this multiple times will overwrite the file list.
+ /// To append files, call `add_file` or `add_files` instead.
+ pub fn files<T: Into<AttachmentType<'a>>, It: IntoIterator<Item=T>>(mut self, files: It) -> Self {
+ self.2 = files.into_iter().map(|f| f.into()).collect();
+
+ self
+ }
}
-impl Default for CreateMessage {
+impl<'a> Default for CreateMessage<'a> {
/// Creates a map for sending a [`Message`], setting [`tts`] to `false` by
/// default.
///
/// [`Message`]: ../model/channel/struct.Message.html
/// [`tts`]: #method.tts
- fn default() -> CreateMessage {
+ fn default() -> CreateMessage<'a> {
let mut map = VecMap::new();
map.insert("tts", Value::Bool(false));
- CreateMessage(map, None)
+ CreateMessage(map, None, Vec::new())
}
}
diff --git a/src/http/mod.rs b/src/http/mod.rs
index bb4af53..e7e2bea 100644
--- a/src/http/mod.rs
+++ b/src/http/mod.rs
@@ -93,6 +93,7 @@ lazy_static! {
}
/// Enum that allows a user to pass a `Path` or a `File` type to `send_files`
+#[derive(Clone, Debug)]
pub enum AttachmentType<'a> {
/// Indicates that the `AttachmentType` is a byte slice with a filename.
Bytes((&'a [u8], &'a str)),
diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs
index 3f52055..d80e362 100644
--- a/src/model/channel/channel_id.rs
+++ b/src/model/channel/channel_id.rs
@@ -597,13 +597,24 @@ impl ChannelId {
#[cfg(feature = "utils")]
pub fn send_message<F>(&self, f: F) -> Result<Message>
where F: FnOnce(CreateMessage) -> CreateMessage {
- let msg = f(CreateMessage::default());
+ let mut msg = f(CreateMessage::default());
+
+ if !msg.2.is_empty() {
+ if let Some(e) = msg.0.remove(&"embed") {
+ msg.0.insert("payload_json", json!({ "embed": e }));
+ }
+ }
+
let map = utils::vecmap_to_json_map(msg.0);
Message::check_content_length(&map)?;
Message::check_embed_length(&map)?;
- let message = http::send_message(self.0, &Value::Object(map))?;
+ let message = if msg.2.is_empty() {
+ http::send_message(self.0, &Value::Object(map))?
+ } else {
+ http::send_files(self.0, msg.2, map)?
+ };
if let Some(reactions) = msg.1 {
for reaction in reactions {