Compare commits

..

No commits in common. "1f7ffcb2d4d1ecaf662e43cc7ed70848713db061" and "09aaccf291b14c33f0cd4cb761a58162cf662953" have entirely different histories.

11 changed files with 63 additions and 200 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
venv
env*
env
config.toml

View File

@ -10,8 +10,6 @@ notice_start = true # 是否在启动 bot 后通知
# 机器人的管理员
admin_list = [0] # 机器人的管理员
# 过滤的人
filter_list = [0]
# python 插件路径
py_plugin_path = "/path/to/your/plugin"

View File

@ -1,6 +1,6 @@
[package]
name = "ica-rs"
version = "0.4.8"
version = "0.4.6"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -9,6 +9,7 @@ edition = "2021"
ed25519 = "2.2.3"
ed25519-dalek = "2.1.1"
hex = "0.4.3"
blake3 = "1.5.0"
rust_socketio = { version = "0.4.4", features = ["async"]}
serde = { version = "1.0", features = ["derive"] }

View File

@ -6,7 +6,7 @@ else:
NewMessage = TypeVar("NewMessage")
IcaClient = TypeVar("IcaClient")
_version_ = "1.1.0"
_version_ = "1.0.0"
def on_message(msg: NewMessage, client: IcaClient) -> None:
if not (msg.is_from_self or msg.is_reply):

View File

@ -1,8 +1,8 @@
import re
import time
import requests
from typing import TYPE_CHECKING, TypeVar, Optional
from typing import TYPE_CHECKING, TypeVar
if TYPE_CHECKING:
from ica_typing import NewMessage, IcaClient
@ -10,7 +10,7 @@ else:
NewMessage = TypeVar("NewMessage")
IcaClient = TypeVar("IcaClient")
_version_ = "2.1.2-rs"
_version_ = "2.0.0-rs"
def format_data_size(data_bytes: float) -> str:
data_lens = ["B", "KB", "MB", "GB", "TB"]
@ -46,24 +46,18 @@ def format_hit_count(count: int) -> str:
else:
return "_".join(count_str[i:i + 4] for i in range(0, count_len, 4))
def wrap_request(url: str, client: IcaClient) -> Optional[dict]:
response = requests.get(url)
def bmcl(msg: NewMessage, client: IcaClient) -> None:
req_time = time.time()
# 记录请求时间
response = requests.get("https://bd.bangbang93.com/openbmclapi/metric/dashboard")
if not response.status_code == 200 or response.reason != "OK":
reply = msg.reply_with(f"请求数据失败\n{response.status_code}")
client.warn(
f"数据请求失败, 请检查网络\n{response.status}"
)
return None
return response.json()
def bmcl_dashboard(msg: NewMessage, client: IcaClient) -> None:
req_time = time.time()
# 记录请求时间
data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/dashboard", client)
if data is None:
client.send_message(reply)
return
data = response.json()
data_bytes: float = data["bytes"]
data_hits: int = data["hits"]
data_bandwidth: float = data["currentBandwidth"]
@ -74,7 +68,7 @@ def bmcl_dashboard(msg: NewMessage, client: IcaClient) -> None:
hits_count = format_hit_count(data_hits)
report_msg = (
f"OpenBMCLAPI 面板v{_version_}-状态\n"
f"OpenBMCLAPI 状态面板v{_version_} :\n"
f"实时信息: {online_node} 带宽: {online_bandwidth}Mbps\n"
f"负载: {load_str:.2f}% 带宽: {data_bandwidth:.2f}Mbps\n"
f"当日请求: {hits_count} 数据量: {data_len}\n"
@ -86,111 +80,7 @@ def bmcl_dashboard(msg: NewMessage, client: IcaClient) -> None:
client.send_message(reply)
def parse_rank(data: dict) -> dict:
rank_data = {"hits": 0, "bytes": 0}
if "metric" in data:
rank_data["hits"] = data["metric"]["hits"]
rank_data["bytes"] = data["metric"]["bytes"]
return {
"name": data["name"],
"start": data["isEnabled"],
# "full": "全量" if "fullSize" in data else "分片",
# "version": data["version"] if "version" in data else "未知版本",
"owner": data["sponsor"]["name"] if "sponsor" in data else "未知",
"rank": rank_data
}
def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None:
req_time = time.time()
# 记录请求时间
rank_data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/rank", client)
if rank_data is None:
return
ranks = [parse_rank(data) for data in rank_data]
if name is None:
# 全部排名
# 显示前3名
limit = 3
if len(ranks) < limit:
show_ranks = ranks
else:
show_ranks = ranks[:limit]
rank_msg = (
f"{'' if r['start'] else ''}名称: {r['name']}\n"
# f"-{rank['full']} \n"
# f"版本: {r['version']}\n"
f"赞助者: {r['owner']}|"
f"h/d {format_hit_count(r['rank']['hits'])}|{format_data_size(r['rank']['bytes'])}"
for r in show_ranks
)
rank_msg = "\n".join(rank_msg)
report_msg = (
f"OpenBMCLAPI 面板v{_version_}-排名\n{rank_msg}\n"
f"请求时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(req_time))}\n"
)
reply = msg.reply_with(report_msg)
client.info(report_msg)
client.send_message(reply)
return
else:
# 搜索是否有这个名字的节点
names = [r["name"].lower() for r in ranks]
finds = [re.search(name.lower(), n) for n in names]
if not any(finds):
reply = msg.reply_with(f"未找到名为{name}的节点")
client.send_message(reply)
return
# 如果找到 > 3 个节点, 则提示 不显示
counts = [True for find in finds if find]
if len(counts) > 3:
if len(counts) > 10:
reply = msg.reply_with(f"搜索|{name}|到{len(counts)}个节点, 请用更精确的名字")
else:
# 4~10 个节点 只显示名称和次序
find_msg = [f"{'' if r['start'] else ''}{r['name']}-No.{i+1}" for i, r in enumerate(ranks) if finds[i]]
find_msg = "\n".join(find_msg)
report_msg = f"OpenBMCLAPI 面板v{_version_}-搜索|{name}|\n{find_msg}"
reply = msg.reply_with(report_msg)
client.send_message(reply)
return
rank_msgs = []
for i, find in enumerate(finds):
if find:
rank = ranks[i]
rank_msg = (
f"{'' if rank['start'] else ''}名称: {rank['name']}-No.{i+1}\n"
# f"-{rank['full']} \n"
# f"版本: {rank['version']}\n"
f"赞助商: {rank['owner']}|"
f"h/d {format_hit_count(rank['rank']['hits'])}|{format_data_size(rank['rank']['bytes'])}"
)
rank_msgs.append(rank_msg)
rank_msgs = "\n".join(rank_msgs)
report_msg = f"OpenBMCLAPI 面板v{_version_}-排名\n{rank_msgs}"
reply = msg.reply_with(report_msg)
client.info(report_msg)
client.send_message(reply)
return
help = """/bmcl -> dashboard
/bmcl rank -> all rank
/bmcl rank <name> -> rank of <name>"""
def on_message(msg: NewMessage, client: IcaClient) -> None:
if not (msg.is_from_self or msg.is_reply):
if msg.content.startswith("/bmcl"):
if msg.content == "/bmcl":
bmcl_dashboard(msg, client)
elif msg.content == "/bmcl rank":
bmcl_rank(msg, client, None)
elif msg.content.startswith("/bmcl rank") and len(msg.content) > 11:
name = msg.content[11:]
bmcl_rank(msg, client, name)
else:
reply = msg.reply_with(help)
client.send_message(reply)
bmcl(msg, client)

View File

@ -19,8 +19,6 @@ pub struct IcaConfig {
pub notice_start: bool,
/// 管理员列表
pub admin_list: Vec<i64>,
/// 过滤列表
pub filter_list: Vec<i64>,
/// Python 插件路径
pub py_plugin_path: Option<String>,
}

View File

@ -2,7 +2,6 @@ use crate::client::IcalinguaStatus;
use crate::data_struct::files::MessageFile;
use crate::data_struct::{MessageId, RoomId, UserId};
use tracing::warn;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value as JsonValue};
@ -139,15 +138,7 @@ impl NewMessage {
}
// 回复的消息
let reply: Option<ReplyMessage> = match message.get("replyMessage") {
Some(value) => {
match serde_json::from_value::<ReplyMessage>(value.clone()) {
Ok(reply) => Some(reply),
Err(e) => {
warn!("Failed to parse reply message: {}", e);
None
}
}
},
Some(value) => serde_json::from_value::<ReplyMessage>(value.clone()).ok(),
None => None,
};
// At
@ -205,8 +196,7 @@ impl NewMessage {
pub fn output(&self) -> String {
format!(
// >10 >10 >15
"{:>10}|{:>12}|{:<20}|{}",
"Room: {}, Sender: {}|{}, Content: {}",
self.room_id, self.sender_id, self.sender_name, self.content
)
}

View File

@ -3,7 +3,7 @@ use rust_socketio::asynchronous::Client;
use rust_socketio::{Event, Payload};
use tracing::{info, warn};
use crate::client::{send_message, IcalinguaStatus};
use crate::client::send_message;
use crate::data_struct::all_rooms::Room;
use crate::data_struct::messages::NewMessage;
use crate::data_struct::online_data::OnlineData;
@ -16,7 +16,7 @@ pub async fn get_online_data(payload: Payload, _client: Client) {
let online_data = OnlineData::new_from_json(value);
info!(
"update_online_data {}",
format!("{:?}", online_data).cyan()
format!("{:#?}", online_data).cyan()
);
unsafe {
crate::ClientStatus.update_online_data(online_data);
@ -30,12 +30,7 @@ pub async fn add_message(payload: Payload, client: Client) {
if let Payload::Text(values) = payload {
if let Some(value) = values.first() {
let message = NewMessage::new_from_json(value);
// 检测是否在过滤列表内
if IcalinguaStatus::get_config().filter_list.contains(&message.sender_id) {
return;
}
info!("add_message {}", message.output().cyan());
// info!("add_message {}", format!("{:#?}", message).cyan());
// 就在这里处理掉最基本的消息
// 之后的处理交给插件
if message.content.eq("/bot-rs") && !message.is_from_self() && !message.is_reply() {
@ -54,7 +49,7 @@ pub async fn delete_message(payload: Payload, _client: Client) {
// 消息 id
if let Some(value) = values.first() {
if let Some(msg_id) = value.as_str() {
info!("delete_message {}", format!("{}", msg_id).yellow());
warn!("delete_message {}", format!("{}", msg_id).yellow());
}
}
}
@ -93,7 +88,6 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) {
"notify",
"closeLoading", // 发送消息/加载新聊天 有一个 loading
"updateRoom",
"syncRead",
];
match &event {
Event::Custom(event_name) => {

View File

@ -38,7 +38,6 @@ async fn main() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
info!("ica-async-rs v{}", VERSION);
// 从命令行获取 host 和 key
// 从命令行获取配置文件路径

View File

@ -82,12 +82,30 @@ pub fn load_py_plugins(path: &PathBuf) {
let path = entry.path();
if let Some(ext) = path.extension() {
if ext == "py" {
match load_py_module(&path) {
Some((changed_time, py_module)) => {
PyStatus::add_file(path.clone(), changed_time, py_module);
match load_py_file(&path) {
Ok((changed_time, content)) => {
let py_module: PyResult<Py<PyAny>> = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
let module: PyResult<Py<PyAny>> = PyModule::from_code(
py,
&content,
&path.to_string_lossy(),
&path.to_string_lossy()
)
.map(|module| module.into());
module
});
match py_module {
Ok(py_module) => {
info!("加载到插件: {:?}", path);
PyStatus::add_file(path, changed_time, py_module);
}
None => {
warn!("加载 Python 插件: {:?} 失败", path);
Err(e) => {
warn!("failed to load file: {:?} | e: {:?}", path, e);
}
}
}
Err(e) => {
warn!("failed to load file: {:?} | e: {:?}", path, e);
}
}
}
@ -127,12 +145,24 @@ pub fn verify_plugins() {
}
info!("file change list: {:?}", need_reload_files);
for reload_file in need_reload_files {
match load_py_module(&reload_file) {
Some((changed_time, py_module)) => {
match load_py_file(&reload_file) {
Ok((changed_time, content)) => {
let py_module = Python::with_gil(|py| -> Py<PyAny> {
let module: Py<PyAny> = PyModule::from_code(
py,
&content,
&reload_file.to_string_lossy(),
&reload_file.to_string_lossy(),
// !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的
)
.unwrap()
.into();
module
});
PyStatus::add_file(reload_file.clone(), changed_time, py_module);
}
None => {
warn!("重载 Python 插件: {:?} 失败", reload_file);
},
Err(e) => {
warn!("重载 Python 插件: {:?} 失败, e: {:?}", reload_file, e);
}
}
}
@ -150,35 +180,6 @@ pub fn load_py_file(path: &PathBuf) -> std::io::Result<(Option<SystemTime>, Stri
Ok((changed_time, content))
}
pub fn load_py_module(path: &PathBuf) -> Option<(Option<SystemTime>, Py<PyAny>)> {
let (changed_time, content) = match load_py_file(&path) {
Ok((changed_time, content)) => (changed_time, content),
Err(e) => {
warn!("failed to load file: {:?} | e: {:?}", path, e);
return None;
}
};
let py_module: PyResult<Py<PyAny>> = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
let module: PyResult<Py<PyAny>> = PyModule::from_code(
py,
&content,
&path.to_string_lossy(),
&path.to_string_lossy(),
// !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的
).map(|module| module.into());
module
});
match py_module {
Ok(py_module) => {
Some((changed_time, py_module))
}
Err(e) => {
warn!("failed to load file: {:?} | e: {:?}", path, e);
None
}
}
}
pub fn init_py(config: &IcaConfig) {
debug!("initing python threads");
pyo3::prepare_freethreaded_python();

View File

@ -1,13 +1,5 @@
# 更新日志
## 0.4.8
添加了 `filter_list` 用于过滤特定人的消息
## 0.4.7
修复了重载时如果代码有问题会直接 panic 的问题
## 0.4.6
现在更适合部署了