diff options
author | Nathan Ringo <nathan@remexre.com> | 2024-11-22 00:11:43 -0600 |
---|---|---|
committer | Nathan Ringo <nathan@remexre.com> | 2024-11-22 00:11:43 -0600 |
commit | dd0e6e45c35133ec8a3e2886b7b050484b388d03 (patch) | |
tree | 3ea9f5a098fc9c150473942c97e8b07833440803 /src/main.rs |
Initial commit
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ed10360 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,90 @@ +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) + .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(); + 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(()) +} |