fix: role_current needednt have been an Option

This commit is contained in:
silver 2024-09-17 22:08:20 +01:00
parent e9aed40f41
commit 11240914ac
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
4 changed files with 22 additions and 33 deletions

View file

@ -39,7 +39,7 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
.as_ref() .as_ref()
.expect("Expected role object") .expect("Expected role object")
{ {
Some(role.id.to_owned()) role.id.to_owned()
} else { } else {
return "Please provide a valid role for ``Role Current``".to_string(); 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<Sqlite>, ctx: &Context, server: &Servers) -> Result<Option<Servers>, Error> { async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Result<Option<Servers>, Error> {
let existing = get_server_config(db, &server.server).await; let existing = get_server_config(db, &server.server).await;
let role_past = server.role_past.map(|x| *x.as_u64() as i64); 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>( let insert = sqlx::query_as::<_, Servers>(
" "
@ -186,7 +185,7 @@ async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Resul
.bind(*server.server.as_u64() as i64) .bind(*server.server.as_u64() as i64)
.bind(&server.wolves_api) .bind(&server.wolves_api)
.bind(role_past) .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.bot_channel_id.as_u64() as i64)
.bind(&server.server_name) .bind(&server.server_name)
.bind(&server.wolves_link) .bind(&server.wolves_link)
@ -204,7 +203,7 @@ async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Resul
if x.role_current != server.role_current { if x.role_current != server.role_current {
result.0 = true; result.0 = true;
result.1 = true; result.1 = true;
result.2 = x.role_current; result.2 = Some(x.role_current);
} }
if x.role_past != server.role_past { if x.role_past != server.role_past {
result.0 = true; result.0 = true;

View file

@ -352,10 +352,8 @@ pub mod verify {
} }
} }
if let Some(role) = &role_current { if !member.roles.contains(&role_current) {
if !member.roles.contains(role) { roles.push(role_current.to_owned());
roles.push(role.to_owned());
}
} }
if let Err(e) = member.add_roles(&ctx, &roles).await { if let Err(e) = member.add_roles(&ctx, &roles).await {

View file

@ -228,8 +228,7 @@ pub struct Servers {
pub server: GuildId, pub server: GuildId,
pub wolves_api: String, pub wolves_api: String,
pub role_past: Option<RoleId>, pub role_past: Option<RoleId>,
// TODO: this should not be option pub role_current: RoleId,
pub role_current: Option<RoleId>,
pub member_past: i64, pub member_past: i64,
pub member_current: i64, pub member_current: i64,
pub bot_channel_id: ChannelId, 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") { let role_current = match row.try_get("role_current") {
Ok(x) => { Ok(x) => {
let tmp: i64 = x; let tmp: i64 = x;
if tmp == 0 { RoleId::from(tmp as u64)
None
} else {
Some(RoleId::from(tmp as u64))
} }
} _ => RoleId::from(0u64),
_ => None,
}; };
let bot_channel_tmp: i64 = row.try_get("bot_channel_id")?; 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) { if !member.roles.contains(role_current) {
roles_set[1] += 1; roles_set[1] += 1;
roles.push(role.to_owned()); roles.push(role_current.to_owned());
}
} }
if let Err(e) = member.add_roles(ctx, &roles).await { if let Err(e) = member.add_roles(ctx, &roles).await {
println!("{:?}", e); println!("{:?}", e);
} }
@ -433,16 +428,15 @@ pub mod set_roles {
} }
} }
if let Some(role) = &role_current {
if member.roles.contains(role) { if member.roles.contains(role_current) {
roles_set[2] += 1; roles_set[2] += 1;
// if theya re not a current member and have the role then remove it // if theya re not a current member and have the role then remove it
if let Err(e) = member.remove_role(ctx, role).await { if let Err(e) = member.remove_role(ctx, role_current).await {
println!("{:?}", e); println!("{:?}", e);
} }
} }
} }
}
for role in remove_roles.iter().flatten() { for role in remove_roles.iter().flatten() {
if let Err(e) = member.remove_role(ctx, role).await { if let Err(e) = member.remove_role(ctx, role).await {
println!("{:?}", e); println!("{:?}", e);

View file

@ -42,10 +42,8 @@ impl EventHandler for Handler {
} }
} }
if let Some(role) = &config.role_current { if !new_member.roles.contains(&config.role_current) {
if !new_member.roles.contains(role) { roles.push(config.role_current.to_owned());
roles.push(role.to_owned());
}
} }
if let Err(e) = new_member.add_roles(&ctx, &roles).await { if let Err(e) = new_member.add_roles(&ctx, &roles).await {