From 44399fe2775abb17bac2a3253e78db33621add51 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sat, 31 Aug 2024 19:21:01 +0100 Subject: [PATCH] fix: small fixes to actually make it work --- db/migrations/4_committee-mk-i.sql | 2 +- src/commands/committee.rs | 27 ++++++++++++++------------- src/lib.rs | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/db/migrations/4_committee-mk-i.sql b/db/migrations/4_committee-mk-i.sql index c4a78e4..25f925f 100644 --- a/db/migrations/4_committee-mk-i.sql +++ b/db/migrations/4_committee-mk-i.sql @@ -3,5 +3,5 @@ CREATE TABLE IF NOT EXISTS committee ( discord integer PRIMARY KEY, email text not null, auth_code text not null, - committee integer DEFAULT 0, + committee integer DEFAULT 0 ); \ No newline at end of file diff --git a/src/commands/committee.rs b/src/commands/committee.rs index d135570..0702a0e 100644 --- a/src/commands/committee.rs +++ b/src/commands/committee.rs @@ -13,12 +13,13 @@ use serenity::{ prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue}, }, }; -use skynet_discord_bot::{get_now_iso, random_string, Config, DataBase, Wolves, WolvesVerify}; +use skynet_discord_bot::{random_string, Config, DataBase}; use sqlx::{Pool, Sqlite}; pub mod link { use serenity::model::id::GuildId; + use skynet_discord_bot::Committee; use super::*; pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { @@ -101,8 +102,8 @@ pub mod link { .create_option(|option| option.name("email").description("UL Wolves Committee Email").kind(CommandOptionType::String).required(true)) } - pub async fn get_server_member_discord(db: &Pool, user: &UserId) -> Option { - sqlx::query_as::<_, Wolves>( + pub async fn get_server_member_discord(db: &Pool, user: &UserId) -> Option { + sqlx::query_as::<_, Committee>( r#" SELECT * FROM committee @@ -175,8 +176,8 @@ pub mod link { mailer.send(&email) } - pub async fn get_verify_from_db(db: &Pool, user: &UserId) -> Option { - sqlx::query_as::<_, WolvesVerify>( + pub async fn get_verify_from_db(db: &Pool, user: &UserId) -> Option { + sqlx::query_as::<_, Committee>( r#" SELECT * FROM committee @@ -189,8 +190,8 @@ pub mod link { .ok() } - async fn save_to_db(db: &Pool, email: &str, auth: &str, user: &UserId) -> Result, sqlx::Error> { - sqlx::query_as::<_, WolvesVerify>( + async fn save_to_db(db: &Pool, email: &str, auth: &str, user: &UserId) -> Result, sqlx::Error> { + sqlx::query_as::<_, Committee>( " INSERT INTO committee (email, discord, auth_code) VALUES (?1, ?2, ?3) @@ -209,7 +210,7 @@ pub mod verify { use super::*; use crate::commands::committee::link::{get_verify_from_db}; use serenity::model::user::User; - use skynet_discord_bot::{get_server_config, ServerMembersWolves, Servers}; + use skynet_discord_bot::{Committee}; use sqlx::Error; pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { @@ -227,7 +228,7 @@ pub mod verify { let db_lock = { let data_read = ctx.data.read().await; - data_read.get::().expect("Expected Databse in TypeMap.").clone() + data_read.get::().expect("Expected Database in TypeMap.").clone() }; let db = db_lock.read().await; @@ -257,7 +258,7 @@ pub mod verify { return "Invalid verification code".to_string(); } - return match set_discord(&db, &command.user.id).await { + match set_discord(&db, &command.user.id).await { Ok(_) => { // get teh right roles for the user set_server_roles(&command.user, ctx).await; @@ -267,7 +268,7 @@ pub mod verify { println!("{:?}", e); "Failed to save, please try /link_committee again".to_string() } - }; + } } pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { @@ -280,8 +281,8 @@ pub mod verify { }) } - async fn set_discord(db: &Pool, discord: &UserId) -> Result, Error> { - sqlx::query_as::<_, Wolves>( + async fn set_discord(db: &Pool, discord: &UserId) -> Result, Error> { + sqlx::query_as::<_, Committee>( " UPDATE committee SET committee = 1 diff --git a/src/lib.rs b/src/lib.rs index f05e58b..20fcb46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -218,6 +218,28 @@ impl<'r> FromRow<'r, SqliteRow> for WolvesVerify { } } + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct Committee { + pub email: String, + pub discord: UserId, + pub auth_code: String, + pub committee: i64, +} +impl<'r> FromRow<'r, SqliteRow> for Committee { + fn from_row(row: &'r SqliteRow) -> Result { + let user_tmp: i64 = row.try_get("discord")?; + let discord = UserId::from(user_tmp as u64); + + Ok(Self { + email: row.try_get("email")?, + discord, + auth_code: row.try_get("auth_code")?, + committee: row.try_get("committee")?, + }) + } +} + #[derive(Debug, Clone, Deserialize, Serialize)] pub struct Servers { pub server: GuildId,