feat(commands/utils): ping command issued from SlashCommand

This commit is contained in:
Raphael 2026-02-12 16:20:07 +01:00
parent a4576acdc2
commit 2c85271a47
No known key found for this signature in database

View file

@ -1,32 +1,53 @@
use std::time::Instant; use std::time::Instant;
use crate::commands::{
CommandEntry,
SlashCommand,
};
use serenity::all::{ use serenity::all::{
CommandInteraction, Context, CreateCommand, CreateInteractionResponse, CommandInteraction, Context, CreateCommand, CreateInteractionResponse,
CreateInteractionResponseMessage, EditInteractionResponse, CreateInteractionResponseMessage, EditInteractionResponse,
}; };
pub const COMMAND_NAME: &str = "ping"; pub struct Ping;
pub const COMMAND_DESC: &str = "Show the discord API latency";
pub async fn run(ctx: &Context, command: &CommandInteraction) -> Result<(), serenity::Error> { #[serenity::async_trait]
let message: CreateInteractionResponseMessage = CreateInteractionResponseMessage::new() impl SlashCommand for Ping {
.content("🏓 | Pong!") fn name(&self) -> &'static str {
.ephemeral(true); "ping"
}
let response: CreateInteractionResponse = CreateInteractionResponse::Message(message); fn description(&self) -> &'static str {
"Show the Discord API latency"
}
let start: Instant = Instant::now(); fn register(&self) -> CreateCommand {
command.create_response(&ctx.http, response).await?; println!("\t✅ | {}", self.name());
let delta_time: u128 = start.elapsed().as_millis(); CreateCommand::new(self.name())
.description(self.description())
}
let edit_msg: String = format!("Ping: **{}**ms", delta_time); async fn run(&self, ctx: &Context, command: &CommandInteraction) -> Result<(), serenity::Error> {
let message: EditInteractionResponse = EditInteractionResponse::new().content(edit_msg); let message: CreateInteractionResponseMessage = CreateInteractionResponseMessage::new()
.content("🏓 | Pong!")
.ephemeral(true);
command.edit_response(&ctx.http, message).await?; let response: CreateInteractionResponse = CreateInteractionResponse::Message(message);
Ok(()) let start: Instant = Instant::now();
command.create_response(&ctx.http, response).await?;
let delta_time: u128 = start.elapsed().as_millis();
let edit_msg: EditInteractionResponse = EditInteractionResponse::new()
.content(format!("Ping: **{delta_time}**ms"));
command.edit_response(&ctx.http, edit_msg).await?;
Ok(())
}
} }
pub fn register() -> CreateCommand { inventory::submit! {
CreateCommand::new(COMMAND_NAME).description(COMMAND_DESC) CommandEntry { create: || Box::new(Ping) }
} }