refactor: simplify Data

This commit is contained in:
oSumAtrIX 2022-08-21 03:31:18 +02:00
parent 3f05f8cd92
commit 204e7e6194
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
5 changed files with 30 additions and 46 deletions

View File

@ -10,7 +10,7 @@ pub async fn reload(ctx: Context<'_>) -> Result<(), Error> {
// Use the embed color from the updated configuration // Use the embed color from the updated configuration
let embed_color = configuration.general.embed_color; let embed_color = configuration.general.embed_color;
// Also save the new configuration to the user data // 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); 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> { pub async fn stop(ctx: Context<'_>) -> Result<(), Error> {
debug!("{} stopped the bot.", ctx.author().name); debug!("{} stopped the bot.", ctx.author().name);
let color = ctx let color = ctx.data().read().await.configuration.general.embed_color;
.data()
.read()
.await
.configuration
.read()
.await
.general
.embed_color;
ctx.send(|f| { ctx.send(|f| {
f.ephemeral(true) f.ephemeral(true)
.embed(|f| f.description("Stopped the bot.").color(color)) .embed(|f| f.description("Stopped the bot.").color(color))

View File

@ -16,9 +16,9 @@ pub async fn unmute(
ctx.defer().await.expect("Failed to defer"); ctx.defer().await.expect("Failed to defer");
let data = &ctx.data().read().await; 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); trace!("Cancelling pending unmute for {}", member.user.id.0);
pending_unmute.abort(); pending_unmute.abort();
} }
@ -83,7 +83,7 @@ pub async fn mute(
let unmute_time = now + mute_duration; let unmute_time = now + mute_duration;
let data = ctx.data().read().await; let data = ctx.data().read().await;
let configuration = data.configuration.read().await; let configuration = &data.configuration;
let embed_color = configuration.general.embed_color; let embed_color = configuration.general.embed_color;
let mute = &configuration.general.mute; let mute = &configuration.general.mute;
let mute_role_id = mute.role; 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); trace!("Cancelling pending unmute for {}", member.user.id.0);
pending_unmute.abort(); pending_unmute.abort();
} }
data.pending_unmutes.write().await.insert( let r = queue_unmute_member(
member.user.id.0, ctx.discord(),
queue_unmute_member( &data.database,
ctx.discord(), &member,
&data.database, mute_role_id,
&member, mute_duration.num_seconds() as u64,
mute_role_id,
mute_duration.num_seconds() as u64,
),
); );
pending_unmute.insert(member.user.id.0, r);
respond_mute_command( respond_mute_command(
&ctx, &ctx,
ModerationKind::Mute( ModerationKind::Mute(
@ -197,7 +197,7 @@ pub async fn purge(
const MAX_BULK_DELETE_AGO_SECS: i64 = 60 * 60 * 24 * 14; const MAX_BULK_DELETE_AGO_SECS: i64 = 60 * 60 * 24 * 14;
let data = ctx.data().read().await; let data = ctx.data().read().await;
let configuration = data.configuration.read().await; let configuration = &data.configuration;
let embed_color = configuration.general.embed_color; let embed_color = configuration.general.embed_color;
let channel = ctx.channel_id(); let channel = ctx.channel_id();
let too_old_timestamp = Utc::now().timestamp() - MAX_BULK_DELETE_AGO_SECS; let too_old_timestamp = Utc::now().timestamp() - MAX_BULK_DELETE_AGO_SECS;

View File

@ -20,8 +20,6 @@ pub async fn message_create(ctx: &serenity::Context, new_message: &serenity::Mes
.read() .read()
.await .await
.configuration .configuration
.read()
.await
.message_responses .message_responses
.iter() .iter()
.find(|&response| { .find(|&response| {

View File

@ -14,11 +14,7 @@ pub async fn thread_create(ctx: &serenity::Context, thread: &serenity::GuildChan
let data_lock = get_data_lock(ctx).await; let data_lock = get_data_lock(ctx).await;
let configuration_lock = data_lock.read().await; let configuration_lock = data_lock.read().await;
let thread_introductions = &configuration_lock let thread_introductions = &configuration_lock.configuration.thread_introductions;
.configuration
.read()
.await
.thread_introductions;
if let Some(introducer) = thread_introductions.iter().find(|introducer| { if let Some(introducer) = thread_introductions.iter().find(|introducer| {
introducer introducer

View File

@ -5,7 +5,7 @@ use std::sync::Arc;
use commands::{configuration, moderation}; use commands::{configuration, moderation};
use db::database::Database; use db::database::Database;
use events::Handler; 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 tokio::task::JoinHandle;
use tracing::{error, trace}; use tracing::{error, trace};
use utils::bot::load_configuration; use utils::bot::load_configuration;
@ -27,9 +27,9 @@ impl serenity::TypeMapKey for Data {
} }
pub struct Data { pub struct Data {
configuration: Arc<RwLock<Configuration>>, configuration: Configuration,
database: Arc<Database>, database: Database,
pending_unmutes: Arc<RwLock<HashMap<u64, JoinHandle<Option<Error>>>>>, pending_unmutes: Mutex<HashMap<u64, JoinHandle<Option<Error>>>>,
} }
#[tokio::main] #[tokio::main]
@ -64,16 +64,14 @@ async fn main() {
.collect(); .collect();
let data = Arc::new(RwLock::new(Data { let data = Arc::new(RwLock::new(Data {
configuration: Arc::new(RwLock::new(configuration)), configuration,
database: Arc::new( database: Database::new(
Database::new( &env::var("MONGODB_URI").expect("MONGODB_URI environment variable not set"),
&env::var("MONGODB_URI").expect("MONGODB_URI environment variable not set"), "revanced_discord_bot",
"revanced_discord_bot", )
) .await
.await .unwrap(),
.unwrap(), pending_unmutes: Mutex::new(HashMap::new()),
),
pending_unmutes: Arc::new(RwLock::new(HashMap::new())),
})); }));
let handler = Arc::new(Handler::new( let handler = Arc::new(Handler::new(
@ -91,7 +89,7 @@ async fn main() {
Box::pin(async move { Box::pin(async move {
if let Some(member) = ctx.author_member().await { if let Some(member) = ctx.author_member().await {
let data_lock = &ctx.data().read().await; let data_lock = &ctx.data().read().await;
let configuration = &data_lock.configuration.read().await; let configuration = &data_lock.configuration;
let administrators = &configuration.administrators; let administrators = &configuration.administrators;
if !(administrators if !(administrators