affluence/src/commands/minecraft.rs

56 lines
1.7 KiB
Rust

use crate::{Context, Error};
use async_minecraft_ping::ServerDescription;
const DEFAULT_SERVER: &str = "rustybever.be";
/// Parent command for Minecraft-related actions.
///
/// Minecraft-related commands
#[poise::command(prefix_command, slash_command, subcommands("ping"))]
pub async fn mc(_ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}
/// Ping a minecraft server; defaults to our server.
#[poise::command(prefix_command, slash_command)]
pub async fn ping(
ctx: Context<'_>,
#[description = "Address of the server"] address: Option<String>,
#[description = "Port the server runs on"] port: Option<u16>,
) -> 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(())
}