diff --git a/src/commands/minecraft.rs b/src/commands/minecraft.rs new file mode 100644 index 0000000..fbbedd9 --- /dev/null +++ b/src/commands/minecraft.rs @@ -0,0 +1,47 @@ +use crate::{Context, Error}; +use async_minecraft_ping::ServerDescription; + +const DEFAULT_SERVER: &str = "rustybever.be"; + +/// Ping a minecraft server +#[poise::command(prefix_command, slash_command)] +pub async fn ping_mc( + ctx: Context<'_>, + #[description = "Address of the server"] address: Option, + #[description = "Port the server runs on"] port: Option, +) -> Result<(), Error> { + let mut full_name = address.unwrap_or(DEFAULT_SERVER.to_string()); + let mut builder = async_minecraft_ping::ConnectionConfig::build(&full_name); + + if let Some(port) = port { + builder = builder.with_port(port); + full_name += &format!(":{}", port); + } + + let conn = builder.connect().await?; + let status = conn.status().await?.status; + + let description = match status.description { + ServerDescription::Plain(s) => s, + ServerDescription::Object { text } => text, + }; + + ctx.send(|f| { + f.embed(|e| { + e.description(format!("Server information for {}", full_name)) + .field("version", status.version.name, false) + .field("description", description, false) + .field( + "players", + format!( + "{} of {} player(s) online", + status.players.online, status.players.max + ), + false, + ) + }) + }) + .await?; + + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index bfb7034..24ca959 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,9 +1,10 @@ mod affluence; +mod minecraft; use crate::{Context, Data, Error}; pub fn commands() -> Vec> { - vec![help(), affluence::available()] + vec![help(), affluence::available(), minecraft::ping_mc()] } /// Show this help menu