wOxlf/src/config.rs

77 lines
2.0 KiB
Rust

use std::path::Path;
use std::path::PathBuf;
use config::{Config, File};
use serde::{Deserialize, Serialize};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "WOXlf", about = "WOXlf discord bot")]
pub struct Args {
pub cfg_path: PathBuf,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct GameConfig {
pub game_name: String,
pub bot_name: String,
pub vote_phase_name: String,
pub enemy_phase_name: String,
pub player_group_name: String,
pub profile_album_hash: String,
pub first_name: Vec<String>,
pub last_name: Vec<String>,
pub messages: MessageConfig,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct MessageConfig {
pub welcome_message: String,
pub status_message: String,
pub announcement_format: String,
pub tally_message: String,
pub vote_message: String,
pub phase_extend_message: String,
pub name_format: String,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct DiscordConfig {
pub token: String,
pub app_id: u64,
pub host_channel: u64,
pub host_webhook_id: u64,
pub vote_channel: u64,
pub category: u64,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct BotConfig {
pub imgur_client_id: String,
pub game_state_dir: PathBuf,
pub discord_config: DiscordConfig,
pub game_config: Vec<GameConfig>,
}
impl BotConfig {
pub fn new(config_path: &Path) -> Result<Self, config::ConfigError> {
let cfg = Config::builder()
.add_source(File::from(config_path))
.build()?;
cfg.try_deserialize()
}
pub fn get_game_config(&self, game_name: &str) -> Option<GameConfig> {
let game_name = game_name.to_lowercase();
self.game_config
.iter()
.find(|g| g.game_name.to_lowercase() == game_name)
.cloned()
}
pub fn get_game_state_path(&self) -> PathBuf {
self.game_state_dir.join("wOxlf_data.toml")
}
}