fix: small fixes to actually make it work

This commit is contained in:
silver 2024-08-31 19:21:01 +01:00
parent 790c01949d
commit 44399fe277
3 changed files with 37 additions and 14 deletions

View file

@ -3,5 +3,5 @@ CREATE TABLE IF NOT EXISTS committee (
discord integer PRIMARY KEY, discord integer PRIMARY KEY,
email text not null, email text not null,
auth_code text not null, auth_code text not null,
committee integer DEFAULT 0, committee integer DEFAULT 0
); );

View file

@ -13,12 +13,13 @@ use serenity::{
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue}, 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}; use sqlx::{Pool, Sqlite};
pub mod link { pub mod link {
use serenity::model::id::GuildId; use serenity::model::id::GuildId;
use skynet_discord_bot::Committee;
use super::*; use super::*;
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { 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)) .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<Sqlite>, user: &UserId) -> Option<Wolves> { pub async fn get_server_member_discord(db: &Pool<Sqlite>, user: &UserId) -> Option<Committee> {
sqlx::query_as::<_, Wolves>( sqlx::query_as::<_, Committee>(
r#" r#"
SELECT * SELECT *
FROM committee FROM committee
@ -175,8 +176,8 @@ pub mod link {
mailer.send(&email) mailer.send(&email)
} }
pub async fn get_verify_from_db(db: &Pool<Sqlite>, user: &UserId) -> Option<WolvesVerify> { pub async fn get_verify_from_db(db: &Pool<Sqlite>, user: &UserId) -> Option<Committee> {
sqlx::query_as::<_, WolvesVerify>( sqlx::query_as::<_, Committee>(
r#" r#"
SELECT * SELECT *
FROM committee FROM committee
@ -189,8 +190,8 @@ pub mod link {
.ok() .ok()
} }
async fn save_to_db(db: &Pool<Sqlite>, email: &str, auth: &str, user: &UserId) -> Result<Option<WolvesVerify>, sqlx::Error> { async fn save_to_db(db: &Pool<Sqlite>, email: &str, auth: &str, user: &UserId) -> Result<Option<Committee>, sqlx::Error> {
sqlx::query_as::<_, WolvesVerify>( sqlx::query_as::<_, Committee>(
" "
INSERT INTO committee (email, discord, auth_code) INSERT INTO committee (email, discord, auth_code)
VALUES (?1, ?2, ?3) VALUES (?1, ?2, ?3)
@ -209,7 +210,7 @@ pub mod verify {
use super::*; use super::*;
use crate::commands::committee::link::{get_verify_from_db}; use crate::commands::committee::link::{get_verify_from_db};
use serenity::model::user::User; use serenity::model::user::User;
use skynet_discord_bot::{get_server_config, ServerMembersWolves, Servers}; use skynet_discord_bot::{Committee};
use sqlx::Error; use sqlx::Error;
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
@ -227,7 +228,7 @@ pub mod verify {
let db_lock = { let db_lock = {
let data_read = ctx.data.read().await; let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone() data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone()
}; };
let db = db_lock.read().await; let db = db_lock.read().await;
@ -257,7 +258,7 @@ pub mod verify {
return "Invalid verification code".to_string(); return "Invalid verification code".to_string();
} }
return match set_discord(&db, &command.user.id).await { match set_discord(&db, &command.user.id).await {
Ok(_) => { Ok(_) => {
// get teh right roles for the user // get teh right roles for the user
set_server_roles(&command.user, ctx).await; set_server_roles(&command.user, ctx).await;
@ -267,7 +268,7 @@ pub mod verify {
println!("{:?}", e); println!("{:?}", e);
"Failed to save, please try /link_committee again".to_string() "Failed to save, please try /link_committee again".to_string()
} }
}; }
} }
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
@ -280,8 +281,8 @@ pub mod verify {
}) })
} }
async fn set_discord(db: &Pool<Sqlite>, discord: &UserId) -> Result<Option<Wolves>, Error> { async fn set_discord(db: &Pool<Sqlite>, discord: &UserId) -> Result<Option<Committee>, Error> {
sqlx::query_as::<_, Wolves>( sqlx::query_as::<_, Committee>(
" "
UPDATE committee UPDATE committee
SET committee = 1 SET committee = 1

View file

@ -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<Self, Error> {
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)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Servers { pub struct Servers {
pub server: GuildId, pub server: GuildId,