diff --git a/src/commands/link_email.rs b/src/commands/link_email.rs index 59e7b93..6cf37b7 100644 --- a/src/commands/link_email.rs +++ b/src/commands/link_email.rs @@ -235,7 +235,10 @@ pub(crate) mod link { pub(crate) mod verify { use super::*; use crate::commands::link_email::link::{db_pending_clear_expired, get_verify_from_db}; + use serenity::model::user::User; + use skynet_discord_bot::{get_server_config, ServerMembersWolves, Servers}; use sqlx::Error; + pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { let db_lock = { let data_read = ctx.data.read().await; @@ -274,12 +277,16 @@ pub(crate) mod verify { match db_pending_clear_successful(&db, &command.user.name).await { Ok(_) => { return match set_discord(&db, &command.user.name, &details.email).await { - Ok(_) => "Discord username linked to Wolves".to_string(), + Ok(_) => { + // get teh right roles for the user + set_server_roles(&db, &command.user, ctx).await; + "Discord username linked to Wolves".to_string() + } Err(e) => { println!("{:?}", e); "Failed to save, please try /link again".to_string() } - } + }; } Err(e) => println!("{:?}", e), } @@ -323,4 +330,52 @@ pub(crate) mod verify { .fetch_optional(db) .await } + + async fn set_server_roles(db: &Pool, discord: &User, ctx: &Context) { + if let Ok(servers) = get_servers(db, &discord.name).await { + for server in servers { + if let Ok(mut member) = server.server.member(&ctx.http, &discord.id).await { + if let Some(config) = get_server_config(db, &server.server).await { + let Servers { + role_past, + role_current, + .. + } = config; + + let mut roles = vec![]; + + if let Some(role) = &role_past { + if !member.roles.contains(role) { + roles.push(role.to_owned()); + } + } + + if let Some(role) = &role_current { + if !member.roles.contains(role) { + roles.push(role.to_owned()); + } + } + + if let Err(e) = member.add_roles(&ctx, &roles).await { + println!("{:?}", e); + } + } + } + } + } + } + + async fn get_servers(db: &Pool, discord: &str) -> Result, Error> { + sqlx::query_as::<_, ServerMembersWolves>( + " + SELECT * + FROM server_members + JOIN wolves USING (id_wolves) + WHERE discord = ? + ", + ) + .bind(discord) + .fetch_all(db) + .await + } }