feat(database/guild): adding the protect management
This commit is contained in:
parent
4a5536dbe1
commit
55d7633614
1 changed files with 48 additions and 0 deletions
|
|
@ -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<Option<Guild>, sqlx::Error> {
|
||||
let guild: Option<Guild> = 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<Option<String>, sqlx::Error> {
|
||||
let query: String = format!("SELECT {} FROM guilds WHERE guild_id = $1", log_select(asked));
|
||||
let channel_id: Option<String> = 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<Option<bool>, sqlx::Error> {
|
||||
let query: String = format!("SELECT {} FROM guilds WHERE guild_id = $1", protect_select(asked));
|
||||
let state: Option<bool> = 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(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue