Skip to content

Commit 642a7bd

Browse files
committed
feat(commands): add base for ping command
1 parent e46f174 commit 642a7bd

9 files changed

Lines changed: 137 additions & 0 deletions

File tree

rustmail/src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub mod id;
2222
pub mod logs;
2323
pub mod move_thread;
2424
pub mod new_thread;
25+
pub mod ping;
2526
pub mod recover;
2627
pub mod release;
2728
pub mod remove_reminder;
@@ -42,6 +43,7 @@ pub use id::*;
4243
pub use logs::*;
4344
pub use move_thread::*;
4445
pub use new_thread::*;
46+
pub use ping::*;
4547
pub use recover::*;
4648
pub use release::*;
4749
pub use remove_reminder::*;

rustmail/src/commands/ping/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod slash_command;
2+
mod text_command;
3+
4+
pub use slash_command::*;
5+
pub use text_command::*;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod ping;
2+
3+
pub use ping::*;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod ping;
2+
3+
pub use ping::*;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::bot::ShardManagerKey;
2+
use crate::prelude::config::*;
3+
use crate::prelude::errors::*;
4+
use crate::prelude::handlers::*;
5+
use serenity::all::{Context, Message};
6+
use std::sync::Arc;
7+
8+
pub async fn ping(
9+
ctx: Context,
10+
msg: Message,
11+
config: &Config,
12+
_handler: Arc<GuildMessagesHandler>,
13+
) -> ModmailResult<()> {
14+
let shard_manager = ctx
15+
.data
16+
.read()
17+
.await
18+
.get::<ShardManagerKey>()
19+
.unwrap()
20+
.clone();
21+
22+
let latency = {
23+
let runners = shard_manager.runners.lock().await;
24+
runners.get(&ctx.shard_id).and_then(|runner| runner.latency)
25+
};
26+
27+
println!("Latency for shard {}: {:?}", ctx.shard_id, latency);
28+
29+
Ok(())
30+
}

rustmail/src/handlers/guild_messages_handler.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl GuildMessagesHandler {
7777
wrap_command!(lock, "logs", logs);
7878
wrap_command!(lock, "take", take);
7979
wrap_command!(lock, "release", release);
80+
wrap_command!(lock, "ping", ping);
8081

8182
drop(lock);
8283
h

rustmail/src/i18n/language/en.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,10 @@ pub fn load_english_messages(dict: &mut ErrorDictionary) {
883883
"help.release".to_string(),
884884
DictionaryMessage::new("Releases ownership of a ticket previously taken with the `!take` command. To release a ticket, use `!release` in the ticket."),
885885
);
886+
dict.messages.insert(
887+
"help.ping".to_string(),
888+
DictionaryMessage::new("Show the actual latency of the Discord API."),
889+
);
886890
dict.messages.insert(
887891
"add_reminder.helper".to_string(),
888892
DictionaryMessage::new(
@@ -928,4 +932,8 @@ pub fn load_english_messages(dict: &mut ErrorDictionary) {
928932
"slash_command.help_command_argument_desc".to_string(),
929933
DictionaryMessage::new("The command to get help with"),
930934
);
935+
dict.messages.insert(
936+
"slash_command.ping_command_desc".to_string(),
937+
DictionaryMessage::new("Check the discord API latency"),
938+
);
931939
}

rustmail/src/i18n/language/fr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,10 @@ pub fn load_french_messages(dict: &mut ErrorDictionary) {
904904
"help.release".to_string(),
905905
DictionaryMessage::new("Permet de ne plus prendre en charge un ticket pris en charge via la commande `take`. Pour libérer un ticket, faites `!release` dans le ticket."),
906906
);
907+
dict.messages.insert(
908+
"help.ping".to_string(),
909+
DictionaryMessage::new("Permet d'afficher la latence actuelle de l'API Discord."),
910+
);
907911
dict.messages.insert(
908912
"add_reminder.helper".to_string(),
909913
DictionaryMessage::new("Format incorrect. Utilisation : `{prefix}remind ou {prefix}rem <HH:MM> [contenu du rappel]`"),
@@ -940,4 +944,8 @@ pub fn load_french_messages(dict: &mut ErrorDictionary) {
940944
"slash_command.help_command_argument_desc".to_string(),
941945
DictionaryMessage::new("Le nom de la commande pour laquelle vous souhaitez de l'aide"),
942946
);
947+
dict.messages.insert(
948+
"slash_command.ping_command_desc".to_string(),
949+
DictionaryMessage::new("Afficher la latence actuelle de l'API Discord"),
950+
);
943951
}

0 commit comments

Comments
 (0)