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/main.rs | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index afedd1a..d355307 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,32 +5,31 @@ use log::*; use poise::{ serenity_prelude as serenity, EditTracker, Framework, FrameworkOptions, PrefixFrameworkOptions, }; -use redis::AsyncCommands; -use settings::Settings; +use storage::Storage; mod api; mod colors; mod commands; mod consts; mod handlers; -mod settings; +mod storage; mod utils; type Context<'a> = poise::Context<'a, Data, Report>; #[derive(Clone)] pub struct Data { - redis: redis::Client, + storage: Storage, } impl Data { pub fn new() -> Result { let redis_url = std::env::var("REDIS_URL") - .wrap_err_with(|| eyre!("Couldn't find Redis URL in environment!"))?; + .wrap_err_with(|| "Couldn't find Redis URL in environment!")?; - let redis = redis::Client::open(redis_url)?; + let storage = Storage::new(&redis_url)?; - Ok(Self { redis }) + Ok(Self { storage }) } } @@ -75,25 +74,18 @@ async fn main() -> Result<()> { info!("Registered global commands!"); // register "extra" commands in guilds that allow it - let mut con = data.redis.get_async_connection().await?; - - info!("Fetching all guild settings from Redis...this might take a while"); - let guilds: Vec = con.keys(format!("{}:*", settings::ROOT_KEY)).await?; + info!("Fetching opted guilds"); + let guilds = data.storage.get_opted_guilds().await?; for guild in guilds { - let settings: Settings = con.get(guild).await?; - - if settings.optional_commands_enabled { - poise::builtins::register_in_guild( - ctx, - &commands::to_guild_commands(), - settings.guild_id, - ) - .await?; - info!("Registered guild commands to {}", settings.guild_id); - } else { - debug!("Not registering guild commands to {} since optional_commands_enabled is False", settings.guild_id); - } + poise::builtins::register_in_guild( + ctx, + &commands::to_optional_commands(), + guild, + ) + .await?; + + info!("Registered guild commands to {}", guild); } Ok(data) -- cgit v1.2.3