Compare commits

..

15 Commits

Author SHA1 Message Date
1f7ffcb2d4
0.4.8 2024-02-22 23:17:20 +08:00
152c8215c3
更新bmcl.py插件,显示名称前添加了一个标志符号,赞助者字段名称更改 2024-02-22 21:25:00 +08:00
ed9ec33ed9
aaa 2024-02-22 21:23:07 +08:00
e09a257886
没分片数据啊 2024-02-22 21:22:01 +08:00
59feec8f4e
简洁信息 2024-02-22 21:20:40 +08:00
2540ba3ee2
消息你得发出去啊 2024-02-22 21:17:42 +08:00
e57cc7f3f0
添加4~10的判定 2024-02-22 21:16:31 +08:00
75f098849e
index +1 2024-02-22 21:09:57 +08:00
64dd2d4ad2
修复syntaxError 2024-02-22 21:06:17 +08:00
954a5a1b19
改进一下bmcl 2024-02-22 21:04:20 +08:00
f254879cf0
remove blake3 2024-02-22 19:19:24 +08:00
8efc7358a3
add message on startup 2024-02-22 19:16:07 +08:00
e2f619e97f
yeeee 2024-02-22 19:10:15 +08:00
f1abfd4f9d
开摆(0.4.7 2024-02-22 19:08:09 +08:00
ef61b3a6b4
2.1.0 for bmcl! 2024-02-22 18:26:09 +08:00
11 changed files with 200 additions and 63 deletions

2
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

@ -1,8 +1,8 @@
import re
import time import time
import requests import requests
from typing import TYPE_CHECKING, TypeVar from typing import TYPE_CHECKING, TypeVar, Optional
if TYPE_CHECKING: if TYPE_CHECKING:
from ica_typing import NewMessage, IcaClient from ica_typing import NewMessage, IcaClient
@ -10,7 +10,7 @@ else:
NewMessage = TypeVar("NewMessage") NewMessage = TypeVar("NewMessage")
IcaClient = TypeVar("IcaClient") IcaClient = TypeVar("IcaClient")
_version_ = "2.0.0-rs" _version_ = "2.1.2-rs"
def format_data_size(data_bytes: float) -> str: def format_data_size(data_bytes: float) -> str:
data_lens = ["B", "KB", "MB", "GB", "TB"] data_lens = ["B", "KB", "MB", "GB", "TB"]
@ -46,18 +46,24 @@ def format_hit_count(count: int) -> str:
else: else:
return "_".join(count_str[i:i + 4] for i in range(0, count_len, 4)) return "_".join(count_str[i:i + 4] for i in range(0, count_len, 4))
def bmcl(msg: NewMessage, client: IcaClient) -> None:
req_time = time.time() def wrap_request(url: str, client: IcaClient) -> Optional[dict]:
# 记录请求时间 response = requests.get(url)
response = requests.get("https://bd.bangbang93.com/openbmclapi/metric/dashboard")
if not response.status_code == 200 or response.reason != "OK": if not response.status_code == 200 or response.reason != "OK":
reply = msg.reply_with(f"请求数据失败\n{response.status_code}")
client.warn( client.warn(
f"数据请求失败, 请检查网络\n{response.status}" f"数据请求失败, 请检查网络\n{response.status}"
) )
client.send_message(reply) 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:
return return
data = response.json()
data_bytes: float = data["bytes"] data_bytes: float = data["bytes"]
data_hits: int = data["hits"] data_hits: int = data["hits"]
data_bandwidth: float = data["currentBandwidth"] data_bandwidth: float = data["currentBandwidth"]
@ -68,7 +74,7 @@ def bmcl(msg: NewMessage, client: IcaClient) -> None:
hits_count = format_hit_count(data_hits) hits_count = format_hit_count(data_hits)
report_msg = ( report_msg = (
f"OpenBMCLAPI 状态面板v{_version_} :\n" f"OpenBMCLAPI 面板v{_version_}-状态\n"
f"实时信息: {online_node} 带宽: {online_bandwidth}Mbps\n" f"实时信息: {online_node} 带宽: {online_bandwidth}Mbps\n"
f"负载: {load_str:.2f}% 带宽: {data_bandwidth:.2f}Mbps\n" f"负载: {load_str:.2f}% 带宽: {data_bandwidth:.2f}Mbps\n"
f"当日请求: {hits_count} 数据量: {data_len}\n" f"当日请求: {hits_count} 数据量: {data_len}\n"
@ -80,7 +86,111 @@ def bmcl(msg: NewMessage, client: IcaClient) -> None:
client.send_message(reply) 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: def on_message(msg: NewMessage, client: IcaClient) -> None:
if not (msg.is_from_self or msg.is_reply): if not (msg.is_from_self or msg.is_reply):
if msg.content == "/bmcl": if msg.content.startswith("/bmcl"):
bmcl(msg, client) 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)

View File

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

View File

@ -2,6 +2,7 @@ use crate::client::IcalinguaStatus;
use crate::data_struct::files::MessageFile; use crate::data_struct::files::MessageFile;
use crate::data_struct::{MessageId, RoomId, UserId}; use crate::data_struct::{MessageId, RoomId, UserId};
use tracing::warn;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, Value as JsonValue}; use serde_json::{json, Value as JsonValue};
@ -138,7 +139,15 @@ impl NewMessage {
} }
// 回复的消息 // 回复的消息
let reply: Option<ReplyMessage> = match message.get("replyMessage") { let reply: Option<ReplyMessage> = match message.get("replyMessage") {
Some(value) => serde_json::from_value::<ReplyMessage>(value.clone()).ok(), Some(value) => {
match serde_json::from_value::<ReplyMessage>(value.clone()) {
Ok(reply) => Some(reply),
Err(e) => {
warn!("Failed to parse reply message: {}", e);
None
}
}
},
None => None, None => None,
}; };
// At // At
@ -196,7 +205,8 @@ impl NewMessage {
pub fn output(&self) -> String { pub fn output(&self) -> String {
format!( format!(
"Room: {}, Sender: {}|{}, Content: {}", // >10 >10 >15
"{:>10}|{:>12}|{:<20}|{}",
self.room_id, self.sender_id, self.sender_name, self.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 rust_socketio::{Event, Payload};
use tracing::{info, warn}; use tracing::{info, warn};
use crate::client::send_message; use crate::client::{send_message, IcalinguaStatus};
use crate::data_struct::all_rooms::Room; use crate::data_struct::all_rooms::Room;
use crate::data_struct::messages::NewMessage; use crate::data_struct::messages::NewMessage;
use crate::data_struct::online_data::OnlineData; 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); let online_data = OnlineData::new_from_json(value);
info!( info!(
"update_online_data {}", "update_online_data {}",
format!("{:#?}", online_data).cyan() format!("{:?}", online_data).cyan()
); );
unsafe { unsafe {
crate::ClientStatus.update_online_data(online_data); crate::ClientStatus.update_online_data(online_data);
@ -30,7 +30,12 @@ pub async fn add_message(payload: Payload, client: Client) {
if let Payload::Text(values) = payload { if let Payload::Text(values) = payload {
if let Some(value) = values.first() { if let Some(value) = values.first() {
let message = NewMessage::new_from_json(value); 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 {}", message.output().cyan());
// info!("add_message {}", format!("{:#?}", message).cyan());
// 就在这里处理掉最基本的消息 // 就在这里处理掉最基本的消息
// 之后的处理交给插件 // 之后的处理交给插件
if message.content.eq("/bot-rs") && !message.is_from_self() && !message.is_reply() { if message.content.eq("/bot-rs") && !message.is_from_self() && !message.is_reply() {
@ -49,7 +54,7 @@ pub async fn delete_message(payload: Payload, _client: Client) {
// 消息 id // 消息 id
if let Some(value) = values.first() { if let Some(value) = values.first() {
if let Some(msg_id) = value.as_str() { if let Some(msg_id) = value.as_str() {
warn!("delete_message {}", format!("{}", msg_id).yellow()); info!("delete_message {}", format!("{}", msg_id).yellow());
} }
} }
} }
@ -88,6 +93,7 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) {
"notify", "notify",
"closeLoading", // 发送消息/加载新聊天 有一个 loading "closeLoading", // 发送消息/加载新聊天 有一个 loading
"updateRoom", "updateRoom",
"syncRead",
]; ];
match &event { match &event {
Event::Custom(event_name) => { Event::Custom(event_name) => {

View File

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

View File

@ -82,30 +82,12 @@ pub fn load_py_plugins(path: &PathBuf) {
let path = entry.path(); let path = entry.path();
if let Some(ext) = path.extension() { if let Some(ext) = path.extension() {
if ext == "py" { if ext == "py" {
match load_py_file(&path) { match load_py_module(&path) {
Ok((changed_time, content)) => { Some((changed_time, py_module)) => {
let py_module: PyResult<Py<PyAny>> = Python::with_gil(|py| -> PyResult<Py<PyAny>> { PyStatus::add_file(path.clone(), changed_time, py_module);
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);
}
Err(e) => {
warn!("failed to load file: {:?} | e: {:?}", path, e);
}
}
} }
Err(e) => { None => {
warn!("failed to load file: {:?} | e: {:?}", path, e); warn!("加载 Python 插件: {:?} 失败", path);
} }
} }
} }
@ -145,24 +127,12 @@ pub fn verify_plugins() {
} }
info!("file change list: {:?}", need_reload_files); info!("file change list: {:?}", need_reload_files);
for reload_file in need_reload_files { for reload_file in need_reload_files {
match load_py_file(&reload_file) { match load_py_module(&reload_file) {
Ok((changed_time, content)) => { Some((changed_time, py_module)) => {
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); PyStatus::add_file(reload_file.clone(), changed_time, py_module);
}, }
Err(e) => { None => {
warn!("重载 Python 插件: {:?} 失败, e: {:?}", reload_file, e); warn!("重载 Python 插件: {:?} 失败", reload_file);
} }
} }
} }
@ -180,6 +150,35 @@ pub fn load_py_file(path: &PathBuf) -> std::io::Result<(Option<SystemTime>, Stri
Ok((changed_time, content)) 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) { pub fn init_py(config: &IcaConfig) {
debug!("initing python threads"); debug!("initing python threads");
pyo3::prepare_freethreaded_python(); pyo3::prepare_freethreaded_python();

View File

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