2020-04-26 20:06:55 +00:00
|
|
|
use chrono_tz::Tz;
|
|
|
|
use config::{Config, ConfigError, File};
|
|
|
|
use serde::de::{self, Error, Visitor};
|
|
|
|
use serde::{Deserialize, Deserializer};
|
2020-04-23 03:28:51 +00:00
|
|
|
use serenity::prelude::TypeMapKey;
|
2020-04-26 20:06:55 +00:00
|
|
|
use std::fmt;
|
2020-04-23 03:28:51 +00:00
|
|
|
|
2020-08-08 23:09:07 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct EventReminder {
|
|
|
|
pub msg: String,
|
|
|
|
pub reminder_time: u32,
|
|
|
|
}
|
|
|
|
|
2020-04-27 23:16:03 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2020-04-23 03:28:51 +00:00
|
|
|
pub struct HypeBotConfig {
|
|
|
|
pub db_url: String,
|
|
|
|
pub default_thumbnail_link: String,
|
|
|
|
pub discord_key: String,
|
|
|
|
pub prefix: String,
|
|
|
|
pub event_channel: u64,
|
2020-04-24 00:45:02 +00:00
|
|
|
pub event_roles: Vec<u64>,
|
2020-07-10 03:05:01 +00:00
|
|
|
pub ping_roles: Vec<u64>,
|
2020-04-26 20:06:55 +00:00
|
|
|
#[serde(deserialize_with = "from_tz_string")]
|
|
|
|
pub event_timezone: Tz,
|
2020-04-27 23:16:03 +00:00
|
|
|
pub log_path: String,
|
2020-08-08 23:09:07 +00:00
|
|
|
pub reminders: Option<Vec<EventReminder>>,
|
2020-04-26 20:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ConfigValueVisitor;
|
|
|
|
impl<'de> Visitor<'de> for ConfigValueVisitor {
|
|
|
|
type Value = String;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(formatter, "Unable to parse timezone field.")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: de::Error,
|
|
|
|
{
|
|
|
|
Ok(s.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tz_string<'de, D>(deserializer: D) -> Result<Tz, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let string = deserializer.deserialize_struct("Value", &["into_str"], ConfigValueVisitor)?;
|
|
|
|
|
2021-01-16 03:51:26 +00:00
|
|
|
let tz: Tz = string.parse().ok().ok_or_else(|| {
|
|
|
|
D::Error::custom("Unable to parse datetime, should be in format \"Country/City\"")
|
|
|
|
})?;
|
2020-04-26 20:06:55 +00:00
|
|
|
|
|
|
|
Ok(tz)
|
2020-04-23 03:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HypeBotConfig {
|
|
|
|
pub fn new(config_path: &str) -> Result<Self, ConfigError> {
|
|
|
|
let mut cfg = Config::new();
|
|
|
|
cfg.merge(File::with_name(config_path))?;
|
|
|
|
|
|
|
|
cfg.try_into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeMapKey for HypeBotConfig {
|
|
|
|
type Value = HypeBotConfig;
|
|
|
|
}
|