2024-02-17 01:28:24 +00:00
|
|
|
use std::{fs, io::Write};
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct Config {
|
2024-02-21 08:58:26 +00:00
|
|
|
pub discord_token: String,
|
|
|
|
pub lavalink_address: String,
|
|
|
|
pub lavalink_password: String,
|
|
|
|
pub user_id: u64
|
2024-02-17 01:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-02-21 08:58:26 +00:00
|
|
|
discord_token: "paste_your_token".to_string(),
|
|
|
|
lavalink_address: "paste_your_lavalink_address".to_string(),
|
|
|
|
lavalink_password: "paste_your_lavalink_password".to_string(),
|
|
|
|
user_id: 1
|
2024-02-17 01:28:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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.")
|
|
|
|
}
|