dev 就是 dev

This commit is contained in:
shenjack-5600u 2025-04-08 00:07:37 +08:00
parent 4d0a32ca0e
commit 1711270c9f
Signed by: shenjack
GPG Key ID: FDF9864E11C7E79F
3 changed files with 57 additions and 16 deletions

View File

@ -1,24 +1,37 @@
pub mod ica; pub mod ica;
pub mod tailchat; pub mod tailchat;
use std::collections::HashMap;
use pyo3::{ use pyo3::{
pyclass, pymethods, pyclass, pymethods, pymodule,
types::{PyBool, PyString}, types::{PyBool, PyModule, PyModuleMethods, PyString},
Bound, IntoPyObject, PyAny, PyRef, Bound, IntoPyObject, PyAny, PyRef, PyResult,
}; };
use toml::Value as TomlValue; use toml::Value as TomlValue;
use tracing::{event, Level};
#[derive(Debug, Clone)]
pub enum ConfigItem {
String(String),
Integer(i64),
Float(f64),
Boolean(bool),
Array(Vec<ConfigItem>),
}
#[derive(Clone)] #[derive(Clone)]
#[pyclass] #[pyclass]
#[pyo3(name = "ConfigRequest")] #[pyo3(name = "ConfigItem")]
pub struct ConfigRequestPy { pub struct ConfigItemPy {
pub path: String, pub item: ConfigItem,
} }
#[pymethods] #[derive(Clone)]
impl ConfigRequestPy { #[pyclass]
#[new] #[pyo3(name = "ConfigStorage")]
pub fn py_new(path: String) -> Self { Self { path } } pub struct ConfigStoragePy {
pub keys: HashMap<String, ConfigItemPy>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -61,3 +74,28 @@ impl ConfigDataPy {
impl ConfigDataPy { impl ConfigDataPy {
pub fn new(data: TomlValue) -> Self { Self { data } } pub fn new(data: TomlValue) -> Self { Self { data } }
} }
#[pymodule]
#[pyo3(name = "shenbot_api")]
fn rs_api_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<ConfigDataPy>()?;
m.add_class::<ConfigStoragePy>()?;
Ok(())
}
/// 在 python 初始化之前注册所有需要的类
///
/// WARNING: 这个函数需要在 Python 初始化之前调用,否则会导致报错
///
/// (pyo3 提供的宏会检查一遍, 不过我这里就直接用原始形式了)
pub fn regist_class() {
event!(Level::INFO, "向 Python 注册 Rust 侧模块/函数");
unsafe {
pyo3::ffi::PyImport_AppendInittab(
rs_api_module::__PYO3_NAME.as_ptr(),
Some(rs_api_module::__pyo3_init),
);
}
event!(Level::INFO, "注册完成");
}

View File

@ -521,7 +521,7 @@ fn init_py_with_env_path(path: &str) {
event!(Level::INFO, "根据配置初始化 python 完成"); event!(Level::INFO, "根据配置初始化 python 完成");
} }
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_EXIT => { pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_EXIT => {
event!(Level::ERROR, "初始化 python 时发生错误: EXIT"); event!(Level::ERROR, "不对啊, 怎么刚刚初始化 Python 就 EXIT 了");
} }
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_ERROR => { pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_ERROR => {
event!(Level::ERROR, "初始化 python 时发生错误: ERROR"); event!(Level::ERROR, "初始化 python 时发生错误: ERROR");
@ -537,9 +537,15 @@ pub fn init_py() {
let span = span!(Level::INFO, "py init"); let span = span!(Level::INFO, "py init");
let _enter = span.enter(); let _enter = span.enter();
event!(Level::INFO, "开始初始化 python");
// 注册东西
class::regist_class();
let plugin_path = MainStatus::global_config().py().plugin_path; let plugin_path = MainStatus::global_config().py().plugin_path;
let cli_args = std::env::args().collect::<Vec<String>>(); let cli_args = std::env::args().collect::<Vec<String>>();
if cli_args.contains(&"-env".to_string()) { if cli_args.contains(&"-env".to_string()) {
let env_path = cli_args.iter().find(|&arg| arg != "-env").expect("未找到 -env 参数的值"); let env_path = cli_args.iter().find(|&arg| arg != "-env").expect("未找到 -env 参数的值");
event!(Level::INFO, "找到 -env 参数: {} 正在初始化", env_path); event!(Level::INFO, "找到 -env 参数: {} 正在初始化", env_path);

View File

@ -1,9 +1,6 @@
# icalingua bot # icalingua bot
这是一个基于 icalingua docker 版的 bot 这是一个基于 icalingua-bridge 的 bot
> 出于某个企鹅, 和保护 作者 和 原项目 ( ica ) 的原因 \
> 功能请自行理解
[插件市场(确信)](https://github.com/shenjackyuanjie/shenbot-plugins) [插件市场(确信)](https://github.com/shenjackyuanjie/shenbot-plugins)
@ -24,7 +21,7 @@ uv venv
- 启动 icalingua 后端 - 启动 icalingua 后端
```bash ```bash
# 用你自己的方法启动你的 icalingua 后端 # 用你自己的方法启动你的 icalingua-bridge
# 例如 # 例如
docker start icalingua docker start icalingua
docker-compose up -d docker-compose up -d