127 lines
3.3 KiB
Rust
127 lines
3.3 KiB
Rust
pub mod common;
|
|
|
|
use chrono::{Datelike, SecondsFormat, Utc};
|
|
use dotenvy::dotenv;
|
|
use rand::{distr::Alphanumeric, rng, Rng};
|
|
use serenity::model::id::{ChannelId, GuildId, RoleId};
|
|
use serenity::prelude::TypeMapKey;
|
|
use std::{env, sync::Arc};
|
|
use tokio::sync::RwLock;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Config {
|
|
// manages where teh database is stored
|
|
pub home: String,
|
|
pub database: String,
|
|
|
|
// tokens for discord and other API's
|
|
pub discord_token: String,
|
|
pub discord_token_minecraft: String,
|
|
pub minecraft_mcprofile: String,
|
|
|
|
// email settings
|
|
pub mail_smtp: String,
|
|
pub mail_user: String,
|
|
pub mail_pass: String,
|
|
|
|
// wolves API base for clubs/socs
|
|
pub wolves_url: String,
|
|
// API key for accessing more general resources
|
|
pub wolves_api: String,
|
|
|
|
// discord server for committee
|
|
pub committee_server: GuildId,
|
|
pub committee_role: RoleId,
|
|
pub committee_category: ChannelId,
|
|
}
|
|
impl TypeMapKey for Config {
|
|
type Value = Arc<RwLock<Config>>;
|
|
}
|
|
|
|
pub fn get_config() -> Config {
|
|
dotenv().ok();
|
|
|
|
// reasonable defaults
|
|
let mut config = Config {
|
|
discord_token: "".to_string(),
|
|
discord_token_minecraft: "".to_string(),
|
|
minecraft_mcprofile: "".to_string(),
|
|
|
|
home: ".".to_string(),
|
|
database: "database.db".to_string(),
|
|
|
|
mail_smtp: "".to_string(),
|
|
mail_user: "".to_string(),
|
|
mail_pass: "".to_string(),
|
|
wolves_url: "".to_string(),
|
|
wolves_api: "".to_string(),
|
|
committee_server: GuildId::new(1),
|
|
committee_role: RoleId::new(1),
|
|
committee_category: ChannelId::new(1),
|
|
};
|
|
|
|
if let Ok(x) = env::var("DATABASE_HOME") {
|
|
config.home = x.trim().to_string();
|
|
}
|
|
if let Ok(x) = env::var("DATABASE") {
|
|
config.database = x.trim().to_string();
|
|
}
|
|
|
|
if let Ok(x) = env::var("DISCORD_TOKEN") {
|
|
config.discord_token = x.trim().to_string();
|
|
}
|
|
if let Ok(x) = env::var("DISCORD_TOKEN_MINECRAFT") {
|
|
config.discord_token_minecraft = x.trim().to_string();
|
|
}
|
|
if let Ok(x) = env::var("MINECRAFT_MCPROFILE_KEY") {
|
|
config.minecraft_mcprofile = x.trim().to_string();
|
|
}
|
|
|
|
if let Ok(x) = env::var("EMAIL_SMTP") {
|
|
config.mail_smtp = x.trim().to_string();
|
|
}
|
|
if let Ok(x) = env::var("EMAIL_USER") {
|
|
config.mail_user = x.trim().to_string();
|
|
}
|
|
if let Ok(x) = env::var("EMAIL_PASS") {
|
|
config.mail_pass = x.trim().to_string();
|
|
}
|
|
|
|
if let Ok(x) = env::var("WOLVES_URL_BASE") {
|
|
config.wolves_url = x.trim().to_string();
|
|
}
|
|
if let Ok(x) = env::var("WOLVES_API") {
|
|
config.wolves_api = x.trim().to_string();
|
|
}
|
|
|
|
if let Ok(x) = env::var("COMMITTEE_DISCORD") {
|
|
if let Ok(x) = x.trim().parse::<u64>() {
|
|
config.committee_server = GuildId::new(x);
|
|
}
|
|
}
|
|
if let Ok(x) = env::var("COMMITTEE_DISCORD") {
|
|
if let Ok(x) = x.trim().parse::<u64>() {
|
|
config.committee_role = RoleId::new(x);
|
|
}
|
|
}
|
|
if let Ok(x) = env::var("COMMITTEE_CATEGORY") {
|
|
if let Ok(x) = x.trim().parse::<u64>() {
|
|
config.committee_category = ChannelId::new(x);
|
|
}
|
|
}
|
|
|
|
config
|
|
}
|
|
|
|
pub fn get_now_iso(short: bool) -> String {
|
|
let now = Utc::now();
|
|
if short {
|
|
format!("{}-{:02}-{:02}", now.year(), now.month(), now.day())
|
|
} else {
|
|
now.to_rfc3339_opts(SecondsFormat::Millis, true)
|
|
}
|
|
}
|
|
|
|
pub fn random_string(len: usize) -> String {
|
|
rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
|
|
}
|