fix: better compiund types because of a join
This commit is contained in:
parent
36e6bea543
commit
6eb61c6798
5 changed files with 38 additions and 33 deletions
|
@ -43,7 +43,6 @@ impl From<&RecordCSV> for Wolves {
|
||||||
Self {
|
Self {
|
||||||
id_wolves: input.mem_id.to_owned(),
|
id_wolves: input.mem_id.to_owned(),
|
||||||
email: input.email.to_owned(),
|
email: input.email.to_owned(),
|
||||||
verified: false,
|
|
||||||
discord: None,
|
discord: None,
|
||||||
minecraft: None,
|
minecraft: None,
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ use serenity::{
|
||||||
},
|
},
|
||||||
Client,
|
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 sqlx::{Pool, Sqlite};
|
||||||
use std::{process, sync::Arc};
|
use std::{process, sync::Arc};
|
||||||
use tokio::sync::RwLock;
|
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> {
|
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<ServerMembersWolves> {
|
||||||
sqlx::query_as::<_, Wolves>(
|
sqlx::query_as::<_, ServerMembersWolves>(
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM server_members
|
FROM server_members
|
||||||
JOIN wolves ON server_members.id_wolves = wolves.id_wolves
|
JOIN wolves USING (id_wolves)
|
||||||
WHERE (
|
WHERE (
|
||||||
server = ?
|
server = ?
|
||||||
AND discord IS NOT NULL
|
AND discord IS NOT NULL
|
||||||
|
|
|
@ -62,7 +62,7 @@ pub(crate) mod link {
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
};
|
};
|
||||||
|
|
||||||
if details.verified {
|
if details.discord.is_some() {
|
||||||
return "Email already verified".to_string();
|
return "Email already verified".to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
58
src/lib.rs
58
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<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)]
|
#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)]
|
||||||
pub struct Wolves {
|
pub struct Wolves {
|
||||||
pub id_wolves: String,
|
pub id_wolves: String,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub verified: bool,
|
|
||||||
pub discord: Option<String>,
|
pub discord: Option<String>,
|
||||||
pub minecraft: 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,
|
id_wolves text PRIMARY KEY,
|
||||||
email text not null,
|
email text not null,
|
||||||
discord text,
|
discord text,
|
||||||
minecraft text,
|
minecraft text
|
||||||
verified integer DEFAULT FALSE
|
|
||||||
)",
|
)",
|
||||||
)
|
)
|
||||||
.execute(&pool)
|
.execute(&pool)
|
||||||
|
@ -258,36 +281,19 @@ pub async fn get_server_config(db: &Pool<Sqlite>, server: &GuildId) -> Option<Se
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &guild::Member) -> Option<ServerMembers> {
|
pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &guild::Member) -> Result<ServerMembersWolves, Error> {
|
||||||
let wolves_data = sqlx::query_as::<_, Wolves>(
|
sqlx::query_as::<_, ServerMembersWolves>(
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM wolves
|
FROM server_members
|
||||||
WHERE discord = ?
|
JOIN wolves USING (id_wolves)
|
||||||
|
WHERE server = ? AND discord = ?
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(*server.as_u64() as i64)
|
||||||
.bind(&member.user.name)
|
.bind(&member.user.name)
|
||||||
.fetch_one(db)
|
.fetch_one(db)
|
||||||
.await;
|
.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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_server_config_bulk(db: &Pool<Sqlite>) -> Vec<Servers> {
|
pub async fn get_server_config_bulk(db: &Pool<Sqlite>) -> Vec<Servers> {
|
||||||
|
|
|
@ -31,7 +31,7 @@ impl EventHandler for Handler {
|
||||||
Some(x) => x,
|
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![];
|
let mut roles = vec![];
|
||||||
|
|
||||||
if let Some(role) = &config.role_past {
|
if let Some(role) = &config.role_past {
|
||||||
|
|
Loading…
Reference in a new issue