summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: d24d38621f0293fcfb46a4af73388ef96843f306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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<HandlerConfig>);

impl From<HandlerConfig> 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];
    }
}