2024-01-24 18:14:50 +08:00
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use serde::Deserialize;
|
2024-02-20 17:15:14 +08:00
|
|
|
use toml::from_str;
|
2024-03-13 01:17:50 +08:00
|
|
|
use tracing::warn;
|
2024-01-24 18:14:50 +08:00
|
|
|
|
2024-02-18 22:43:54 +08:00
|
|
|
/// Icalingua bot 的配置
|
2024-02-20 17:47:45 +08:00
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
2024-01-24 18:14:50 +08:00
|
|
|
pub struct IcaConfig {
|
|
|
|
/// icalingua 私钥
|
|
|
|
pub private_key: String,
|
|
|
|
/// icalingua 服务器地址
|
|
|
|
pub host: String,
|
|
|
|
/// bot 的 qq
|
|
|
|
pub self_id: u64,
|
|
|
|
/// 提醒的房间
|
|
|
|
pub notice_room: Vec<i64>,
|
|
|
|
/// 是否提醒
|
2024-02-20 18:22:59 +08:00
|
|
|
pub notice_start: bool,
|
|
|
|
/// 管理员列表
|
|
|
|
pub admin_list: Vec<i64>,
|
2024-02-22 23:17:20 +08:00
|
|
|
/// 过滤列表
|
|
|
|
pub filter_list: Vec<i64>,
|
2024-03-12 00:16:12 +08:00
|
|
|
}
|
|
|
|
|
2024-03-15 00:40:22 +08:00
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct PyConfig {
|
|
|
|
/// 插件路径
|
|
|
|
pub plugin_path: String,
|
|
|
|
/// 配置文件路径
|
|
|
|
pub config_path: String,
|
|
|
|
}
|
|
|
|
|
2024-03-12 00:16:12 +08:00
|
|
|
/// 主配置
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct BotConfig {
|
2024-03-12 00:47:00 +08:00
|
|
|
/// 是否启用 icalingua
|
2024-03-13 01:17:50 +08:00
|
|
|
pub enable_ica: Option<bool>,
|
2024-03-12 00:16:12 +08:00
|
|
|
/// Ica 配置
|
|
|
|
pub ica: Option<IcaConfig>,
|
2024-03-29 02:03:27 +08:00
|
|
|
|
2024-03-15 00:40:22 +08:00
|
|
|
/// 是否启用 Python 插件
|
|
|
|
pub enable_py: Option<bool>,
|
|
|
|
/// Python 插件配置
|
|
|
|
pub py: Option<PyConfig>,
|
2024-01-24 18:14:50 +08:00
|
|
|
}
|
|
|
|
|
2024-03-12 00:16:12 +08:00
|
|
|
impl BotConfig {
|
2024-01-24 18:14:50 +08:00
|
|
|
pub fn new_from_path(config_file_path: String) -> Self {
|
|
|
|
// try read config from file
|
|
|
|
let config = fs::read_to_string(&config_file_path).expect("Failed to read config file");
|
2024-02-20 17:15:14 +08:00
|
|
|
let ret: Self = from_str(&config)
|
2024-03-13 01:20:41 +08:00
|
|
|
.unwrap_or_else(|_| panic!("Failed to parse config file {}", &config_file_path));
|
2024-01-24 18:14:50 +08:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
pub fn new_from_cli() -> Self {
|
2024-03-16 14:04:30 +08:00
|
|
|
// let config_file_path = env::args().nth(1).expect("No config path given");
|
|
|
|
// -c <config_file_path>
|
|
|
|
let mut config_file_path = String::new();
|
|
|
|
let mut args = env::args();
|
|
|
|
while let Some(arg) = args.next() {
|
|
|
|
if arg == "-c" {
|
|
|
|
config_file_path = args.next().expect("No config path given");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 18:14:50 +08:00
|
|
|
Self::new_from_path(config_file_path)
|
|
|
|
}
|
2024-03-12 00:16:12 +08:00
|
|
|
|
2024-03-13 01:17:50 +08:00
|
|
|
/// 检查是否启用 ica
|
|
|
|
pub fn check_ica(&self) -> bool {
|
|
|
|
match self.enable_ica {
|
|
|
|
Some(enable) => {
|
2024-03-14 01:12:08 +08:00
|
|
|
if enable && self.ica.is_none() {
|
|
|
|
warn!("enable_ica 为 true 但未填写 [ica] 配置\n将不启用 ica");
|
2024-03-13 01:17:50 +08:00
|
|
|
false
|
2024-03-14 01:12:08 +08:00
|
|
|
} else {
|
|
|
|
true
|
2024-03-13 01:17:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
2024-03-13 01:20:41 +08:00
|
|
|
if self.ica.is_some() {
|
2024-03-13 01:17:50 +08:00
|
|
|
warn!("未填写 enable_ica 但填写了 [ica] 配置\n将不启用 ica");
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 00:40:22 +08:00
|
|
|
/// 检查是否启用 Python 插件
|
|
|
|
pub fn check_py(&self) -> bool {
|
|
|
|
match self.enable_py {
|
|
|
|
Some(enable) => {
|
|
|
|
if enable && self.py.is_none() {
|
|
|
|
warn!("enable_py 为 true 但未填写 [py] 配置\n将不启用 Python 插件");
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if self.py.is_some() {
|
|
|
|
warn!("未填写 enable_py 但填写了 [py] 配置\n将不启用 Python 插件");
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 00:16:12 +08:00
|
|
|
pub fn ica(&self) -> IcaConfig { self.ica.clone().expect("No ica config found") }
|
2024-03-15 00:40:22 +08:00
|
|
|
pub fn py(&self) -> PyConfig { self.py.clone().expect("No py config found") }
|
2024-01-24 18:14:50 +08:00
|
|
|
}
|