Better ping command

This commit is contained in:
TheKodeToad 2024-05-01 00:01:10 +01:00 committed by Sefa Eyeoglu
parent edbe99ac8f
commit e4ef3d0c66

View file

@ -1,11 +1,34 @@
use std::time::{Duration, Instant};
use crate::{Context, Error}; use crate::{Context, Error};
use log::trace; use log::trace;
use poise::CreateReply;
const PING_PREFIX: &str = "<:catstareback:1078622789885497414> Pong!";
/// Replies with pong! /// Replies with pong!
#[poise::command(slash_command, prefix_command, track_edits = true, ephemeral)] #[poise::command(slash_command, prefix_command, track_edits = true, ephemeral)]
pub async fn ping(ctx: Context<'_>) -> Result<(), Error> { pub async fn ping(ctx: Context<'_>) -> Result<(), Error> {
trace!("Running ping command!"); trace!("Running ping command!");
ctx.say("Pong!").await?;
let start = Instant::now();
let response = ctx.say(PING_PREFIX).await?;
let rtt = start.elapsed().as_millis();
let gateway_ping = match ctx.ping().await {
Duration::ZERO => "Undetermined".to_string(),
duration => format!("{}ms", duration.as_millis()),
};
response
.edit(
ctx,
CreateReply::default().content(format!(
"{PING_PREFIX}\n\nRTT: {rtt}ms\nGateway: {gateway_ping}",
)),
)
.await?;
Ok(()) Ok(())
} }