mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-05-24 17:20:49 +00:00
Prelude/Main: rework hook system
This commit is contained in:
parent
b819509244
commit
238e44a64c
5 changed files with 96 additions and 47 deletions
24
youmubot-prelude/src/hook.rs
Normal file
24
youmubot-prelude/src/hook.rs
Normal 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
|
||||
}
|
||||
}
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue