fix: better compiund types because of a join

This commit is contained in:
silver 2023-09-17 21:17:57 +01:00
parent 36e6bea543
commit 6eb61c6798
5 changed files with 38 additions and 33 deletions

View file

@ -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,
}

View file

@ -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<Context>) {
}
}
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Wolves> {
sqlx::query_as::<_, Wolves>(
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<ServerMembersWolves> {
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

View file

@ -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();
}

View file

@ -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<String>,
pub minecraft: Option<String>,
}
impl<'r> FromRow<'r, SqliteRow> for ServerMembersWolves {
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
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<String>,
pub minecraft: Option<String>,
}
@ -192,8 +216,7 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, 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<Sqlite>, server: &GuildId) -> Option<Se
.ok()
}
pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &guild::Member) -> Option<ServerMembers> {
let wolves_data = sqlx::query_as::<_, Wolves>(
pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &guild::Member) -> Result<ServerMembersWolves, Error> {
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<Sqlite>) -> Vec<Servers> {

View file

@ -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 {