From 204e7e61949d4d613ab43e20c30e48f51cb985b4 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 21 Aug 2022 03:31:18 +0200 Subject: [PATCH] refactor: simplify `Data` --- src/commands/configuration.rs | 12 ++---------- src/commands/moderation.rs | 28 ++++++++++++++-------------- src/events/message_create.rs | 2 -- src/events/thread_create.rs | 6 +----- src/main.rs | 28 +++++++++++++--------------- 5 files changed, 30 insertions(+), 46 deletions(-) diff --git a/src/commands/configuration.rs b/src/commands/configuration.rs index 5792087..e308138 100644 --- a/src/commands/configuration.rs +++ b/src/commands/configuration.rs @@ -10,7 +10,7 @@ pub async fn reload(ctx: Context<'_>) -> Result<(), Error> { // Use the embed color from the updated configuration let embed_color = configuration.general.embed_color; // Also save the new configuration to the user data - *ctx.data().write().await.configuration.write().await = configuration; + ctx.data().write().await.configuration = configuration; debug!("{} reloaded the configuration.", ctx.author().name); @@ -29,15 +29,7 @@ pub async fn reload(ctx: Context<'_>) -> Result<(), Error> { pub async fn stop(ctx: Context<'_>) -> Result<(), Error> { debug!("{} stopped the bot.", ctx.author().name); - let color = ctx - .data() - .read() - .await - .configuration - .read() - .await - .general - .embed_color; + let color = ctx.data().read().await.configuration.general.embed_color; ctx.send(|f| { f.ephemeral(true) .embed(|f| f.description("Stopped the bot.").color(color)) diff --git a/src/commands/moderation.rs b/src/commands/moderation.rs index cb1cf13..b7a82cc 100644 --- a/src/commands/moderation.rs +++ b/src/commands/moderation.rs @@ -16,9 +16,9 @@ pub async fn unmute( ctx.defer().await.expect("Failed to defer"); let data = &ctx.data().read().await; - let configuration = data.configuration.read().await; + let configuration = &data.configuration; - if let Some(pending_unmute) = data.pending_unmutes.read().await.get(&member.user.id.0) { + if let Some(pending_unmute) = data.pending_unmutes.lock().await.get(&member.user.id.0) { trace!("Cancelling pending unmute for {}", member.user.id.0); pending_unmute.abort(); } @@ -83,7 +83,7 @@ pub async fn mute( let unmute_time = now + mute_duration; let data = ctx.data().read().await; - let configuration = data.configuration.read().await; + let configuration = &data.configuration; let embed_color = configuration.general.embed_color; let mute = &configuration.general.mute; let mute_role_id = mute.role; @@ -149,22 +149,22 @@ pub async fn mute( } }; - if let Some(pending_unmute) = data.pending_unmutes.read().await.get(&member.user.id.0) { + let mut pending_unmute = data.pending_unmutes.lock().await; + if let Some(pending_unmute) = pending_unmute.get(&member.user.id.0) { trace!("Cancelling pending unmute for {}", member.user.id.0); pending_unmute.abort(); } - data.pending_unmutes.write().await.insert( - member.user.id.0, - queue_unmute_member( - ctx.discord(), - &data.database, - &member, - mute_role_id, - mute_duration.num_seconds() as u64, - ), + let r = queue_unmute_member( + ctx.discord(), + &data.database, + &member, + mute_role_id, + mute_duration.num_seconds() as u64, ); + pending_unmute.insert(member.user.id.0, r); + respond_mute_command( &ctx, ModerationKind::Mute( @@ -197,7 +197,7 @@ pub async fn purge( const MAX_BULK_DELETE_AGO_SECS: i64 = 60 * 60 * 24 * 14; let data = ctx.data().read().await; - let configuration = data.configuration.read().await; + let configuration = &data.configuration; let embed_color = configuration.general.embed_color; let channel = ctx.channel_id(); let too_old_timestamp = Utc::now().timestamp() - MAX_BULK_DELETE_AGO_SECS; diff --git a/src/events/message_create.rs b/src/events/message_create.rs index 6e005fc..fadcf29 100644 --- a/src/events/message_create.rs +++ b/src/events/message_create.rs @@ -20,8 +20,6 @@ pub async fn message_create(ctx: &serenity::Context, new_message: &serenity::Mes .read() .await .configuration - .read() - .await .message_responses .iter() .find(|&response| { diff --git a/src/events/thread_create.rs b/src/events/thread_create.rs index 2906e28..4a8266b 100644 --- a/src/events/thread_create.rs +++ b/src/events/thread_create.rs @@ -14,11 +14,7 @@ pub async fn thread_create(ctx: &serenity::Context, thread: &serenity::GuildChan let data_lock = get_data_lock(ctx).await; let configuration_lock = data_lock.read().await; - let thread_introductions = &configuration_lock - .configuration - .read() - .await - .thread_introductions; + let thread_introductions = &configuration_lock.configuration.thread_introductions; if let Some(introducer) = thread_introductions.iter().find(|introducer| { introducer diff --git a/src/main.rs b/src/main.rs index addbbb4..962dbe8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use commands::{configuration, moderation}; use db::database::Database; use events::Handler; -use poise::serenity_prelude::{self as serenity, RwLock, UserId}; +use poise::serenity_prelude::{self as serenity, Mutex, RwLock, UserId}; use tokio::task::JoinHandle; use tracing::{error, trace}; use utils::bot::load_configuration; @@ -27,9 +27,9 @@ impl serenity::TypeMapKey for Data { } pub struct Data { - configuration: Arc>, - database: Arc, - pending_unmutes: Arc>>>>, + configuration: Configuration, + database: Database, + pending_unmutes: Mutex>>>, } #[tokio::main] @@ -64,16 +64,14 @@ async fn main() { .collect(); let data = Arc::new(RwLock::new(Data { - configuration: Arc::new(RwLock::new(configuration)), - database: Arc::new( - Database::new( - &env::var("MONGODB_URI").expect("MONGODB_URI environment variable not set"), - "revanced_discord_bot", - ) - .await - .unwrap(), - ), - pending_unmutes: Arc::new(RwLock::new(HashMap::new())), + configuration, + database: Database::new( + &env::var("MONGODB_URI").expect("MONGODB_URI environment variable not set"), + "revanced_discord_bot", + ) + .await + .unwrap(), + pending_unmutes: Mutex::new(HashMap::new()), })); let handler = Arc::new(Handler::new( @@ -91,7 +89,7 @@ async fn main() { Box::pin(async move { if let Some(member) = ctx.author_member().await { let data_lock = &ctx.data().read().await; - let configuration = &data_lock.configuration.read().await; + let configuration = &data_lock.configuration; let administrators = &configuration.administrators; if !(administrators