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]]
name = "kon"
version = "0.1.15"
version = "0.1.16"
dependencies = [
"cargo_toml",
"gamedig",

View File

@ -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

View File

@ -27,9 +27,9 @@ static PMS_BASE: Lazy<String> = Lazy::new(||
#[derive(Deserialize)]
struct MinecraftQueryData {
motd: MinecraftMotd,
players: MinecraftPlayers,
version: String,
motd: Option<MinecraftMotd>,
players: Option<MinecraftPlayers>,
version: Option<String>,
online: bool
}
@ -38,7 +38,7 @@ struct MinecraftMotd {
clean: Vec<String>
}
#[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<MinecraftQueryData, Error
if req.status().is_success() {
let data: MinecraftQueryData = req.json().await?;
Ok(data)
} else if req.status().is_server_error() {
Err(Error::from("Webserver returned a 5xx error."))
} else {
Err(Error::from("Failed to query the server."))
}
@ -137,9 +139,9 @@ pub async fn gs(
let mut embed_fields = Vec::new();
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(("MOTD".to_owned(), format!("{}", result.motd.clean[0]), true));
embed_fields.push(("Players".to_owned(), format!("**{}**/**{}**", result.players.online, result.players.max), true));
embed_fields.push(("Version".to_owned(), result.version, true));
embed_fields.push(("MOTD".to_owned(), format!("{}", result.motd.unwrap().clean[0]), 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.unwrap(), true));
ctx.send(CreateReply::default()
.embed(embed
@ -149,7 +151,7 @@ pub async fn gs(
).await?;
} else {
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?;
}
},

View File

@ -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()
})