From 3ad345b009da9711a87d14b6a349c36ee2366869 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Thu, 25 Aug 2022 03:36:23 +0200 Subject: [PATCH] perf: clone `Arc` of `data.database` --- src/commands/mod.rs | 2 +- src/commands/moderation.rs | 10 +++------- src/events/ready.rs | 2 +- src/main.rs | 16 +++++++++------- src/utils/moderation.rs | 13 ++++++++----- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 105fd8b..19ddd4d 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,4 +1,4 @@ pub mod configuration; +pub mod misc; pub mod moderation; pub mod utils; -pub mod misc; \ No newline at end of file diff --git a/src/commands/moderation.rs b/src/commands/moderation.rs index 2acd345..d96daae 100644 --- a/src/commands/moderation.rs +++ b/src/commands/moderation.rs @@ -30,7 +30,7 @@ pub async fn unmute( &ctx, ModerationKind::Unmute( queue_unmute_member( - ctx.discord(), + &ctx.discord().http, &data.database, &member, configuration.general.mute.role, @@ -161,7 +161,7 @@ pub async fn mute( data.pending_unmutes.insert( member.user.id.0, queue_unmute_member( - ctx.discord(), + &ctx.discord().http, &data.database, &member, mute_role_id, @@ -303,11 +303,7 @@ pub async fn ban( .unwrap(); let ban_result = member - .ban_with_reason( - &ctx.discord().http, - cmp::min(dmd.unwrap_or(0), 7), - reason, - ) + .ban_with_reason(&ctx.discord().http, cmp::min(dmd.unwrap_or(0), 7), reason) .await; let embed_color = ctx.data().read().await.configuration.general.embed_color; diff --git a/src/events/ready.rs b/src/events/ready.rs index a339ef9..47d8019 100644 --- a/src/events/ready.rs +++ b/src/events/ready.rs @@ -39,7 +39,7 @@ pub async fn load_muted_members(ctx: &serenity::Context, _: &serenity::Ready) { data.pending_unmutes.insert( member.user.id.0, queue_unmute_member( - ctx, + &ctx.http, &data.database, &member, mute_role_id, diff --git a/src/main.rs b/src/main.rs index 45b6eaf..da9611f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ impl serenity::TypeMapKey for Data { pub struct Data { configuration: Configuration, - database: Database, + database: Arc, pending_unmutes: HashMap>>, } @@ -67,12 +67,14 @@ async fn main() { let data = Arc::new(RwLock::new(Data { configuration, - database: Database::new( - &env::var("MONGODB_URI").expect("MONGODB_URI environment variable not set"), - "revanced_discord_bot", - ) - .await - .unwrap(), + database: Arc::new( + Database::new( + &env::var("MONGODB_URI").expect("MONGODB_URI environment variable not set"), + "revanced_discord_bot", + ) + .await + .unwrap(), + ), pending_unmutes: HashMap::new(), })); diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index cda69b6..7147542 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -1,3 +1,6 @@ +use std::sync::Arc; + +use poise::serenity_prelude::Http; use tokio::task::JoinHandle; use super::*; @@ -11,13 +14,13 @@ pub enum ModerationKind { } pub fn queue_unmute_member( - ctx: &serenity::Context, - database: &Database, + http: &Arc, + database: &Arc, member: &Member, mute_role_id: u64, mute_duration: u64, ) -> JoinHandle> { - let ctx = ctx.clone(); + let http = http.clone(); let database = database.clone(); let mut member = member.clone(); @@ -46,9 +49,9 @@ pub fn queue_unmute_member( .map(|r| RoleId::from(r.parse::().unwrap())) .collect::>(); - if let Err(add_role_result) = member.add_roles(&ctx.http, &taken_roles).await { + if let Err(add_role_result) = member.add_roles(&http, &taken_roles).await { Some(Error::from(add_role_result)) - } else if let Err(remove_result) = member.remove_role(ctx.http, mute_role_id).await { + } else if let Err(remove_result) = member.remove_role(http, mute_role_id).await { Some(Error::from(remove_result)) } else { None