icalingua-python-bot/ica-rs/src/config.rs

133 lines
4.0 KiB
Rust
Raw Normal View History

use std::env;
use std::fs;
2024-08-02 19:25:39 +08:00
use colored::Colorize;
use serde::Deserialize;
2024-02-20 17:15:14 +08:00
use toml::from_str;
2024-03-30 14:24:19 +08:00
use crate::data_struct::{ica, tailchat};
/// Icalingua bot 的配置
2024-02-20 17:47:45 +08:00
#[derive(Debug, Clone, Deserialize)]
pub struct IcaConfig {
/// icalingua 私钥
pub private_key: String,
/// icalingua 服务器地址
pub host: String,
/// bot 的 qq
2024-03-30 14:24:19 +08:00
pub self_id: ica::UserId,
/// 提醒的房间
2024-08-18 02:04:32 +08:00
#[serde(default = "default_empty_i64_vec")]
2024-03-30 14:24:19 +08:00
pub notice_room: Vec<ica::RoomId>,
/// 是否提醒
2024-08-18 02:04:32 +08:00
#[serde(default = "default_false")]
pub notice_start: bool,
/// 管理员列表
2024-08-18 02:04:32 +08:00
#[serde(default = "default_empty_i64_vec")]
2024-03-30 14:24:19 +08:00
pub admin_list: Vec<ica::UserId>,
2024-02-22 23:17:20 +08:00
/// 过滤列表
2024-08-18 02:04:32 +08:00
#[serde(default = "default_empty_i64_vec")]
2024-03-30 14:24:19 +08:00
pub filter_list: Vec<ica::UserId>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct TailchatConfig {
/// 服务器地址
pub host: String,
/// 机器人 App ID
pub app_id: String,
/// 机器人 App Secret
pub app_secret: String,
/// 提醒的房间
pub notice_room: Vec<(tailchat::GroupId, tailchat::ConverseId)>,
/// 是否提醒
2024-08-18 02:04:32 +08:00
#[serde(default = "default_false")]
2024-03-30 14:24:19 +08:00
pub notice_start: bool,
/// 管理员列表
2024-08-18 02:04:32 +08:00
#[serde(default = "default_empty_str_vec")]
2024-03-30 14:24:19 +08:00
pub admin_list: Vec<tailchat::UserId>,
/// 过滤列表
2024-08-18 02:04:32 +08:00
#[serde(default = "default_empty_str_vec")]
2024-03-30 14:24:19 +08:00
pub filter_list: Vec<tailchat::UserId>,
2024-03-12 00:16:12 +08:00
}
2024-08-18 02:04:32 +08:00
fn default_plugin_path() -> String { "./plugins".to_string() }
fn default_config_path() -> String { "./config".to_string() }
#[derive(Debug, Clone, Deserialize)]
pub struct PyConfig {
/// 插件路径
2024-08-18 02:04:32 +08:00
#[serde(default = "default_plugin_path")]
pub plugin_path: String,
2024-08-18 02:04:32 +08:00
/// 配置文件夹路径
#[serde(default = "default_config_path")]
pub config_path: String,
}
2024-08-18 02:04:32 +08:00
fn default_empty_i64_vec() -> Vec<i64> { Vec::new() }
fn default_empty_str_vec() -> Vec<String> { Vec::new() }
fn default_false() -> bool { false }
2024-03-12 00:16:12 +08:00
/// 主配置
#[derive(Debug, Clone, Deserialize)]
pub struct BotConfig {
/// 是否启用 icalingua
2024-08-18 02:04:32 +08:00
#[serde(default = "default_false")]
pub enable_ica: bool,
2024-03-12 00:16:12 +08:00
/// Ica 配置
pub ica: Option<IcaConfig>,
2024-03-30 14:24:19 +08:00
/// 是否启用 Tailchat
2024-08-18 02:04:32 +08:00
#[serde(default = "default_false")]
pub enable_tailchat: bool,
2024-03-30 14:24:19 +08:00
/// Tailchat 配置
pub tailchat: Option<TailchatConfig>,
/// 是否启用 Python 插件
2024-08-18 02:04:32 +08:00
#[serde(default = "default_false")]
pub enable_py: bool,
/// Python 插件配置
pub py: Option<PyConfig>,
}
2024-03-12 00:16:12 +08:00
impl BotConfig {
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-08-02 21:34:28 +08:00
let ret: Self = from_str(&config).unwrap_or_else(|e| {
panic!("Failed to parse config file {}\ne:{:?}", &config_file_path, e)
});
ret
}
pub fn new_from_cli() -> Self {
// 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" {
2024-08-18 02:04:32 +08:00
config_file_path = args.next().unwrap_or_else(|| {
panic!("{}", "No config path given\nUsage: -c <config_file_path>".red())
});
break;
}
}
Self::new_from_path(config_file_path)
}
2024-03-12 00:16:12 +08:00
2024-03-13 01:17:50 +08:00
/// 检查是否启用 ica
2024-08-18 02:04:32 +08:00
pub fn check_ica(&self) -> bool { self.enable_ica }
2024-03-13 01:17:50 +08:00
2024-03-30 16:59:06 +08:00
/// 检查是否启用 Tailchat
2024-08-18 02:04:32 +08:00
pub fn check_tailchat(&self) -> bool { self.enable_tailchat }
2024-03-30 16:59:06 +08:00
/// 检查是否启用 Python 插件
2024-08-18 02:04:32 +08:00
pub fn check_py(&self) -> bool { self.enable_py }
2024-03-12 00:16:12 +08:00
pub fn ica(&self) -> IcaConfig { self.ica.clone().expect("No ica config found") }
2024-03-30 18:30:43 +08:00
pub fn tailchat(&self) -> TailchatConfig {
self.tailchat.clone().expect("No tailchat config found")
}
pub fn py(&self) -> PyConfig { self.py.clone().expect("No py config found") }
}