Skip to content

Commit e46f174

Browse files
committed
feat(shard): insert shard manager in client's data to be able to fetch discord's api latency
1 parent b39371f commit e46f174

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

rustmail/src/bot.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ use crate::prelude::panel_commands::*;
77
use crate::prelude::types::*;
88
use base64::Engine;
99
use rand::RngCore;
10-
use serenity::all::{ClientBuilder, GatewayIntents};
10+
use serenity::all::{ClientBuilder, GatewayIntents, ShardManager};
1111
use serenity::cache::Settings as CacheSettings;
12+
use serenity::prelude::TypeMapKey;
1213
use std::collections::HashMap;
1314
use std::process;
1415
use std::sync::Arc;
1516
use std::time::Duration;
1617
use tokio::sync::Mutex;
1718
use tokio::{select, spawn};
1819

20+
pub struct ShardManagerKey;
21+
22+
impl TypeMapKey for ShardManagerKey {
23+
type Value = Arc<ShardManager>;
24+
}
25+
1926
pub async fn init_bot_state() -> Arc<Mutex<BotState>> {
2027
let pool = init_database().await.expect("An error occured!");
2128
println!("Database connected!");
@@ -136,6 +143,7 @@ pub async fn run_bot(
136143
registry.register_command(LogsCommand);
137144
registry.register_command(TakeCommand);
138145
registry.register_command(ReleaseCommand);
146+
registry.register_command(PingCommand);
139147

140148
let registry = Arc::new(registry);
141149

@@ -174,6 +182,12 @@ pub async fn run_bot(
174182
state_lock.bot_http = Some(client.http.clone());
175183
}
176184

185+
{
186+
let mut data = client.data.write().await;
187+
188+
data.insert::<ShardManagerKey>(client.shard_manager.clone());
189+
}
190+
177191
if let Err(e) = config.validate_servers(&client.http).await {
178192
eprintln!("Configuration validation error: {}", e);
179193
eprintln!(

rustmail/src/config.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ pub struct Config {
2121

2222
pub db_pool: Option<SqlitePool>,
2323
pub error_handler: Option<Arc<ErrorHandler>>,
24-
pub thread_locks: Arc<std::sync::Mutex<std::collections::HashMap<u64, Arc<tokio::sync::Mutex<()>>>>>,
24+
pub thread_locks:
25+
Arc<std::sync::Mutex<std::collections::HashMap<u64, Arc<tokio::sync::Mutex<()>>>>>,
2526
}
2627

2728
fn get_local_ip() -> Option<String> {
@@ -50,21 +51,30 @@ pub fn load_config(path: &str) -> Option<Config> {
5051
}
5152

5253
if u64::from_str_radix(&config_response.thread.user_message_color, 16).is_err() {
53-
eprintln!("Incorrect user message color in the config.toml! Please put a color in hex format!");
54+
eprintln!(
55+
"Incorrect user message color in the config.toml! Please put a color in hex format!"
56+
);
5457
return None;
5558
}
5659

5760
if u64::from_str_radix(&config_response.thread.staff_message_color, 16).is_err() {
58-
eprintln!("Incorrect staff message color in the config.toml! Please put a color in hex format!");
61+
eprintln!(
62+
"Incorrect staff message color in the config.toml! Please put a color in hex format!"
63+
);
5964
return None;
6065
}
6166

6267
if u64::from_str_radix(&config_response.reminders.embed_color, 16).is_err() {
63-
eprintln!("Incorrect reminder embed color in the config.toml! Please put a color in hex format!");
68+
eprintln!(
69+
"Incorrect reminder embed color in the config.toml! Please put a color in hex format!"
70+
);
6471
return None;
6572
}
6673

67-
if !config_response.language.is_language_supported(config_response.language.get_default_language()) {
74+
if !config_response
75+
.language
76+
.is_language_supported(config_response.language.get_default_language())
77+
{
6878
eprintln!(
6979
"Warning: Default language '{}' is not in supported languages list",
7080
config_response.language.default_language

0 commit comments

Comments
 (0)