-
Notifications
You must be signed in to change notification settings - Fork 2
feat(commands): add ping command #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e46f174
feat(shard): insert shard manager in client's data to be able to fetc…
Akinator31 642a7bd
feat(commands): add base for ping command
Akinator31 a88d03e
refactor(init): use rng instead of deprecated thread_rng
Akinator31 a21b34d
feat(commands): add complete ping command
Akinator31 7978a8c
refactor(ping): add more info in ping command
Akinator31 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| mod slash_command; | ||
| mod text_command; | ||
|
|
||
| pub use slash_command::*; | ||
| pub use text_command::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub mod ping; | ||
|
|
||
| pub use ping::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| use crate::bot::ShardManagerKey; | ||
| use crate::commands::{BoxFuture, CommunityRegistrable, RegistrableCommand}; | ||
| use crate::config::Config; | ||
| use crate::errors::{DiscordError, ModmailError, ModmailResult}; | ||
| use crate::handlers::InteractionHandler; | ||
| use crate::i18n::get_translated_message; | ||
| use crate::utils::{MessageBuilder, defer_response}; | ||
| use chrono::Utc; | ||
| use serenity::FutureExt; | ||
| use serenity::all::{CommandInteraction, Context, CreateCommand, ResolvedOption}; | ||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
| use tokio::time::Instant; | ||
|
|
||
| pub struct PingCommand; | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl RegistrableCommand for PingCommand { | ||
| fn as_community(&self) -> Option<&dyn CommunityRegistrable> { | ||
| None | ||
| } | ||
|
|
||
| fn name(&self) -> &'static str { | ||
| "ping" | ||
| } | ||
|
|
||
| fn doc<'a>(&self, config: &'a Config) -> BoxFuture<'a, String> { | ||
| async move { get_translated_message(config, "help.ping", None, None, None, None).await } | ||
| .boxed() | ||
| } | ||
|
|
||
| fn register(&self, config: &Config) -> BoxFuture<'_, Vec<CreateCommand>> { | ||
| let config = config.clone(); | ||
|
|
||
| Box::pin(async move { | ||
| let cmd_desc = get_translated_message( | ||
| &config, | ||
| "slash_command.ping_command_desc", | ||
| None, | ||
| None, | ||
| None, | ||
| None, | ||
| ) | ||
| .await; | ||
|
|
||
| vec![CreateCommand::new(self.name()).description(cmd_desc)] | ||
| }) | ||
| } | ||
|
|
||
| fn run( | ||
| &self, | ||
| ctx: &Context, | ||
| command: &CommandInteraction, | ||
| _options: &[ResolvedOption<'_>], | ||
| config: &Config, | ||
| _handler: Arc<InteractionHandler>, | ||
| ) -> BoxFuture<'_, ModmailResult<()>> { | ||
| let ctx = ctx.clone(); | ||
| let command = command.clone(); | ||
| let config = config.clone(); | ||
|
|
||
| Box::pin(async move { | ||
| defer_response(&ctx, &command).await?; | ||
| let response = MessageBuilder::system_message(&ctx, &config) | ||
| .content("...") | ||
| .to_channel(command.channel_id) | ||
| .build_interaction_message_followup() | ||
| .await; | ||
|
|
||
| let shard_manager = ctx | ||
| .data | ||
| .read() | ||
| .await | ||
| .get::<ShardManagerKey>() | ||
| .cloned() | ||
| .ok_or(ModmailError::Discord(DiscordError::ShardManagerNotFound))?; | ||
|
|
||
| let time_before = Instant::now(); | ||
| let mut res = command.create_followup(&ctx.http, response).await?; | ||
| let msg_send_ping = time_before.elapsed().as_millis(); | ||
|
|
||
| let gateway_ping = { | ||
| let runners = shard_manager.runners.lock().await; | ||
| runners.get(&ctx.shard_id).and_then(|runner| runner.latency) | ||
| }; | ||
|
|
||
| let start = Instant::now(); | ||
| ctx.http.get_gateway().await?; | ||
| let api_ping = start.elapsed(); | ||
|
|
||
| let mut params = HashMap::new(); | ||
| params.insert( | ||
| "gateway_latency".to_string(), | ||
| format!( | ||
| "{:?}", | ||
| gateway_ping.unwrap_or(Duration::default()).as_millis() | ||
| ), | ||
| ); | ||
| params.insert( | ||
| "api_latency".to_string(), | ||
| format!("{:?}", api_ping.as_millis()), | ||
| ); | ||
| params.insert( | ||
| "message_latency".to_string(), | ||
| format!("{:?}", msg_send_ping), | ||
| ); | ||
|
|
||
| let response = MessageBuilder::system_message(&ctx, &config) | ||
| .translated_content("slash_command.ping_command", Some(¶ms), None, None) | ||
| .await | ||
| .to_channel(command.channel_id) | ||
| .build_edit_message() | ||
| .await; | ||
|
|
||
| res.edit(&ctx.http, response).await?; | ||
|
|
||
| Ok(()) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub mod ping; | ||
|
|
||
| pub use ping::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| use crate::bot::ShardManagerKey; | ||
| use crate::prelude::config::*; | ||
| use crate::prelude::errors::*; | ||
| use crate::prelude::handlers::*; | ||
| use crate::utils::MessageBuilder; | ||
| use chrono::Utc; | ||
| use serenity::all::{Context, Message}; | ||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
| use tokio::time::Instant; | ||
|
|
||
| pub async fn ping( | ||
| ctx: Context, | ||
| msg: Message, | ||
| config: &Config, | ||
| _handler: Arc<GuildMessagesHandler>, | ||
| ) -> ModmailResult<()> { | ||
| let shard_manager = ctx | ||
| .data | ||
| .read() | ||
| .await | ||
| .get::<ShardManagerKey>() | ||
| .cloned() | ||
| .ok_or(ModmailError::Discord(DiscordError::ShardManagerNotFound))?; | ||
|
|
||
| let time_before = Instant::now(); | ||
| let mut res = msg.reply(&ctx.http, "...").await?; | ||
| let msg_send_ping = time_before.elapsed().as_millis(); | ||
|
|
||
| let gateway_ping = { | ||
| let runners = shard_manager.runners.lock().await; | ||
| runners.get(&ctx.shard_id).and_then(|runner| runner.latency) | ||
| }; | ||
|
|
||
| let start = Instant::now(); | ||
| ctx.http.get_gateway().await?; | ||
| let api_ping = start.elapsed(); | ||
|
|
||
| let mut params = HashMap::new(); | ||
| params.insert( | ||
| "gateway_latency".to_string(), | ||
| format!( | ||
| "{:?}", | ||
| gateway_ping.unwrap_or(Duration::default()).as_millis() | ||
| ), | ||
| ); | ||
| params.insert( | ||
| "api_latency".to_string(), | ||
| format!("{:?}", api_ping.as_millis()), | ||
| ); | ||
| params.insert( | ||
| "message_latency".to_string(), | ||
| format!("{:?}", msg_send_ping), | ||
| ); | ||
|
|
||
| let edited_msg = MessageBuilder::system_message(&ctx, &config) | ||
| .translated_content("slash_command.ping_command", Some(¶ms), None, None) | ||
| .await | ||
| .to_channel(msg.channel_id) | ||
| .build_edit_message() | ||
| .await; | ||
|
|
||
| res.edit(&ctx.http, edited_msg).await?; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.