|
| 1 | +use crate::bot::ShardManagerKey; |
| 2 | +use crate::commands::{BoxFuture, CommunityRegistrable, RegistrableCommand}; |
| 3 | +use crate::config::Config; |
| 4 | +use crate::errors::{DiscordError, ModmailError, ModmailResult}; |
| 5 | +use crate::handlers::InteractionHandler; |
| 6 | +use crate::i18n::get_translated_message; |
| 7 | +use crate::utils::{MessageBuilder, defer_response}; |
| 8 | +use chrono::Utc; |
| 9 | +use serenity::FutureExt; |
| 10 | +use serenity::all::{CommandInteraction, Context, CreateCommand, ResolvedOption}; |
| 11 | +use std::collections::HashMap; |
| 12 | +use std::sync::Arc; |
| 13 | +use std::time::Duration; |
| 14 | +use tokio::time::Instant; |
| 15 | + |
| 16 | +pub struct PingCommand; |
| 17 | + |
| 18 | +#[async_trait::async_trait] |
| 19 | +impl RegistrableCommand for PingCommand { |
| 20 | + fn as_community(&self) -> Option<&dyn CommunityRegistrable> { |
| 21 | + None |
| 22 | + } |
| 23 | + |
| 24 | + fn name(&self) -> &'static str { |
| 25 | + "ping" |
| 26 | + } |
| 27 | + |
| 28 | + fn doc<'a>(&self, config: &'a Config) -> BoxFuture<'a, String> { |
| 29 | + async move { get_translated_message(config, "help.ping", None, None, None, None).await } |
| 30 | + .boxed() |
| 31 | + } |
| 32 | + |
| 33 | + fn register(&self, config: &Config) -> BoxFuture<'_, Vec<CreateCommand>> { |
| 34 | + let config = config.clone(); |
| 35 | + |
| 36 | + Box::pin(async move { |
| 37 | + let cmd_desc = get_translated_message( |
| 38 | + &config, |
| 39 | + "slash_command.ping_command_desc", |
| 40 | + None, |
| 41 | + None, |
| 42 | + None, |
| 43 | + None, |
| 44 | + ) |
| 45 | + .await; |
| 46 | + |
| 47 | + vec![CreateCommand::new(self.name()).description(cmd_desc)] |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + fn run( |
| 52 | + &self, |
| 53 | + ctx: &Context, |
| 54 | + command: &CommandInteraction, |
| 55 | + _options: &[ResolvedOption<'_>], |
| 56 | + config: &Config, |
| 57 | + _handler: Arc<InteractionHandler>, |
| 58 | + ) -> BoxFuture<'_, ModmailResult<()>> { |
| 59 | + let ctx = ctx.clone(); |
| 60 | + let command = command.clone(); |
| 61 | + let config = config.clone(); |
| 62 | + |
| 63 | + Box::pin(async move { |
| 64 | + defer_response(&ctx, &command).await?; |
| 65 | + let response = MessageBuilder::system_message(&ctx, &config) |
| 66 | + .content("...") |
| 67 | + .to_channel(command.channel_id) |
| 68 | + .build_interaction_message_followup() |
| 69 | + .await; |
| 70 | + |
| 71 | + let shard_manager = ctx |
| 72 | + .data |
| 73 | + .read() |
| 74 | + .await |
| 75 | + .get::<ShardManagerKey>() |
| 76 | + .cloned() |
| 77 | + .ok_or(ModmailError::Discord(DiscordError::ShardManagerNotFound))?; |
| 78 | + |
| 79 | + let time_before = Instant::now(); |
| 80 | + let mut res = command.create_followup(&ctx.http, response).await?; |
| 81 | + let msg_send_ping = time_before.elapsed().as_millis(); |
| 82 | + |
| 83 | + let gateway_ping = { |
| 84 | + let runners = shard_manager.runners.lock().await; |
| 85 | + runners.get(&ctx.shard_id).and_then(|runner| runner.latency) |
| 86 | + }; |
| 87 | + |
| 88 | + let start = Instant::now(); |
| 89 | + ctx.http.get_gateway().await?; |
| 90 | + let api_ping = start.elapsed(); |
| 91 | + |
| 92 | + let mut params = HashMap::new(); |
| 93 | + params.insert( |
| 94 | + "gateway_latency".to_string(), |
| 95 | + format!( |
| 96 | + "{:?}", |
| 97 | + gateway_ping.unwrap_or(Duration::default()).as_millis() |
| 98 | + ), |
| 99 | + ); |
| 100 | + params.insert( |
| 101 | + "api_latency".to_string(), |
| 102 | + format!("{:?}", api_ping.as_millis()), |
| 103 | + ); |
| 104 | + params.insert( |
| 105 | + "message_latency".to_string(), |
| 106 | + format!("{:?}", msg_send_ping), |
| 107 | + ); |
| 108 | + |
| 109 | + let response = MessageBuilder::system_message(&ctx, &config) |
| 110 | + .translated_content("slash_command.ping_command", Some(¶ms), None, None) |
| 111 | + .await |
| 112 | + .to_channel(command.channel_id) |
| 113 | + .build_edit_message() |
| 114 | + .await; |
| 115 | + |
| 116 | + res.edit(&ctx.http, response).await?; |
| 117 | + |
| 118 | + Ok(()) |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments