Skip to content

Commit

Permalink
Support adding reactions when creating message
Browse files Browse the repository at this point in the history
These reactions are added onto the `MessageCreate` builder, and are sent
after the message has been created.
  • Loading branch information
arqunis authored and Zeyla Hellyer committed May 24, 2017
1 parent 8c0aeac commit 77b5b48
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/builder/create_message.rs
@@ -1,4 +1,5 @@
use super::CreateEmbed;
use ::model::ReactionType;
use ::internal::prelude::*;

/// A builder to specify the contents of an [`http::send_message`] request,
Expand Down Expand Up @@ -37,7 +38,7 @@ use ::internal::prelude::*;
/// [`embed`]: #method.embed
/// [`http::send_message`]: ../http/fn.send_message.html
#[derive(Clone, Debug)]
pub struct CreateMessage(pub Map<String, Value>);
pub struct CreateMessage(pub Map<String, Value>, pub Option<Vec<ReactionType>>);

impl CreateMessage {
/// Set the content of the message.
Expand All @@ -46,7 +47,7 @@ impl CreateMessage {
pub fn content(mut self, content: &str) -> Self {
self.0.insert("content".to_owned(), Value::String(content.to_owned()));

CreateMessage(self.0)
CreateMessage(self.0, self.1)
}

/// Set an embed for the message.
Expand All @@ -56,7 +57,7 @@ impl CreateMessage {

self.0.insert("embed".to_owned(), embed);

CreateMessage(self.0)
CreateMessage(self.0, self.1)
}

/// Set whether the message is text-to-speech.
Expand All @@ -67,7 +68,14 @@ impl CreateMessage {
pub fn tts(mut self, tts: bool) -> Self {
self.0.insert("tts".to_owned(), Value::Bool(tts));

CreateMessage(self.0)
CreateMessage(self.0, self.1)
}

/// Adds a list of reactions to create after the message's sent.
pub fn reactions<R: Into<ReactionType>>(mut self, reactions: Vec<R>) -> Self {
self.1 = Some(reactions.into_iter().map(|r| r.into()).collect());

CreateMessage(self.0, self.1)
}
}

Expand All @@ -81,6 +89,6 @@ impl Default for CreateMessage {
let mut map = Map::default();
map.insert("tts".to_owned(), Value::Bool(false));

CreateMessage(map)
CreateMessage(map, None)
}
}
12 changes: 10 additions & 2 deletions src/model/channel/channel_id.rs
Expand Up @@ -425,12 +425,20 @@ impl ChannelId {
/// [Send Messages]: permissions/constant.SEND_MESSAGES.html
pub fn send_message<F>(&self, f: F) -> Result<Message>
where F: FnOnce(CreateMessage) -> CreateMessage {
let map = f(CreateMessage::default()).0;
let CreateMessage(map, reactions) = f(CreateMessage::default());

Message::check_content_length(&map)?;
Message::check_embed_length(&map)?;

http::send_message(self.0, &Value::Object(map))
let message = http::send_message(self.0, &Value::Object(map))?;

if let Some(reactions) = reactions {
for reaction in reactions {
self.create_reaction(message.id, reaction)?;
}
}

Ok(message)
}

/// Unpins a [`Message`] in the channel given by its Id.
Expand Down

0 comments on commit 77b5b48

Please sign in to comment.