From 3da094eaa61716c90b26c164c0d420202daa0100 Mon Sep 17 00:00:00 2001 From: toast Date: Mon, 11 Mar 2024 10:04:12 +1100 Subject: [PATCH] Add error handler and finally fix the query --- Cargo.lock | 2 +- Cargo.toml | 5 ++--- src/commands/status.rs | 18 ++++++++++-------- src/commands/uptime.rs | 6 +++--- src/main.rs | 12 ++++++++++-- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c46322f..fbeb000 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -833,7 +833,7 @@ dependencies = [ [[package]] name = "kon" -version = "0.1.15" +version = "0.1.16" dependencies = [ "cargo_toml", "gamedig", diff --git a/Cargo.toml b/Cargo.toml index 2622747..360266a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,9 @@ [package] name = "kon" -version = "0.1.15" +version = "0.1.16" rust-version = "1.74" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] cargo_toml = "0.19.2" gamedig = "0.4.1" @@ -31,3 +29,4 @@ debug = true [profile.release] opt-level = 3 debug = false +strip = true diff --git a/src/commands/status.rs b/src/commands/status.rs index 794891b..e3c9176 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -27,9 +27,9 @@ static PMS_BASE: Lazy = Lazy::new(|| #[derive(Deserialize)] struct MinecraftQueryData { - motd: MinecraftMotd, - players: MinecraftPlayers, - version: String, + motd: Option, + players: Option, + version: Option, online: bool } @@ -38,7 +38,7 @@ struct MinecraftMotd { clean: Vec } -#[derive(Deserialize)] +#[derive(Deserialize, Clone, Copy)] struct MinecraftPlayers { online: i32, max: i32 @@ -70,6 +70,8 @@ async fn gs_query_minecraft(server_ip: &str) -> Result String { let mut formatted_string = String::new(); if days > 0 { - formatted_string.push_str(&format!("{}d, ", days)); + formatted_string.push_str(&format!("{}d, ", days)); } if hours > 0 || days > 0 { - formatted_string.push_str(&format!("{}h, ", hours)); + formatted_string.push_str(&format!("{}h, ", hours)); } if minutes > 0 || hours > 0 { - formatted_string.push_str(&format!("{}m, ", minutes)); + formatted_string.push_str(&format!("{}m, ", minutes)); } formatted_string.push_str(&format!("{}s", seconds)); diff --git a/src/main.rs b/src/main.rs index 77dd54f..f7b64f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,8 +47,8 @@ async fn on_ready( match commands { Ok(cmdmap) => for command in cmdmap.iter() { - println!("Registered command globally: {}", command.name); - }, + println!("Registered command globally: {}", command.name); + }, Err(why) => println!("Error registering commands: {:?}", why) } } @@ -76,6 +76,14 @@ async fn main() { }; println!("[{}] {} ran /{}", get_guild_name, ctx.author().name, ctx.command().qualified_name) }), + on_error: |error| Box::pin(async move { + match error { + poise::FrameworkError::Command { error, ctx, .. } => { + println!("PoiseCommandError({}): {}", ctx.command().qualified_name, error); + } + other => println!("PoiseOtherError: {:?}", other) + } + }), initialize_owners: true, ..Default::default() })