This commit is contained in:
moonleay 2024-02-17 02:28:24 +01:00
commit 9dad9bbda3
Signed by: moonleay
GPG key ID: 82667543CCD715FB
9 changed files with 2152 additions and 0 deletions

33
src/util/config.rs Normal file
View file

@ -0,0 +1,33 @@
use std::{fs, io::Write};
use serde::{Deserialize, Serialize};
use std::error::Error;
#[derive(Deserialize, Serialize)]
pub struct Config {
pub discord_token: String
}
const CONFIG_FILE: &str = "./data/config.json";
pub fn load() -> Result<Config, Box<dyn Error>> {
// TODO: load config, create empty config if there is no config, stop if there is no complete config
let config_file = match fs::File::open(CONFIG_FILE) {
Ok(file) => file,
Err(_) => create_empty()
};
let config_file = serde_json::from_reader(config_file).unwrap();
Ok(config_file)
}
fn create_empty() -> fs::File{
let example_config = Config {
discord_token: "paste_your_config".to_string()
};
let mut config_file = fs::File::create(CONFIG_FILE).unwrap();
let file_content = serde_json::to_string(&example_config).unwrap();
config_file.write_all(&file_content.as_bytes()).unwrap();
panic!("There is no config. But now there is a template.")
}

1
src/util/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod config;