mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-02-23 01:39:58 +08:00
fmt + 改进 msg display
This commit is contained in:
parent
76a3628d2d
commit
ede6640aa9
|
@ -88,12 +88,25 @@ impl<'de> Deserialize<'de> for Message {
|
|||
|
||||
impl Display for Message {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if !self.content.is_empty() {
|
||||
if !self.content.is_empty() && !self.content.trim().is_empty() {
|
||||
write!(f, "{}|{}|{}|{}", self.msg_id(), self.sender_id, self.sender_name, self.content)
|
||||
} else if !self.files.is_empty() {
|
||||
write!(f, "{}|{}|{}|{:?}", self.msg_id(), self.sender_id, self.sender_name, self.files[0].name)
|
||||
write!(
|
||||
f,
|
||||
"{}|{}|{}|{:?}",
|
||||
self.msg_id(),
|
||||
self.sender_id,
|
||||
self.sender_name,
|
||||
self.files[0].name
|
||||
)
|
||||
} else {
|
||||
write!(f, "{}|{}|{}|empty content & empty files", self.msg_id(), self.sender_id, self.sender_name)
|
||||
write!(
|
||||
f,
|
||||
"{}|{}|{}|empty content & empty files",
|
||||
self.msg_id(),
|
||||
self.sender_id,
|
||||
self.sender_name
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -120,14 +133,32 @@ impl MessageTrait for NewMessage {
|
|||
|
||||
impl Display for NewMessage {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if !self.msg.content.trim().is_empty() {
|
||||
write!(
|
||||
f,
|
||||
"{}|{}|{}|{}|{}",
|
||||
self.msg_id(),
|
||||
self.msg.msg_id,
|
||||
self.room_id,
|
||||
self.msg.sender_id,
|
||||
self.msg.sender_name,
|
||||
self.msg.content
|
||||
)
|
||||
} else if !self.msg.files.is_empty() {
|
||||
write!(
|
||||
f,
|
||||
"{}|{}|{}|{}|{:?}",
|
||||
self.msg.msg_id,
|
||||
self.room_id,
|
||||
self.msg.sender_id,
|
||||
self.msg.sender_name,
|
||||
self.msg.files[0].name
|
||||
)
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
"{}|{}|{}|{}|empty content & empty files",
|
||||
self.msg.msg_id, self.room_id, self.msg.sender_id, self.msg.sender_name
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,9 +54,7 @@ pub fn help_msg() -> String {
|
|||
|
||||
static STARTUP_TIME: OnceLock<SystemTime> = OnceLock::new();
|
||||
|
||||
pub fn start_up_time() -> SystemTime {
|
||||
*STARTUP_TIME.get().expect("WTF, why did you panic?")
|
||||
}
|
||||
pub fn start_up_time() -> SystemTime { *STARTUP_TIME.get().expect("WTF, why did you panic?") }
|
||||
|
||||
/// 获得当前客户端的 id
|
||||
/// 防止串号
|
||||
|
|
|
@ -55,9 +55,7 @@ impl PyTasks {
|
|||
self.ica_new_message.len() + self.ica_delete_message.len() + self.tailchat_new_message.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
|
||||
pub fn cancel_all(&mut self) {
|
||||
for handle in self.ica_new_message.drain(..) {
|
||||
|
@ -72,11 +70,13 @@ impl PyTasks {
|
|||
}
|
||||
}
|
||||
|
||||
pub static PY_TASKS: LazyLock<Mutex<PyTasks>> = LazyLock::new(|| Mutex::new(PyTasks {
|
||||
pub static PY_TASKS: LazyLock<Mutex<PyTasks>> = LazyLock::new(|| {
|
||||
Mutex::new(PyTasks {
|
||||
ica_new_message: Vec::new(),
|
||||
ica_delete_message: Vec::new(),
|
||||
tailchat_new_message: Vec::new(),
|
||||
}));
|
||||
})
|
||||
});
|
||||
|
||||
pub fn get_func<'py>(
|
||||
py_module: &Bound<'py, PyAny>,
|
||||
|
|
|
@ -39,18 +39,12 @@ impl PyStatus {
|
|||
let _ = unsafe { PyPluginStatus.get_or_init(|| status) };
|
||||
}
|
||||
|
||||
pub fn get() -> &'static PyStatus {
|
||||
unsafe { PyPluginStatus.get().unwrap() }
|
||||
}
|
||||
pub fn get() -> &'static PyStatus { unsafe { PyPluginStatus.get().unwrap() } }
|
||||
|
||||
pub fn get_mut() -> &'static mut PyStatus {
|
||||
unsafe { PyPluginStatus.get_mut().unwrap() }
|
||||
}
|
||||
pub fn get_mut() -> &'static mut PyStatus { unsafe { PyPluginStatus.get_mut().unwrap() } }
|
||||
|
||||
/// 添加一个插件
|
||||
pub fn add_file(&mut self, path: PathBuf, plugin: PyPlugin) {
|
||||
self.files.insert(path, plugin);
|
||||
}
|
||||
pub fn add_file(&mut self, path: PathBuf, plugin: PyPlugin) { self.files.insert(path, plugin); }
|
||||
|
||||
/// 重新加载一个插件
|
||||
pub fn reload_plugin(&mut self, plugin_name: &str) -> bool {
|
||||
|
@ -70,9 +64,7 @@ impl PyStatus {
|
|||
}
|
||||
|
||||
/// 删除一个插件
|
||||
pub fn delete_file(&mut self, path: &PathBuf) -> Option<PyPlugin> {
|
||||
self.files.remove(path)
|
||||
}
|
||||
pub fn delete_file(&mut self, path: &PathBuf) -> Option<PyPlugin> { self.files.remove(path) }
|
||||
|
||||
pub fn get_status(&self, pluging_id: &str) -> Option<bool> {
|
||||
self.files.iter().find_map(|(_, plugin)| {
|
||||
|
@ -195,9 +187,7 @@ impl PyPlugin {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_id(&self) -> String {
|
||||
plugin_path_as_id(&self.file_path)
|
||||
}
|
||||
pub fn get_id(&self) -> String { plugin_path_as_id(&self.file_path) }
|
||||
}
|
||||
|
||||
impl Display for PyPlugin {
|
||||
|
@ -398,9 +388,7 @@ pub fn load_py_plugins(path: &PathBuf) {
|
|||
);
|
||||
}
|
||||
|
||||
pub fn get_change_time(path: &Path) -> Option<SystemTime> {
|
||||
path.metadata().ok()?.modified().ok()
|
||||
}
|
||||
pub fn get_change_time(path: &Path) -> Option<SystemTime> { path.metadata().ok()?.modified().ok() }
|
||||
|
||||
pub fn py_module_from_code(content: &str, path: &Path) -> PyResult<Py<PyAny>> {
|
||||
Python::with_gil(|py| -> PyResult<Py<PyAny>> {
|
||||
|
@ -525,7 +513,7 @@ pub async fn post_py() -> anyhow::Result<()> {
|
|||
|
||||
async fn stop_tasks() {
|
||||
if call::PY_TASKS.lock().await.is_empty() {
|
||||
return ;
|
||||
return;
|
||||
}
|
||||
let waiter = tokio::spawn(async {
|
||||
call::PY_TASKS.lock().await.join_all().await;
|
||||
|
|
Loading…
Reference in New Issue
Block a user