diff --git a/src/database/guild.rs b/src/database/guild.rs index 94f2974..4aa7dd0 100644 --- a/src/database/guild.rs +++ b/src/database/guild.rs @@ -15,6 +15,14 @@ pub enum LogChannel { Server, } +pub enum Protect { + AntiBot, + AntiChannel, + AntiMassMention, + AntiMassban, + AntiPerm, + AntiRank, +} fn log_select(asked: LogChannel) -> &'static str { match asked { @@ -28,6 +36,27 @@ fn log_select(asked: LogChannel) -> &'static str { } } +fn protect_select(asked: Protect) -> &'static str { + match asked { + Protect::AntiBot => "protect_anti_bot", + Protect::AntiChannel => "protect_anti_channel", + Protect::AntiMassMention => "protect_anti_mass_mention", + Protect::AntiMassban => "protect_anti_massban", + Protect::AntiPerm => "protect_anti_perm", + Protect::AntiRank => "protect_anti_rank", + } +} + +pub async fn get(db: &PgPool, guild_id: &str) -> Result, sqlx::Error> { + let guild: Option = query_as::<_, Guild>( + "SELECT * FROM guilds WHERE guild_id = $1", + ) + .bind(guild_id) + .fetch_optional(db) + .await?; + Ok(guild) +} + pub async fn get_log(db: &PgPool, guild_id: &str, asked: LogChannel) -> Result, sqlx::Error> { let query: String = format!("SELECT {} FROM guilds WHERE guild_id = $1", log_select(asked)); let channel_id: Option = query_scalar(&query) @@ -46,3 +75,22 @@ pub async fn set_log(db: &PgPool, user_id: &str, asked: LogChannel, value: &str) .await?; Ok(()) } + +pub async fn get_protect(db: &PgPool, guild_id: &str, asked: Protect) -> Result, sqlx::Error> { + let query: String = format!("SELECT {} FROM guilds WHERE guild_id = $1", protect_select(asked)); + let state: Option = query_scalar(&query) + .bind(guild_id) + .fetch_optional(db) + .await?; + Ok(state) +} + +pub async fn set_protect(db: &PgPool, user_id: &str, asked: Protect, value: &str) -> Result<(), sqlx::Error> { + let query = format!("UPDATE guilds set {} = $1 WHERE guild_id = $2", log_protect(asked)); + query(&query) + .bind(value) + .bind(user_id) + .execute(db) + .await?; + Ok(()) +}