From 6eb61c67980bd2a00b9dc46c685ac588d53927d3 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sun, 17 Sep 2023 21:17:57 +0100 Subject: [PATCH] fix: better compiund types because of a join --- src/bin/update_data.rs | 1 - src/bin/update_users.rs | 8 +++--- src/commands/link_email.rs | 2 +- src/lib.rs | 58 +++++++++++++++++++++----------------- src/main.rs | 2 +- 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/bin/update_data.rs b/src/bin/update_data.rs index d9c683f..2b1d1f7 100644 --- a/src/bin/update_data.rs +++ b/src/bin/update_data.rs @@ -43,7 +43,6 @@ impl From<&RecordCSV> for Wolves { Self { id_wolves: input.mem_id.to_owned(), email: input.email.to_owned(), - verified: false, discord: None, minecraft: None, } diff --git a/src/bin/update_users.rs b/src/bin/update_users.rs index b0098d7..28557c3 100644 --- a/src/bin/update_users.rs +++ b/src/bin/update_users.rs @@ -7,7 +7,7 @@ use serenity::{ }, Client, }; -use skynet_discord_bot::{db_init, get_config, get_now_iso, get_server_config_bulk, Config, DataBase, Servers, Wolves}; +use skynet_discord_bot::{db_init, get_config, get_now_iso, get_server_config_bulk, Config, DataBase, ServerMembersWolves, Servers, Wolves}; use sqlx::{Pool, Sqlite}; use std::{process, sync::Arc}; use tokio::sync::RwLock; @@ -131,12 +131,12 @@ async fn bulk_check(ctx: Arc) { } } -async fn get_server_member_bulk(db: &Pool, server: &GuildId) -> Vec { - sqlx::query_as::<_, Wolves>( +async fn get_server_member_bulk(db: &Pool, server: &GuildId) -> Vec { + sqlx::query_as::<_, ServerMembersWolves>( r#" SELECT * FROM server_members - JOIN wolves ON server_members.id_wolves = wolves.id_wolves + JOIN wolves USING (id_wolves) WHERE ( server = ? AND discord IS NOT NULL diff --git a/src/commands/link_email.rs b/src/commands/link_email.rs index 45b07e7..59e7b93 100644 --- a/src/commands/link_email.rs +++ b/src/commands/link_email.rs @@ -62,7 +62,7 @@ pub(crate) mod link { Some(x) => x, }; - if details.verified { + if details.discord.is_some() { return "Email already verified".to_string(); } diff --git a/src/lib.rs b/src/lib.rs index 54c2174..cd5ee35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,11 +119,35 @@ impl<'r> FromRow<'r, SqliteRow> for ServerMembers { } } +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct ServerMembersWolves { + pub server: GuildId, + pub id_wolves: String, + pub expiry: String, + pub email: String, + pub discord: Option, + pub minecraft: Option, +} +impl<'r> FromRow<'r, SqliteRow> for ServerMembersWolves { + fn from_row(row: &'r SqliteRow) -> Result { + let server_tmp: i64 = row.try_get("server")?; + let server = GuildId::from(server_tmp as u64); + + Ok(Self { + server, + id_wolves: row.try_get("id_wolves")?, + expiry: row.try_get("expiry")?, + email: row.try_get("email")?, + discord: row.try_get("discord")?, + minecraft: row.try_get("minecraft")?, + }) + } +} + #[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)] pub struct Wolves { pub id_wolves: String, pub email: String, - pub verified: bool, pub discord: Option, pub minecraft: Option, } @@ -192,8 +216,7 @@ pub async fn db_init(config: &Config) -> Result, Error> { id_wolves text PRIMARY KEY, email text not null, discord text, - minecraft text, - verified integer DEFAULT FALSE + minecraft text )", ) .execute(&pool) @@ -258,36 +281,19 @@ pub async fn get_server_config(db: &Pool, server: &GuildId) -> Option, server: &GuildId, member: &guild::Member) -> Option { - let wolves_data = sqlx::query_as::<_, Wolves>( +pub async fn get_server_member(db: &Pool, server: &GuildId, member: &guild::Member) -> Result { + sqlx::query_as::<_, ServerMembersWolves>( r#" SELECT * - FROM wolves - WHERE discord = ? + FROM server_members + JOIN wolves USING (id_wolves) + WHERE server = ? AND discord = ? "#, ) .bind(*server.as_u64() as i64) .bind(&member.user.name) .fetch_one(db) - .await; - - if let Ok(user_wolves) = wolves_data { - // check if the suer is on the server - return sqlx::query_as::<_, ServerMembers>( - r#" - SELECT * - FROM server_members - WHERE server = ? AND id_wolves = ? - "#, - ) - .bind(*server.as_u64() as i64) - .bind(&user_wolves.id_wolves) - .fetch_one(db) - .await - .ok(); - } - - None + .await } pub async fn get_server_config_bulk(db: &Pool) -> Vec { diff --git a/src/main.rs b/src/main.rs index be2e4b7..e7a8655 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ impl EventHandler for Handler { Some(x) => x, }; - if get_server_member(&db, &new_member.guild_id, &new_member).await.is_some() { + if get_server_member(&db, &new_member.guild_id, &new_member).await.is_ok() { let mut roles = vec![]; if let Some(role) = &config.role_past {