added dm on all python errors. (Untested)

This commit is contained in:
2025-04-02 18:18:33 +02:00
parent a6d40b9527
commit eb0991df53
3 changed files with 21 additions and 8 deletions
+2 -2
View File
@@ -92,7 +92,7 @@ async fn main() {
if args.py && !args.rs { if args.py && !args.rs {
println!("----- PYTHON ONLY MODE -----"); println!("----- PYTHON ONLY MODE -----");
rs_println!("ARGS: {}", args_str); rs_println!("ARGS: {}", args_str);
let _ = python::start(args_str); let _ = python::start(args);
process::exit(0); process::exit(0);
} }
else if args.rs && ! args.py { else if args.rs && ! args.py {
@@ -108,7 +108,7 @@ async fn main() {
rs_println!("ARGS: {}", args_str); rs_println!("ARGS: {}", args_str);
let rt = Runtime::new().unwrap(); let rt = Runtime::new().unwrap();
let python_args = args_str; let python_args = args.clone();
let rust_args = args.clone(); let rust_args = args.clone();
let rust = thread::spawn(move || { let rust = thread::spawn(move || {
+18 -5
View File
@@ -1,4 +1,5 @@
use crate::{rs_println, errln}; use crate::messages::send_dm;
use crate::{errln, rs_println, Args};
use std::fs; use std::fs;
use std::ffi::CString; use std::ffi::CString;
@@ -8,16 +9,18 @@ use pyo3::prelude::*;
use pyo3::types::PyList; use pyo3::types::PyList;
pub fn start(args: String) -> PyResult<()> { pub async fn start(args: Args) -> PyResult<()> {
rs_println!("Running Python program..."); rs_println!("Running Python program...");
let args_str = serde_json::to_string(&args).expect("Error serializing args to JSON");
let slash = if cfg!(windows) { "\\" } else if cfg!(unix) { "/" } else { "" }; let slash = if cfg!(windows) { "\\" } else if cfg!(unix) { "/" } else { "" };
if slash.is_empty() { errln!("Man what kinda OS do you have? Neither unix or windows, what the hell!? I can't process this anymore, you're too weird!"); } if slash.is_empty() { errln!("Man what kinda OS do you have? Neither unix or windows, what the hell!? I can't process this anymore, you're too weird!"); }
let path = format!("{0}{1}src{1}python", env!("CARGO_MANIFEST_DIR"), slash); let path = format!("{0}{1}src{1}python", env!("CARGO_MANIFEST_DIR"), slash);
let code = get_code(&format!("{}{}main.py", path, slash)); let code = get_code(&format!("{}{}main.py", path, slash));
let py_args = args.replace(":true", ":True").replace(":false", ":False"); let py_args = args_str.replace(":true", ":True").replace(":false", ":False");
let app_path = CString::new(format!("args = {}\n{}", py_args, code)).unwrap(); let app_path = CString::new(format!("args = {}\n{}", py_args, code)).unwrap();
pyo3::prepare_freethreaded_python(); pyo3::prepare_freethreaded_python();
@@ -32,13 +35,23 @@ pub fn start(args: String) -> PyResult<()> {
return Ok(app); return Ok(app);
}); });
if from_python.is_err() { errln!("pyO3: {:?}", from_python); } if from_python.is_err() {
let own_env = std::env::var("ASSISTANT_OWNERS").unwrap_or("0".to_string());
let own_vec_str: Vec<String> = own_env.split(",").map(String::from).collect();
let own_vec_u64: Vec<u64> = own_vec_str
.iter()
.map(|s| s.parse::<u64>().expect("Failed to parse ASSISTANT_OWNERS. Invalid syntax."))
.collect();
send_dm(format!("Unknown internal Python Error: {:?}", from_python), args, own_vec_u64).await;
errln!("pyO3: {:?}", from_python);
}
return Ok(()); return Ok(());
} }
fn get_code(path: &str) -> String { fn get_code(path: &str) -> String {
return fs::read_to_string(path) return fs::read_to_string(path)
.unwrap_or_else(|_| panic!("Failed to read Python file.\nPath: {}", path)) .unwrap_or_else(|_| errln!("Failed to read Python file.\nPath: {}", path))
.to_string(); .to_string();
} }
+1 -1
View File
@@ -134,7 +134,7 @@ async fn handle_message(msg: tungstenite::protocol::Message, args: Args, owners:
if let Some(stripped) = text.strip_prefix("json:") { if let Some(stripped) = text.strip_prefix("json:") {
let t_json: Value = serde_json::from_str(stripped).unwrap(); let t_json: Value = serde_json::from_str(stripped).unwrap();
if t_json.get("error").is_some() { if t_json.get("error").is_some() {
send_dm("Unknown internal Python error occurred!".to_string(), args, owners).await; send_dm("Unknown internal Python error occurred: Websocket response error.".to_string(), args, owners).await;
} }
} }