|
| 1 | +use crate::bot::ShardManagerKey; |
| 2 | +use crate::commands::{BoxFuture, CommunityRegistrable, RegistrableCommand}; |
| 3 | +use crate::config::Config; |
| 4 | +use crate::errors::ModmailResult; |
| 5 | +use crate::handlers::InteractionHandler; |
| 6 | +use crate::i18n::get_translated_message; |
| 7 | +use serenity::FutureExt; |
| 8 | +use serenity::all::{CommandInteraction, Context, CreateCommand, ResolvedOption}; |
| 9 | +use std::sync::Arc; |
| 10 | + |
| 11 | +pub struct PingCommand; |
| 12 | + |
| 13 | +#[async_trait::async_trait] |
| 14 | +impl RegistrableCommand for PingCommand { |
| 15 | + fn as_community(&self) -> Option<&dyn CommunityRegistrable> { |
| 16 | + None |
| 17 | + } |
| 18 | + |
| 19 | + fn name(&self) -> &'static str { |
| 20 | + "ping" |
| 21 | + } |
| 22 | + |
| 23 | + fn doc<'a>(&self, config: &'a Config) -> BoxFuture<'a, String> { |
| 24 | + async move { get_translated_message(config, "help.ping", None, None, None, None).await } |
| 25 | + .boxed() |
| 26 | + } |
| 27 | + |
| 28 | + fn register(&self, config: &Config) -> BoxFuture<'_, Vec<CreateCommand>> { |
| 29 | + let config = config.clone(); |
| 30 | + |
| 31 | + Box::pin(async move { |
| 32 | + let cmd_desc = get_translated_message( |
| 33 | + &config, |
| 34 | + "slash_command.ping_command_desc", |
| 35 | + None, |
| 36 | + None, |
| 37 | + None, |
| 38 | + None, |
| 39 | + ) |
| 40 | + .await; |
| 41 | + |
| 42 | + vec![CreateCommand::new(self.name()).description(cmd_desc)] |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + fn run( |
| 47 | + &self, |
| 48 | + ctx: &Context, |
| 49 | + command: &CommandInteraction, |
| 50 | + _options: &[ResolvedOption<'_>], |
| 51 | + config: &Config, |
| 52 | + _handler: Arc<InteractionHandler>, |
| 53 | + ) -> BoxFuture<'_, ModmailResult<()>> { |
| 54 | + let ctx = ctx.clone(); |
| 55 | + let command = command.clone(); |
| 56 | + let config = config.clone(); |
| 57 | + |
| 58 | + Box::pin(async move { |
| 59 | + let shard_manager = ctx |
| 60 | + .data |
| 61 | + .read() |
| 62 | + .await |
| 63 | + .get::<ShardManagerKey>() |
| 64 | + .unwrap() |
| 65 | + .clone(); |
| 66 | + |
| 67 | + let latency = { |
| 68 | + let runners = shard_manager.runners.lock().await; |
| 69 | + runners.get(&ctx.shard_id).and_then(|runner| runner.latency) |
| 70 | + }; |
| 71 | + |
| 72 | + println!("Latency for shard {}: {:?}", ctx.shard_id, latency); |
| 73 | + |
| 74 | + Ok(()) |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
0 commit comments