Add error handler and finally fix the query
All checks were successful
Build and push container image / build (push) Successful in 10m38s

This commit is contained in:
toast 2024-03-11 10:04:12 +11:00
parent 85e0ffce71
commit 3da094eaa6
5 changed files with 26 additions and 17 deletions

2
Cargo.lock generated
View File

@ -833,7 +833,7 @@ dependencies = [
[[package]] [[package]]
name = "kon" name = "kon"
version = "0.1.15" version = "0.1.16"
dependencies = [ dependencies = [
"cargo_toml", "cargo_toml",
"gamedig", "gamedig",

View File

@ -1,11 +1,9 @@
[package] [package]
name = "kon" name = "kon"
version = "0.1.15" version = "0.1.16"
rust-version = "1.74" rust-version = "1.74"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
cargo_toml = "0.19.2" cargo_toml = "0.19.2"
gamedig = "0.4.1" gamedig = "0.4.1"
@ -31,3 +29,4 @@ debug = true
[profile.release] [profile.release]
opt-level = 3 opt-level = 3
debug = false debug = false
strip = true

View File

@ -27,9 +27,9 @@ static PMS_BASE: Lazy<String> = Lazy::new(||
#[derive(Deserialize)] #[derive(Deserialize)]
struct MinecraftQueryData { struct MinecraftQueryData {
motd: MinecraftMotd, motd: Option<MinecraftMotd>,
players: MinecraftPlayers, players: Option<MinecraftPlayers>,
version: String, version: Option<String>,
online: bool online: bool
} }
@ -38,7 +38,7 @@ struct MinecraftMotd {
clean: Vec<String> clean: Vec<String>
} }
#[derive(Deserialize)] #[derive(Deserialize, Clone, Copy)]
struct MinecraftPlayers { struct MinecraftPlayers {
online: i32, online: i32,
max: i32 max: i32
@ -70,6 +70,8 @@ async fn gs_query_minecraft(server_ip: &str) -> Result<MinecraftQueryData, Error
if req.status().is_success() { if req.status().is_success() {
let data: MinecraftQueryData = req.json().await?; let data: MinecraftQueryData = req.json().await?;
Ok(data) Ok(data)
} else if req.status().is_server_error() {
Err(Error::from("Webserver returned a 5xx error."))
} else { } else {
Err(Error::from("Failed to query the server.")) Err(Error::from("Failed to query the server."))
} }
@ -137,9 +139,9 @@ pub async fn gs(
let mut embed_fields = Vec::new(); let mut embed_fields = Vec::new();
embed_fields.push(("Server IP".to_owned(), ip_address.to_owned(), true)); embed_fields.push(("Server IP".to_owned(), ip_address.to_owned(), true));
embed_fields.push((format!("\u{200b}"), format!("\u{200b}"), true)); embed_fields.push((format!("\u{200b}"), format!("\u{200b}"), true));
embed_fields.push(("MOTD".to_owned(), format!("{}", result.motd.clean[0]), true)); embed_fields.push(("MOTD".to_owned(), format!("{}", result.motd.unwrap().clean[0]), true));
embed_fields.push(("Players".to_owned(), format!("**{}**/**{}**", result.players.online, result.players.max), true)); embed_fields.push(("Players".to_owned(), format!("**{}**/**{}**", result.players.unwrap().online, result.players.clone().unwrap().max), true));
embed_fields.push(("Version".to_owned(), result.version, true)); embed_fields.push(("Version".to_owned(), result.version.unwrap(), true));
ctx.send(CreateReply::default() ctx.send(CreateReply::default()
.embed(embed .embed(embed
@ -149,7 +151,7 @@ pub async fn gs(
).await?; ).await?;
} else { } else {
ctx.send(CreateReply::default() ctx.send(CreateReply::default()
.content(format!("Server **{}** (`{}`) is currently offline.", server_name, ip_address)) .content(format!("**{}** (`{}`) is currently offline or unreachable.", server_name, ip_address))
).await?; ).await?;
} }
}, },

View File

@ -38,13 +38,13 @@ fn format_duration(secs: u64) -> String {
let mut formatted_string = String::new(); let mut formatted_string = String::new();
if days > 0 { if days > 0 {
formatted_string.push_str(&format!("{}d, ", days)); formatted_string.push_str(&format!("{}d, ", days));
} }
if hours > 0 || days > 0 { if hours > 0 || days > 0 {
formatted_string.push_str(&format!("{}h, ", hours)); formatted_string.push_str(&format!("{}h, ", hours));
} }
if minutes > 0 || hours > 0 { 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)); formatted_string.push_str(&format!("{}s", seconds));

View File

@ -47,8 +47,8 @@ async fn on_ready(
match commands { match commands {
Ok(cmdmap) => for command in cmdmap.iter() { 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) 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) 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, initialize_owners: true,
..Default::default() ..Default::default()
}) })