feat: add minecraft ping command
parent
38994a29a0
commit
c966529a2b
|
@ -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<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(())
|
||||||
|
}
|
|
@ -1,9 +1,10 @@
|
||||||
mod affluence;
|
mod affluence;
|
||||||
|
mod minecraft;
|
||||||
|
|
||||||
use crate::{Context, Data, Error};
|
use crate::{Context, Data, Error};
|
||||||
|
|
||||||
pub fn commands() -> Vec<poise::structs::Command<Data, Error>> {
|
pub fn commands() -> Vec<poise::structs::Command<Data, Error>> {
|
||||||
vec![help(), affluence::available()]
|
vec![help(), affluence::available(), minecraft::ping_mc()]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Show this help menu
|
/// Show this help menu
|
||||||
|
|
Loading…
Reference in New Issue