From 11240914ac770a17b133768cbc9e8c4741983fea Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Tue, 17 Sep 2024 22:08:20 +0100 Subject: [PATCH] fix: role_current needednt have been an Option --- src/commands/add_server.rs | 7 +++---- src/commands/link_email.rs | 6 ++---- src/lib.rs | 36 +++++++++++++++--------------------- src/main.rs | 6 ++---- 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/commands/add_server.rs b/src/commands/add_server.rs index a904875..0d5d03b 100644 --- a/src/commands/add_server.rs +++ b/src/commands/add_server.rs @@ -39,7 +39,7 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri .as_ref() .expect("Expected role object") { - Some(role.id.to_owned()) + role.id.to_owned() } else { return "Please provide a valid role for ``Role Current``".to_string(); }; @@ -175,7 +175,6 @@ pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicatio async fn add_server(db: &Pool, ctx: &Context, server: &Servers) -> Result, Error> { let existing = get_server_config(db, &server.server).await; let role_past = server.role_past.map(|x| *x.as_u64() as i64); - let role_current = server.role_current.map(|x| *x.as_u64() as i64); let insert = sqlx::query_as::<_, Servers>( " @@ -186,7 +185,7 @@ async fn add_server(db: &Pool, ctx: &Context, server: &Servers) -> Resul .bind(*server.server.as_u64() as i64) .bind(&server.wolves_api) .bind(role_past) - .bind(role_current) + .bind(*server.role_current.as_u64() as i64) .bind(*server.bot_channel_id.as_u64() as i64) .bind(&server.server_name) .bind(&server.wolves_link) @@ -204,7 +203,7 @@ async fn add_server(db: &Pool, ctx: &Context, server: &Servers) -> Resul if x.role_current != server.role_current { result.0 = true; result.1 = true; - result.2 = x.role_current; + result.2 = Some(x.role_current); } if x.role_past != server.role_past { result.0 = true; diff --git a/src/commands/link_email.rs b/src/commands/link_email.rs index 5a0fd26..2a0d744 100644 --- a/src/commands/link_email.rs +++ b/src/commands/link_email.rs @@ -352,10 +352,8 @@ pub mod verify { } } - if let Some(role) = &role_current { - if !member.roles.contains(role) { - roles.push(role.to_owned()); - } + if !member.roles.contains(&role_current) { + roles.push(role_current.to_owned()); } if let Err(e) = member.add_roles(&ctx, &roles).await { diff --git a/src/lib.rs b/src/lib.rs index 1b2520f..466ab41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -228,8 +228,7 @@ pub struct Servers { pub server: GuildId, pub wolves_api: String, pub role_past: Option, - // TODO: this should not be option - pub role_current: Option, + pub role_current: RoleId, pub member_past: i64, pub member_current: i64, pub bot_channel_id: ChannelId, @@ -255,13 +254,9 @@ impl<'r> FromRow<'r, SqliteRow> for Servers { let role_current = match row.try_get("role_current") { Ok(x) => { let tmp: i64 = x; - if tmp == 0 { - None - } else { - Some(RoleId::from(tmp as u64)) - } - } - _ => None, + RoleId::from(tmp as u64) + } + _ => RoleId::from(0u64), }; let bot_channel_tmp: i64 = row.try_get("bot_channel_id")?; @@ -414,13 +409,13 @@ pub mod set_roles { } } - if let Some(role) = &role_current { - if !member.roles.contains(role) { - roles_set[1] += 1; - roles.push(role.to_owned()); - } + + if !member.roles.contains(role_current) { + roles_set[1] += 1; + roles.push(role_current.to_owned()); } + if let Err(e) = member.add_roles(ctx, &roles).await { println!("{:?}", e); } @@ -433,13 +428,12 @@ pub mod set_roles { } } - if let Some(role) = &role_current { - if member.roles.contains(role) { - roles_set[2] += 1; - // if theya re not a current member and have the role then remove it - if let Err(e) = member.remove_role(ctx, role).await { - println!("{:?}", e); - } + + if member.roles.contains(role_current) { + roles_set[2] += 1; + // if theya re not a current member and have the role then remove it + if let Err(e) = member.remove_role(ctx, role_current).await { + println!("{:?}", e); } } } diff --git a/src/main.rs b/src/main.rs index ee0d7fc..064b780 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,10 +42,8 @@ impl EventHandler for Handler { } } - if let Some(role) = &config.role_current { - if !new_member.roles.contains(role) { - roles.push(role.to_owned()); - } + if !new_member.roles.contains(&config.role_current) { + roles.push(config.role_current.to_owned()); } if let Err(e) = new_member.add_roles(&ctx, &roles).await {