summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 71f2d5d437480616fe14a3fd1754238f6535d52e (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::path::PathBuf;

use anyhow::{Context, Result};
use clap::{value_parser, ArgAction, Parser};
use log::LevelFilter;
use minneplbot::{Handler, HandlerConfig};
use serenity::{
    all::{ChannelId, GatewayIntents},
    Client,
};
use simple_logger::SimpleLogger;

#[derive(Debug, Parser)]
struct Args {
    /// Decreases the log level.
    #[clap(
        short,
        long,
        conflicts_with("verbose"),
        action = ArgAction::Count,
        value_parser = value_parser!(u8).range(..=3)
    )]
    quiet: u8,

    /// Increases the log level.
    #[clap(
        short,
        long,
        conflicts_with("quiet"),
        action = ArgAction::Count,
        value_parser = value_parser!(u8).range(..=2)
    )]
    verbose: u8,

    /// The directory to store files in.
    #[clap(env = "DB_DIR")]
    db_dir: PathBuf,

    /// The Discord API token. Pass this via an environment variable, _not_ the command-line!
    #[clap(
        long = "do-not-pass-the-discord-token-via-the-command-line",
        env = "DISCORD_TOKEN"
    )]
    discord_token: String,

    /// The channel ID of the #labwatch channel.
    #[clap(long = "labwatch-channel-id", env = "LABWATCH_CHANNEL_ID")]
    labwatch_channel_id: ChannelId,
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
    // Parse the arguments.
    let args = Args::parse();

    // Compute the log level to use.
    let log_level = match (args.quiet, args.verbose) {
        (0, 0) => LevelFilter::Info,
        (0, 1) => LevelFilter::Debug,
        (0, 2) => LevelFilter::Trace,
        (1, _) => LevelFilter::Warn,
        (2, _) => LevelFilter::Error,
        (_, _) => LevelFilter::Off,
    };
    SimpleLogger::new()
        .with_level(log_level)
        .with_module_level("tracing::span", LevelFilter::Warn)
        .init()
        .context("Failed to configure logger")?;

    // Promise that we aren't gonna call setenv.
    unsafe {
        use time::util::local_offset::{set_soundness, Soundness};
        set_soundness(Soundness::Unsound);
    }

    // Start up the client.
    let intents = GatewayIntents::default() | GatewayIntents::MESSAGE_CONTENT;
    Client::builder(&args.discord_token, intents)
        .event_handler(Handler::from(HandlerConfig {
            db_dir: args.db_dir,
            labwatch_channel_id: args.labwatch_channel_id,
        }))
        .await
        .context("failed to create Discord client")?
        .start()
        .await
        .context("failed to start Discord client")?;

    Ok(())
}