diff --git a/db/migrations/9_member_committee-id.sql b/db/migrations/9_member_committee-id.sql new file mode 100644 index 0000000..77552ce --- /dev/null +++ b/db/migrations/9_member_committee-id.sql @@ -0,0 +1,6 @@ +-- No need for this col since it is goign to be in "committees" anyways +ALTER TABLE servers DROP COLUMN server_name; + +-- we do care about teh ID of the club/soc though +ALTER TABLE servers ADD COLUMN wolves_id integer DEFAULT 0; + diff --git a/src/commands/add_server.rs b/src/commands/add_server.rs index fdc0f23..5a9dbee 100644 --- a/src/commands/add_server.rs +++ b/src/commands/add_server.rs @@ -61,12 +61,12 @@ pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { let server_data = Servers { server: command.guild_id.unwrap_or_default(), wolves_api, + wolves_id: 0, role_past, role_current, member_past: 0, member_current: 0, bot_channel_id, - server_name: "".to_string(), }; match add_server(&db, ctx, &server_data).await { diff --git a/src/common/database.rs b/src/common/database.rs index 596b411..e4856fa 100644 --- a/src/common/database.rs +++ b/src/common/database.rs @@ -121,12 +121,12 @@ impl<'r> FromRow<'r, SqliteRow> for WolvesVerify { pub struct Servers { pub server: GuildId, pub wolves_api: String, + pub wolves_id: i64, pub role_past: Option, pub role_current: RoleId, pub member_past: i64, pub member_current: i64, pub bot_channel_id: ChannelId, - pub server_name: String, } impl<'r> FromRow<'r, SqliteRow> for Servers { @@ -158,12 +158,12 @@ impl<'r> FromRow<'r, SqliteRow> for Servers { Ok(Self { server, wolves_api: row.try_get("wolves_api")?, + wolves_id: row.try_get("wolves_id").unwrap_or(0), role_past, role_current, member_past: row.try_get("member_past")?, member_current: row.try_get("member_current")?, bot_channel_id, - server_name: row.try_get("server_name")?, }) } } diff --git a/src/common/wolves.rs b/src/common/wolves.rs index ea64699..6f73842 100644 --- a/src/common/wolves.rs +++ b/src/common/wolves.rs @@ -87,7 +87,7 @@ pub mod cns { server, // this is the unique api key for each club/soc wolves_api, - server_name, + wolves_id, .. } = &server_config; // dbg!(&server_config); @@ -101,7 +101,7 @@ pub mod cns { for user in wolves.get_members(wolves_api).await { // dbg!(&user.committee); if server_name_tmp.is_none() { - server_name_tmp = Some(user.committee.to_owned()); + server_name_tmp = Some(user.committee_id); } let id = user.member_id.parse::().unwrap_or_default(); match existing.get(&(id as i64)) { @@ -124,9 +124,9 @@ pub mod cns { } } - if let Some(name) = server_name_tmp { - if &name != server_name { - set_server_member(&db, server, &name).await; + if let Some(cs_id) = server_name_tmp { + if &cs_id != wolves_id { + set_server_member(&db, server, cs_id).await; } } if !user_to_update.is_empty() { @@ -135,15 +135,15 @@ pub mod cns { } } - async fn set_server_member(db: &Pool, server: &GuildId, name: &str) { + async fn set_server_member(db: &Pool, server: &GuildId, wolves_id: i64) { match sqlx::query_as::<_, Servers>( " UPDATE servers - SET server_name = ? + SET wolves_id = ? WHERE server = ? ", ) - .bind(name) + .bind(wolves_id) .bind(server.get() as i64) .fetch_optional(db) .await diff --git a/src/main.rs b/src/main.rs index 36316d1..5dab0f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,7 +68,7 @@ impl EventHandler for Handler { println!("{:?}", e); } } else { - let tmp = get_committee(&db, &config_server.server_name).await; + let tmp = get_committee(&db, config_server.wolves_id).await; if !tmp.is_empty() { let committee = &tmp[0]; let msg = format!( @@ -158,15 +158,15 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use } } -async fn get_committee(db: &Pool, committee: &str) -> Vec { +async fn get_committee(db: &Pool, wolves_id: i64) -> Vec { sqlx::query_as::<_, Committees>( r#" SELECT * FROM committees - WHERE name_plain = ? + WHERE id = ? "#, ) - .bind(committee) + .bind(wolves_id) .fetch_all(db) .await .unwrap_or_default()