Prelude/Main: rework hook system

This commit is contained in:
Natsu Kagami 2020-09-07 19:41:59 -04:00
parent b819509244
commit 238e44a64c
Signed by: nki
GPG key ID: 73376E117CD20735
5 changed files with 96 additions and 47 deletions

View file

@ -0,0 +1,24 @@
use crate::{async_trait, future, Context, Result};
use serenity::model::channel::Message;
/// Hook represents the asynchronous hook that is run on every message.
#[async_trait]
pub trait Hook: Send + Sync {
async fn call(&mut self, ctx: &Context, message: &Message) -> Result<()>;
}
#[async_trait]
impl<T> Hook for T
where
T: for<'a> FnMut(
&'a Context,
&'a Message,
)
-> std::pin::Pin<Box<dyn future::Future<Output = Result<()>> + 'a + Send>>
+ Send
+ Sync,
{
async fn call(&mut self, ctx: &Context, message: &Message) -> Result<()> {
self(ctx, message).await
}
}

View file

@ -5,11 +5,13 @@ use std::sync::Arc;
pub mod announcer;
pub mod args;
pub mod hook;
pub mod pagination;
pub mod setup;
pub use announcer::{Announcer, AnnouncerHandler};
pub use args::{Duration, UsernameArg};
pub use hook::Hook;
pub use pagination::paginate;
/// Re-exporting async_trait helps with implementing Announcer.