|
1 | | -use std::collections::HashMap; |
2 | 1 | use crate::bot::ShardManagerKey; |
3 | 2 | use crate::commands::{BoxFuture, CommunityRegistrable, RegistrableCommand}; |
4 | 3 | use crate::config::Config; |
5 | 4 | use crate::errors::{DiscordError, ModmailError, ModmailResult}; |
6 | 5 | use crate::handlers::InteractionHandler; |
7 | 6 | use crate::i18n::get_translated_message; |
| 7 | +use crate::utils::{MessageBuilder, defer_response}; |
| 8 | +use chrono::Utc; |
8 | 9 | use serenity::FutureExt; |
9 | 10 | use serenity::all::{CommandInteraction, Context, CreateCommand, ResolvedOption}; |
| 11 | +use std::collections::HashMap; |
10 | 12 | use std::sync::Arc; |
11 | 13 | use std::time::Duration; |
12 | | -use crate::utils::{defer_response, MessageBuilder}; |
| 14 | +use tokio::time::Instant; |
13 | 15 |
|
14 | 16 | pub struct PingCommand; |
15 | 17 |
|
@@ -60,30 +62,58 @@ impl RegistrableCommand for PingCommand { |
60 | 62 |
|
61 | 63 | Box::pin(async move { |
62 | 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; |
63 | 70 |
|
64 | 71 | let shard_manager = ctx |
65 | 72 | .data |
66 | 73 | .read() |
67 | 74 | .await |
68 | 75 | .get::<ShardManagerKey>() |
69 | | - .unwrap() |
70 | | - .clone(); |
| 76 | + .cloned() |
| 77 | + .ok_or(ModmailError::Discord(DiscordError::ShardManagerNotFound))?; |
71 | 78 |
|
72 | | - let latency = { |
| 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 = { |
73 | 84 | let runners = shard_manager.runners.lock().await; |
74 | 85 | runners.get(&ctx.shard_id).and_then(|runner| runner.latency) |
75 | 86 | }; |
76 | 87 |
|
| 88 | + let start = Instant::now(); |
| 89 | + ctx.http.get_gateway().await?; |
| 90 | + let api_ping = start.elapsed(); |
| 91 | + |
77 | 92 | let mut params = HashMap::new(); |
78 | | - params.insert("latency".to_string(), format!("{:?}", latency.unwrap_or(Duration::default()).as_millis())); |
| 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 | + ); |
79 | 108 |
|
80 | 109 | let response = MessageBuilder::system_message(&ctx, &config) |
81 | | - .translated_content("slash_command.ping_command", Some(¶ms), None, None).await |
| 110 | + .translated_content("slash_command.ping_command", Some(¶ms), None, None) |
| 111 | + .await |
82 | 112 | .to_channel(command.channel_id) |
83 | | - .build_interaction_message_followup().await; |
| 113 | + .build_edit_message() |
| 114 | + .await; |
84 | 115 |
|
85 | | - command.create_followup(&ctx.http, response).await |
86 | | - .map_err(|e| ModmailError::Discord(DiscordError::ApiError(e.to_string())))?; |
| 116 | + res.edit(&ctx.http, response).await?; |
87 | 117 |
|
88 | 118 | Ok(()) |
89 | 119 | }) |
|
0 commit comments