mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-16 07:18:54 +00:00
Reaction watcher with fixed duration
This commit is contained in:
parent
6ce110c203
commit
3e42c74c78
2 changed files with 26 additions and 2 deletions
|
@ -95,7 +95,7 @@ pub fn vote(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
|||
let reaction_to_choice: Map<_, _> = choices.iter().map(|r| (r.0, &r.1)).collect();
|
||||
let mut user_reactions: Map<UserId, Vec<&str>> = Map::new();
|
||||
|
||||
ctx.data.get_cloned::<ReactionWatcher>().handle_reactions(
|
||||
ctx.data.get_cloned::<ReactionWatcher>().handle_reactions_timed(
|
||||
|reaction: &Reaction, is_add| {
|
||||
if reaction.message_id != panel.id {
|
||||
return Ok(());
|
||||
|
|
|
@ -50,7 +50,7 @@ impl ReactionWatcher {
|
|||
}
|
||||
/// React! to a series of reaction
|
||||
///
|
||||
/// The reactions stop after `duration`.
|
||||
/// The reactions stop after `duration` of idle.
|
||||
pub fn handle_reactions(
|
||||
&self,
|
||||
mut h: impl ReactionHandler,
|
||||
|
@ -72,4 +72,28 @@ impl ReactionWatcher {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
/// React! to a series of reaction
|
||||
///
|
||||
/// The handler will stop after `duration` no matter what.
|
||||
pub fn handle_reactions_timed(
|
||||
&self,
|
||||
mut h: impl ReactionHandler,
|
||||
duration: std::time::Duration,
|
||||
) -> CommandResult {
|
||||
let (send, reactions) = bounded(0);
|
||||
{
|
||||
self.channels.lock().expect("Poisoned!").push(send);
|
||||
}
|
||||
let timeout = after(duration);
|
||||
loop {
|
||||
let r = select! {
|
||||
recv(reactions) -> r => { let (r, is_added) = r.unwrap(); h.handle_reaction(&*r, is_added) },
|
||||
recv(timeout) -> _ => break,
|
||||
};
|
||||
if let Err(v) = r {
|
||||
dbg!(v);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue