From 815cb0df3b3e3f9dd2078b00f85754da87b1d55e Mon Sep 17 00:00:00 2001 From: seth Date: Tue, 5 Dec 2023 05:17:49 -0500 Subject: refactor: centralize storage handlers --- src/commands/moderation/actions.rs | 7 +++++-- src/commands/moderation/config.rs | 30 +++++++++++++----------------- 2 files changed, 18 insertions(+), 19 deletions(-) (limited to 'src/commands/moderation') diff --git a/src/commands/moderation/actions.rs b/src/commands/moderation/actions.rs index 1050656..4d4d0f8 100644 --- a/src/commands/moderation/actions.rs +++ b/src/commands/moderation/actions.rs @@ -2,6 +2,7 @@ use crate::colors::Colors; use crate::Context; use color_eyre::eyre::{eyre, Result}; +use log::*; use poise::serenity_prelude::{CreateEmbed, User}; fn create_moderation_embed( @@ -23,7 +24,7 @@ fn create_moderation_embed( |e: &mut CreateEmbed| e.title(title).fields(fields).color(Colors::Red) } -// ban a user +/// ban a user #[poise::command( slash_command, prefix_command, @@ -42,6 +43,7 @@ pub async fn ban_user( let reason = reason.unwrap_or("n/a".to_string()); + debug!("Banning user {} with reason {reason}", user.id); if reason != "n/a" { guild.ban_with_reason(ctx, &user, days, &reason).await?; } else { @@ -55,7 +57,7 @@ pub async fn ban_user( Ok(()) } -// kick a user +/// kick a user #[poise::command( slash_command, prefix_command, @@ -68,6 +70,7 @@ pub async fn kick_user(ctx: Context<'_>, user: User, reason: Option) -> let reason = reason.unwrap_or("n/a".to_string()); + debug!("Kicking user {} for reason {reason}", user.id); if reason != "n/a" { guild.kick_with_reason(ctx, &user, &reason).await?; } else { diff --git a/src/commands/moderation/config.rs b/src/commands/moderation/config.rs index 2d1410c..d64c4cc 100644 --- a/src/commands/moderation/config.rs +++ b/src/commands/moderation/config.rs @@ -1,5 +1,5 @@ -use crate::settings::{Settings, SettingsProperties}; -use crate::Context; +use crate::{storage, Context}; +use storage::SettingsProperties; use color_eyre::eyre::{eyre, Context as _, ContextCompat, Result}; use log::*; @@ -33,9 +33,9 @@ pub async fn set( #[description = "Enables 'extra' commands like teawiespam and copypasta. Defaults to false."] optional_commands_enabled: Option, ) -> Result<()> { - let redis = &ctx.data().redis; + let storage = &ctx.data().storage; let gid = ctx.guild_id().unwrap_or_default(); - let mut settings = Settings::from_redis(redis, &gid).await?; + let mut settings = storage.get_guild_settings(&gid).await?; let previous_settings = settings.clone(); if let Some(channel) = pinboard_channel { @@ -48,24 +48,21 @@ pub async fn set( settings.pinboard_watch = Some(prev); } else { let new = Vec::from([watch.id]); - debug!("Setting pinboard_watch to {new:#?} for {} in Redis", gid); + debug!("Setting pinboard_watch to {new:#?} for {}", gid); settings.pinboard_watch = Some(new); } } if let Some(channel) = reactboard_channel { - debug!( - "Setting reactboard_channel to {channel} for {} in Redis", - gid - ); + debug!("Setting reactboard_channel to {channel} for {}", gid); settings.reactboard_channel = Some(channel.id); } if let Some(requirement) = reactboard_requirement { debug!( - "Setting reactboard_requirement to {requirement} for {} in Redis", + "Setting reactboard_requirement to {requirement} for {}", gid ); @@ -82,25 +79,24 @@ pub async fn set( settings.reactboard_reactions = Some(prev); } else { let new = Vec::from([emoji]); - debug!("Setting pinboard_watch to {new:#?} for {} in Redis", gid); + debug!("Setting pinboard_watch to {new:#?} for {}", gid); settings.reactboard_reactions = Some(new); } } if let Some(enabled) = optional_commands_enabled { - debug!( - "Setting optional_commands_enabled to {enabled} for {} in Redis", - gid - ); + debug!("Setting optional_commands_enabled to {enabled} for {}", gid); settings.optional_commands_enabled = enabled; } if previous_settings != settings { - settings.save(redis).await?; + debug!("Updating settings key for {gid}"); + storage.create_settings_key(settings).await?; ctx.reply("Configuration updated!").await?; } else { + debug!("Not updating settings key for {gid} since no changes were made"); ctx.reply("No changes made, so i'm not updating anything") .await?; } @@ -117,7 +113,7 @@ pub async fn get( .guild_id() .wrap_err_with(|| eyre!("Failed to get GuildId from context!"))?; - let settings = Settings::from_redis(&ctx.data().redis, gid).await?; + let settings = ctx.data().storage.get_guild_settings(gid).await?; let value = match setting { SettingsProperties::GuildId => settings.guild_id.to_string(), -- cgit v1.2.3