Skip to content

Commit

Permalink
perf: clone Arc of data.database
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Aug 25, 2022
1 parent 8840a65 commit 3ad345b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod configuration;
pub mod misc;
pub mod moderation;
pub mod utils;
pub mod misc;
10 changes: 3 additions & 7 deletions src/commands/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/events/ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 9 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl serenity::TypeMapKey for Data {

pub struct Data {
configuration: Configuration,
database: Database,
database: Arc<Database>,
pending_unmutes: HashMap<u64, JoinHandle<Option<Error>>>,
}

Expand Down Expand Up @@ -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(),
}));

Expand Down
13 changes: 8 additions & 5 deletions src/utils/moderation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::sync::Arc;

use poise::serenity_prelude::Http;
use tokio::task::JoinHandle;

use super::*;
Expand All @@ -11,13 +14,13 @@ pub enum ModerationKind {
}

pub fn queue_unmute_member(
ctx: &serenity::Context,
database: &Database,
http: &Arc<Http>,
database: &Arc<Database>,
member: &Member,
mute_role_id: u64,
mute_duration: u64,
) -> JoinHandle<Option<Error>> {
let ctx = ctx.clone();
let http = http.clone();
let database = database.clone();
let mut member = member.clone();

Expand Down Expand Up @@ -46,9 +49,9 @@ pub fn queue_unmute_member(
.map(|r| RoleId::from(r.parse::<u64>().unwrap()))
.collect::<Vec<_>>();

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
Expand Down

0 comments on commit 3ad345b

Please sign in to comment.