Python error handling (sends Discord DM)

This commit is contained in:
2025-02-18 14:22:26 +01:00
parent 7fc23896cb
commit cb2b5a508e
4 changed files with 39 additions and 3 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ Adds the post to the list of posts.
- **Only moderators of a subreddit or the OP (Original Poster) can use this command.** - **Only moderators of a subreddit or the OP (Original Poster) can use this command.**
## `bk_week_vote` ## `bk_week_vote`
Adds a vote to the post. (see `/bk_week_vote` in the Discord help). Adds a vote to the post. (see `/bk_week_vote` in the Discord help).
- **Anyone can use this command.** - **Anyone can use this command. If a moderator uses it the vote will be flagged as a moderator vote.**
### **Examples** ### **Examples**
``` ```
"u/ByteDiceAssistant bk_week_add" "u/ByteDiceAssistant bk_week_add"
+24 -2
View File
@@ -1,8 +1,10 @@
use crate::Context; use std::env;
use crate::{Args, Context};
use poise::serenity_prelude::json::Value; use poise::serenity_prelude::json::Value;
use poise::{serenity_prelude::CreateMessage, CreateReply, ReplyHandle}; use poise::{serenity_prelude::CreateMessage, CreateReply, ReplyHandle};
use poise::serenity_prelude::{Color, CreateEmbed, CreateEmbedAuthor, Timestamp}; use poise::serenity_prelude::{Color, CreateEmbed, CreateEmbedAuthor, Http, Timestamp, UserId};
use serde_json::json; use serde_json::json;
@@ -135,6 +137,26 @@ pub async fn edit_msg(
} }
pub async fn send_dm(msg: String, args: Args) {
let uid = env::var("ASSISTANT_DM_USER").expect("Missing ASSISTANT_DM_USER env var!").parse::<u64>().unwrap();
let user = UserId::new(uid);
let token: String;
if !args.test {
token = std::env::var("ASSISTANT_TOKEN").expect("Missing ASSISTANT_TOKEN env var!");
}
else {
token = std::env::var("ASSISTANT_TOKEN_TEST").expect("Missing ASSISTANT_TOKEN_TEST env var!");
};
let http = Http::new(&token);
let c_msg = CreateMessage::new().content(msg);
let _ = user.dm(http, c_msg).await;
}
pub fn embed_post(post_data: &Value, url: &str, empheral: bool) -> EmbedOptions { pub fn embed_post(post_data: &Value, url: &str, empheral: bool) -> EmbedOptions {
let media_type = &post_data["post_data"]["media_type"]; let media_type = &post_data["post_data"]["media_type"];
let re_votes = &post_data["votes"]["voters_re"].as_array().unwrap().len(); let re_votes = &post_data["votes"]["voters_re"].as_array().unwrap().len();
+6
View File
@@ -16,6 +16,12 @@ async def send_message(message: str):
await ws_global.send(message) await ws_global.send(message)
async def send_internal_error(e: Exception):
global ws_global
if ws_global:
await ws_global.send(f"json:{{\"error\":\"Internal Python error: {e}\"}}")
async def websocket_client(bot: botPy.Bot): async def websocket_client(bot: botPy.Bot):
global ws_global, is_connected global ws_global, is_connected
port = bot.args["port"] port = bot.args["port"]
+8
View File
@@ -8,6 +8,7 @@ use futures::StreamExt;
use std::sync::Arc; use std::sync::Arc;
use serde_json::Value; use serde_json::Value;
use crate::messages::send_dm;
use crate::rs_println; use crate::rs_println;
use crate::Args; use crate::Args;
@@ -125,6 +126,13 @@ async fn handle_message(msg: tungstenite::protocol::Message, args: Args) {
tungstenite::Message::Text(text) => { tungstenite::Message::Text(text) => {
rs_println!("Received from Python: {}", text); rs_println!("Received from Python: {}", text);
if text.starts_with("json:") {
let t_json: Value = serde_json::from_str(&text[5..]).unwrap();
if t_json.get("error").is_some() {
send_dm("Internal Python error!".to_string(), args).await;
}
}
unsafe { unsafe {
if !REPLY_HELLO { if !REPLY_HELLO {
send_msg("[Connection test] Hello from Rust!").await; send_msg("[Connection test] Hello from Rust!").await;