diff --git a/src/bin/update_data.rs b/src/bin/update_data.rs index 1e0bdb1..471e323 100644 --- a/src/bin/update_data.rs +++ b/src/bin/update_data.rs @@ -1,16 +1,17 @@ use skynet_discord_bot::{db_init, get_config, Accounts, Config, DataBase}; -use std::process; +use std::{process, sync::Arc}; use serde::Deserialize; -use serenity::model::id::GuildId; use serenity::{ async_trait, client::{Context, EventHandler}, - model::gateway::{GatewayIntents, Ready}, + model::{ + gateway::{GatewayIntents, Ready}, + id::GuildId, + }, Client, }; use sqlx::{Pool, Sqlite}; -use std::sync::Arc; use tokio::sync::RwLock; diff --git a/src/bin/update_users.rs b/src/bin/update_users.rs index 464a96b..f44ad44 100644 --- a/src/bin/update_users.rs +++ b/src/bin/update_users.rs @@ -1,17 +1,15 @@ -use serenity::model::id::GuildId; use serenity::{ async_trait, client::{Context, EventHandler}, model::{ gateway::{GatewayIntents, Ready}, - prelude::RoleId, + id::GuildId, }, Client, }; -use skynet_discord_bot::{db_init, get_config, Accounts, Config, DataBase, Servers}; +use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Accounts, Config, DataBase, Servers}; use sqlx::{Pool, Sqlite}; -use std::process; -use std::sync::Arc; +use std::{process, sync::Arc}; use tokio::sync::RwLock; #[tokio::main] @@ -75,7 +73,6 @@ async fn bulk_check(ctx: Arc) { let mut roles_set = [0, 0, 0]; let mut members = vec![]; - let server = GuildId::from(server as u64); for member in get_server_member_bulk(&db, &server).await { if let Some(x) = member.discord { members.push(x); @@ -89,16 +86,14 @@ async fn bulk_check(ctx: Arc) { let mut roles = vec![]; if let Some(role) = &role_past { - let role = RoleId::from(*role as u64); - if !member.roles.contains(&role) { + if !member.roles.contains(role) { roles_set[0] += 1; roles.push(role.to_owned()); } } if let Some(role) = &role_current { - let role = RoleId::from(*role as u64); - if !member.roles.contains(&role) { + if !member.roles.contains(role) { roles_set[1] += 1; roles.push(role.to_owned()); } @@ -111,15 +106,13 @@ async fn bulk_check(ctx: Arc) { // old and never if let Some(role) = &role_past { - let role = RoleId::from(*role as u64); - if member.roles.contains(&role) { + if member.roles.contains(role) { members_all += 1; } } if let Some(role) = &role_current { - let role = RoleId::from(*role as u64); - if member.roles.contains(&role) { + 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 { @@ -137,17 +130,6 @@ async fn bulk_check(ctx: Arc) { println!("{:?} Changes: New: +{}, Current: +{}/-{}", server.as_u64(), roles_set[0], roles_set[1], roles_set[2]); } } -async fn get_server_config_bulk(db: &Pool) -> Vec { - sqlx::query_as::<_, Servers>( - r#" - SELECT * - FROM servers - "#, - ) - .fetch_all(db) - .await - .unwrap_or_default() -} async fn get_server_member_bulk(db: &Pool, server: &GuildId) -> Vec { sqlx::query_as::<_, Accounts>( diff --git a/src/lib.rs b/src/lib.rs index 254c2c2..daa34e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,18 @@ use dotenvy::dotenv; use serde::{Deserialize, Serialize}; use serenity::{ - model::{guild, id::GuildId}, + model::{ + guild, + id::{GuildId, RoleId}, + }, prelude::TypeMapKey, }; + use sqlx::{ - sqlite::{SqliteConnectOptions, SqlitePoolOptions}, - Error, Pool, Sqlite, + sqlite::{SqliteConnectOptions, SqlitePoolOptions, SqliteRow}, + Error, FromRow, Pool, Row, Sqlite, }; -use std::str::FromStr; -use std::{env, sync::Arc}; +use std::{env, str::FromStr, sync::Arc}; use tokio::sync::RwLock; pub struct Config { @@ -99,9 +102,9 @@ fn str_to_num(x: &str) -> T { x.trim().parse::().unwrap_or_default() } -#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct Accounts { - pub server: i64, + pub server: GuildId, pub wolves_id: String, pub email: String, pub expiry: String, @@ -109,15 +112,60 @@ pub struct Accounts { pub minecraft: Option, } -#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)] +impl<'r> FromRow<'r, SqliteRow> for Accounts { + fn from_row(row: &'r SqliteRow) -> Result { + let server_tmp: i64 = row.try_get("server")?; + let server = GuildId::from(server_tmp as u64); + + Ok(Self { + server, + wolves_id: row.try_get("wolves_api")?, + email: row.try_get("email")?, + expiry: row.try_get("expiry")?, + discord: row.try_get("discord")?, + minecraft: row.try_get("minecraft")?, + }) + } +} + +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct Servers { - pub server: i64, + pub server: GuildId, pub wolves_api: String, - pub role_past: Option, - pub role_current: Option, + pub role_past: Option, + pub role_current: Option, pub member_past: i64, pub member_current: i64, } +impl<'r> FromRow<'r, SqliteRow> for Servers { + fn from_row(row: &'r SqliteRow) -> Result { + let server_tmp: i64 = row.try_get("server")?; + let server = GuildId::from(server_tmp as u64); + let role_past = match row.try_get("role_past") { + Ok(x) => { + let tmp: i64 = x; + Some(RoleId::from(tmp as u64)) + } + _ => None, + }; + let role_current = match row.try_get("role_current") { + Ok(x) => { + let tmp: i64 = x; + Some(RoleId::from(tmp as u64)) + } + _ => None, + }; + + Ok(Self { + server, + wolves_api: row.try_get("wolves_api")?, + role_past, + role_current, + member_past: row.try_get("member_past")?, + member_current: row.try_get("member_current")?, + }) + } +} pub async fn db_init(config: &Config) -> Result, Error> { let database = format!("{}/{}", &config.home, &config.database); @@ -190,3 +238,15 @@ pub async fn get_server_member(db: &Pool, server: &GuildId, member: &gui .await .ok() } + +pub async fn get_server_config_bulk(db: &Pool) -> Vec { + sqlx::query_as::<_, Servers>( + r#" + SELECT * + FROM servers + "#, + ) + .fetch_all(db) + .await + .unwrap_or_default() +} diff --git a/src/main.rs b/src/main.rs index 23b647c..63431be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ use serenity::{ model::{ gateway::{GatewayIntents, Ready}, guild, - prelude::RoleId, }, Client, }; @@ -33,15 +32,13 @@ impl EventHandler for Handler { let mut roles = vec![]; if let Some(role) = &config.role_past { - let role = RoleId::from(*role as u64); - if !new_member.roles.contains(&role) { + if !new_member.roles.contains(role) { roles.push(role.to_owned()); } } if let Some(role) = &config.role_current { - let role = RoleId::from(*role as u64); - if !new_member.roles.contains(&role) { + if !new_member.roles.contains(role) { roles.push(role.to_owned()); } }