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
|
mod handlers;
use crate::handlers::*;
use anyhow::{Context as _, Result};
use clap::{value_parser, ArgAction, Parser};
use serde::Deserialize;
use serenity::{
all::{ActivityData, GatewayIntents, GuildMemberUpdateEvent, Member, Ready},
async_trait,
client::{Context, EventHandler},
Client,
};
use std::{fs, path::PathBuf};
use stderrlog::StdErrLog;
#[derive(Debug, Deserialize)]
struct Config {
discord_token: String,
}
#[derive(Debug, Parser)]
struct Args {
/// The path to the configuration file.
config_path: PathBuf,
/// Decreases the log level.
#[clap(
short,
long,
conflicts_with("verbose"),
action = ArgAction::Count,
value_parser = value_parser!(u8).range(..=2)
)]
quiet: u8,
/// Increases the log level.
#[clap(
short,
long,
conflicts_with("quiet"),
action = ArgAction::Count,
value_parser = value_parser!(u8).range(..=3)
)]
verbose: u8,
}
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();
// Set up logging.
{
let mut logger = StdErrLog::new();
match args.quiet {
0 => logger.verbosity(1 + args.verbose as usize),
1 => logger.verbosity(0),
2 => logger.quiet(true),
// UNREACHABLE: A maximum of two occurrences of quiet are allowed.
_ => unreachable!(),
};
// UNWRAP: No other logger should be set up.
logger.show_module_names(true).init().unwrap()
}
let config_str = fs::read_to_string(&args.config_path)
.with_context(|| format!("failed to read {}", args.config_path.display()))?;
let config: Config = toml::from_str(&config_str)
.with_context(|| format!("failed to parse {}", args.config_path.display()))?;
drop(config_str);
let handler = MultiHandler(vec![Box::new(PresenceSetter), Box::new(X500Mapper)]);
let mut client = Client::builder(
&config.discord_token,
GatewayIntents::default() | GatewayIntents::GUILD_MEMBERS,
)
.event_handler(handler)
.await
.context("failed to create Discord client")?;
client
.start()
.await
.context("failed to start Discord client")?;
Ok(())
}
|