diff --git a/youmubot-prelude/src/reaction_watch.rs b/youmubot-prelude/src/reaction_watch.rs index e522435..cb83269 100644 --- a/youmubot-prelude/src/reaction_watch.rs +++ b/youmubot-prelude/src/reaction_watch.rs @@ -8,22 +8,24 @@ use std::sync::{Arc, Mutex}; pub trait ReactionHandler { /// Handle a reaction. This is fired on EVERY reaction. /// You do the filtering yourself. - fn handle_reaction(&mut self, reaction: &Reaction) -> CommandResult; + /// + /// If `is_added` is false, the reaction was removed instead of added. + fn handle_reaction(&mut self, reaction: &Reaction, is_added: bool) -> CommandResult; } impl ReactionHandler for T where - T: FnMut(&Reaction) -> CommandResult, + T: FnMut(&Reaction, bool) -> CommandResult, { - fn handle_reaction(&mut self, reaction: &Reaction) -> CommandResult { - self(reaction) + fn handle_reaction(&mut self, reaction: &Reaction, is_added: bool) -> CommandResult { + self(reaction, is_added) } } /// The store for a set of dynamic reaction handlers. #[derive(Debug, Clone)] pub struct ReactionWatcher { - channels: Arc>>>>, + channels: Arc, bool)>>>>, } impl TypeMapKey for ReactionWatcher { @@ -38,12 +40,13 @@ impl ReactionWatcher { } } /// Send a reaction. - pub fn send(&self, r: Reaction) { + /// If `is_added` is false, the reaction was removed. + pub fn send(&self, r: Reaction, is_added: bool) { let r = Arc::new(r); self.channels .lock() .expect("Poisoned!") - .retain(|e| e.send(r.clone()).is_ok()); + .retain(|e| e.send((r.clone(), is_added)).is_ok()); } /// React! to a series of reaction /// @@ -60,7 +63,7 @@ impl ReactionWatcher { let timeout = after(duration); loop { let r = select! { - recv(reactions) -> r => h.handle_reaction(&*r.unwrap()), + recv(reactions) -> r => { let (r, is_added) = r.unwrap(); h.handle_reaction(&*r, is_added) }, recv(timeout) -> _ => break, }; if let Err(v) = r { diff --git a/youmubot/src/main.rs b/youmubot/src/main.rs index bff0c7a..7cc6c30 100644 --- a/youmubot/src/main.rs +++ b/youmubot/src/main.rs @@ -2,7 +2,10 @@ use dotenv; use dotenv::var; use serenity::{ framework::standard::{DispatchError, StandardFramework}, - model::{channel::{Message, Reaction}, gateway}, + model::{ + channel::{Message, Reaction}, + gateway, + }, }; use youmubot_prelude::*; @@ -27,7 +30,15 @@ impl EventHandler for Handler { } fn reaction_add(&self, ctx: Context, reaction: Reaction) { - ctx.data.get_cloned::().send(reaction); + ctx.data + .get_cloned::() + .send(reaction, true); + } + + fn reaction_remove(&self, ctx: Context, reaction: Reaction) { + ctx.data + .get_cloned::() + .send(reaction, false); } }