diff options
| author | alex <[email protected]> | 2017-06-28 13:41:52 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-06-28 13:41:52 +0200 |
| commit | 54f82aeacef32914ec66ac22dd8cae53872e15c5 (patch) | |
| tree | b80cc1db5b5c147c88e4c589c5aa0f2192dcd326 /src/framework | |
| parent | Add an `on_cached` event (#114) (diff) | |
| download | serenity-54f82aeacef32914ec66ac22dd8cae53872e15c5.tar.xz serenity-54f82aeacef32914ec66ac22dd8cae53872e15c5.zip | |
Add reaction actions (#115)
Fixes #87
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/mod.rs | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/src/framework/mod.rs b/src/framework/mod.rs index 9a84b7f..aae518b 100644 --- a/src/framework/mod.rs +++ b/src/framework/mod.rs @@ -75,7 +75,7 @@ use std::default::Default; use std::sync::Arc; use std::thread; use ::client::Context; -use ::model::{Message, UserId}; +use ::model::{Message, MessageId, UserId, ChannelId, ReactionType}; use ::model::permissions::Permissions; use ::utils; @@ -209,6 +209,16 @@ pub enum DispatchError { type DispatchErrorHook = Fn(Context, Message, DispatchError) + Send + Sync + 'static; +pub(crate) type ActionFn = Fn(Context, MessageId, ChannelId) + Send + Sync + 'static; + +/// Defines wheter this action should be called when +/// a reaction's added, or removed. +#[derive(Clone, Eq, Hash, PartialEq)] +pub enum ReactionAction { + Add(ReactionType), + Remove(ReactionType), +} + /// A utility for easily managing dispatches to commands. /// /// Refer to the [module-level documentation] for more information. @@ -222,6 +232,7 @@ pub struct Framework { before: Option<Arc<BeforeHook>>, dispatch_error_handler: Option<Arc<DispatchErrorHook>>, buckets: HashMap<String, Bucket>, + pub(crate) reaction_actions: HashMap<ReactionAction, Arc<ActionFn>>, after: Option<Arc<AfterHook>>, /// Whether the framework has been "initialized". /// @@ -336,6 +347,52 @@ impl Framework { self } + /// Defines a "reaction action", that will be called if a reaction was + /// added; or deleted in a message. + /// + /// # Examples + /// ```rust,no_run + /// # use serenity::Client; + /// use serenity::model::ReactionType; + /// use serenity::framework::ReactionAction; + /// # let mut client = Client::new("token"); + /// # + /// client.with_framework(|f| f + /// .action(ReactionAction::Add(ReactionType::Unicode("❤".to_string())), |_, _, channel_id| { + /// let _ = channel_id.say("love you too"); + /// }) + /// ); + /// ``` + pub fn action<F>(mut self, action: ReactionAction, f: F) -> Self + where F: Fn(Context, MessageId, ChannelId) + Send + Sync + 'static { + self.reaction_actions.insert(action, Arc::new(f)); + + self + } + + /// Remove the action from any further usage by the framework. + /// + /// # Examples + /// ```rust,no_run + /// # use serenity::Client; + /// use serenity::model::ReactionType; + /// use serenity::framework::ReactionAction; + /// # let mut client = Client::new("token"); + /// # + /// let action = ReactionAction::Add(ReactionType::Unicode("❤".to_string())); + /// client.with_framework(|f| f + /// .action(action.clone(), |_, _, channel_id| { + /// let _ = channel_id.say("love you too"); + /// }) + /// .remove_action(action) + /// ); + /// ``` + pub fn remove_action(mut self, action: ReactionAction) -> Self { + self.reaction_actions.remove(&action); + + self + } + #[cfg(feature="cache")] fn is_blocked_guild(&self, message: &Message) -> bool { if let Some(Channel::Guild(channel)) = CACHE.read().unwrap().channel(message.channel_id) { |