mod labwatch; mod quit_twitter; use serenity::{ all::{ChannelId, Context, EventHandler, Message, Ready}, async_trait, }; use std::{ops::ControlFlow, path::PathBuf, sync::Arc}; pub struct Handler(Arc); impl From for Handler { fn from(config: HandlerConfig) -> Handler { Handler(Arc::new(config)) } } pub struct HandlerConfig { pub db_dir: PathBuf, pub labwatch_channel_id: ChannelId, } #[async_trait] impl EventHandler for Handler { async fn ready(&self, ctx: Context, _data_about_bot: Ready) { tokio::spawn(labwatch::start(self.0.clone(), ctx)); } async fn message(&self, ctx: Context, msg: Message) { // Apologies for the macro, but Rust does some control-flow analysis on async functions // that it's much easier to pass when all your calls are direct calls. macro_rules! handlers { ($($handler:expr),* $(,)?) => { $(match $handler(&self.0, &ctx, &msg).await { Ok(ControlFlow::Break(())) => return, Ok(ControlFlow::Continue(())) => (), Err(err) => log::error!("{err:?}"), })* }; } handlers![quit_twitter::on_message]; } }