Allow IRC options to be configured with TOML

This commit is contained in:
Yash Karandikar 2022-01-04 17:05:29 -06:00
parent 671566a690
commit 3e041b10ef
Signed by: karx
GPG key ID: A794DA2529474BA5
3 changed files with 25 additions and 20 deletions

1
Cargo.lock generated
View file

@ -173,6 +173,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"irc",
"serde",
"serenity",
"tokio",
"toml",

View file

@ -9,6 +9,7 @@ edition = "2021"
anyhow = "1.0.52"
irc = "0.15"
toml = "0.5"
serde = "1.0"
[dependencies.tokio]
version = "1.15.0"

View file

@ -20,7 +20,19 @@ use irc::{
proto::Command,
};
use toml::Value;
use serde::Deserialize;
#[derive(Deserialize)]
struct DircordConfig {
token: String,
webhook: Option<String>,
nickname: Option<String>,
server: String,
port: Option<u16>,
channels: Vec<String>,
mode: Option<String>,
tls: Option<bool>,
}
struct Handler;
@ -85,34 +97,25 @@ impl TypeMapKey for SenderKey {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let filename = env::args().nth(1).expect("No filename was provided!");
let filename = env::args().nth(1).unwrap_or(String::from("config.toml"));
let mut data = String::new();
File::open(filename)?.read_to_string(&mut data)?;
let value = data.parse::<Value>()?;
let conf: DircordConfig = toml::from_str(&data)?;
let token = value["token"]
.as_str()
.expect("No token provided!")
.to_string();
let webhook = value
.get("webhook")
.map(|v| v.as_str().map(|s| s.to_string()))
.flatten();
let mut discord_client = DiscordClient::builder(&token)
let mut discord_client = DiscordClient::builder(&conf.token)
.event_handler(Handler)
.await?;
let channel_id = ChannelId(831255708875751477);
let config = Config {
nickname: Some("dircord".to_string()),
server: Some("192.168.1.28".to_owned()),
port: Some(6667),
channels: vec!["#no-normies".to_string()],
use_tls: Some(false),
umodes: Some("+B".to_string()),
nickname: conf.nickname,
server: Some(conf.server),
port: conf.port,
channels: conf.channels,
use_tls: conf.tls,
umodes: conf.mode,
..Config::default()
};
@ -134,7 +137,7 @@ async fn main() -> anyhow::Result<()> {
data.insert::<SenderKey>(irc_client.sender());
}
let webhook = parse_webhook_url(http.clone(), webhook)
let webhook = parse_webhook_url(http.clone(), conf.webhook)
.await
.expect("Invalid webhook URL");