diff --git a/Cargo.lock b/Cargo.lock index 23bef46..a1c92c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1298,7 +1298,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2 0.5.7", + "socket2 0.4.10", "tokio", "tower-service", "tracing", @@ -2628,6 +2628,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "2.2.0" @@ -2648,6 +2657,7 @@ dependencies = [ "maud", "rand 0.8.5", "serde", + "serde_json", "serenity", "sqlx", "surf", @@ -3242,7 +3252,9 @@ dependencies = [ "bytes 1.7.1", "libc", "mio", + "parking_lot", "pin-project-lite", + "signal-hook-registry", "socket2 0.5.7", "tokio-macros", "windows-sys 0.52.0", @@ -3917,7 +3929,7 @@ dependencies = [ [[package]] name = "wolves_oxidised" version = "0.1.0" -source = "git+https://forgejo.skynet.ie/Skynet/wolves-oxidised.git#867778128c1ef580ebfded7808bbd4e86f22903b" +source = "git+https://forgejo.skynet.ie/Skynet/wolves-oxidised.git#6101104c794c2dcf7100b057fe37fdf165b8b381" dependencies = [ "reqwest 0.12.9", "serde", diff --git a/Cargo.toml b/Cargo.toml index 19cb9ae..fa0ffdc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,11 +20,8 @@ serenity = { version = "0.11.6", default-features = false, features = ["client", tokio = { version = "1.0", features = ["macros", "rt-multi-thread", "full"] } # wolves api -# TODO: move off of unstable -wolves_oxidised = { git = "https://forgejo.skynet.ie/Skynet/wolves-oxidised.git", features = ["unstable"]} - -# wolves api -wolves_oxidised = { git = "https://forgejo.skynet.ie/Skynet/wolves-oxidised.git" } +wolves_oxidised = { git = "https://forgejo.skynet.ie/Skynet/wolves-oxidised.git", features = ["unstable"] } +# wolves_oxidised = { path = "../wolves-oxidised", features = ["unstable"] } # to make the http requests surf = "2.3.2" diff --git a/db/migrations/7_committee-mk-ii.sql b/db/migrations/7_committee-mk-ii.sql deleted file mode 100644 index c086f3d..0000000 --- a/db/migrations/7_committee-mk-ii.sql +++ /dev/null @@ -1,10 +0,0 @@ --- No longer using the previous committee table -DROP TABLE committee; - --- new table pulling from teh api -CREATE TABLE IF NOT EXISTS committees ( - id integer PRIMARY KEY, - name text not null, - link text not null, - committee text not null -); diff --git a/db/migrations/8_committee-mk-ii.sql b/db/migrations/8_committee-mk-ii.sql new file mode 100644 index 0000000..d2e1d7c --- /dev/null +++ b/db/migrations/8_committee-mk-ii.sql @@ -0,0 +1,11 @@ +-- No longer using the previous committee table +DROP TABLE committee; + +-- new table pulling from teh api +CREATE TABLE IF NOT EXISTS committees ( + id integer PRIMARY KEY, + name_profile text not null, + name_full text not null, + link text not null, + committee text not null +); diff --git a/src/commands/link_email.rs b/src/commands/link_email.rs index fdf82d4..a76769f 100644 --- a/src/commands/link_email.rs +++ b/src/commands/link_email.rs @@ -263,7 +263,6 @@ pub mod link { .await } - #[derive(Serialize, Deserialize, Debug)] #[serde(untagged)] pub enum WolvesResultUserResult { diff --git a/src/commands/minecraft.rs b/src/commands/minecraft.rs index 45c4643..10ee284 100644 --- a/src/commands/minecraft.rs +++ b/src/commands/minecraft.rs @@ -18,8 +18,8 @@ pub(crate) mod user { use serde::{Deserialize, Serialize}; use serenity::model::id::UserId; use skynet_discord_bot::common::database::Wolves; - use skynet_discord_bot::common::minecraft::{whitelist_update, Minecraft}; - use skynet_discord_bot::Config; + use skynet_discord_bot::common::minecraft::Minecraft; + use skynet_discord_bot::{whitelist_update, Config}; use sqlx::Error; pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { diff --git a/src/common/database.rs b/src/common/database.rs index b3edc18..5139cb5 100644 --- a/src/common/database.rs +++ b/src/common/database.rs @@ -42,6 +42,7 @@ pub struct ServerMembersWolves { pub email: String, pub discord: Option, pub minecraft: Option, + pub minecraft_uid: Option, } impl<'r> FromRow<'r, SqliteRow> for ServerMembersWolves { @@ -67,6 +68,7 @@ impl<'r> FromRow<'r, SqliteRow> for ServerMembersWolves { email: row.try_get("email")?, discord, minecraft: row.try_get("minecraft")?, + minecraft_uid: row.try_get("minecraft_uid")?, }) } } diff --git a/src/common/minecraft.rs b/src/common/minecraft.rs index 6ee11c8..000aa46 100644 --- a/src/common/minecraft.rs +++ b/src/common/minecraft.rs @@ -1,5 +1,5 @@ use crate::common::set_roles::normal::get_server_member_bulk; -use crate::Config; +use crate::{whitelist_update, Config}; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use serenity::model::id::GuildId; @@ -33,7 +33,10 @@ pub async fn update_server(server_id: &str, db: &Pool, g_id: &GuildId, c let mut usernames = vec![]; for member in get_server_member_bulk(db, g_id).await { if let Some(x) = member.minecraft { - usernames.push(x); + usernames.push((x, true)); + } + if let Some(x) = member.minecraft_uid { + usernames.push((x, false)); } } if !usernames.is_empty() { @@ -98,18 +101,6 @@ struct BodyDelete { files: Vec, } -pub async fn whitelist_update(add: &Vec, server: &str, token: &str) { - let url_base = format!("http://panel.games.skynet.ie/api/client/servers/{server}"); - let bearer = format!("Bearer {token}"); - - for name in add { - let data = BodyCommand { - command: format!("whitelist add {name}"), - }; - post(&format!("{url_base}/command"), &bearer, &data).await; - } -} - pub async fn whitelist_wipe(server: &str, token: &str) { let url_base = format!("http://panel.games.skynet.ie/api/client/servers/{server}"); let bearer = format!("Bearer {token}"); diff --git a/src/common/set_roles.rs b/src/common/set_roles.rs index 9b26117..912d661 100644 --- a/src/common/set_roles.rs +++ b/src/common/set_roles.rs @@ -199,11 +199,11 @@ pub mod committee { for committee in &committees { // get the role for this committee/club/soc - let role = match roles_name.get(&committee.name) { + let role = match roles_name.get(&committee.name_profile) { Some(x) => Some(x.to_owned()), None => { // create teh role if it does not exist - match server.create_role(&ctx, |r| r.hoist(false).mentionable(true).name(&committee.name)).await { + match server.create_role(&ctx, |r| r.hoist(false).mentionable(true).name(&committee.name_profile)).await { Ok(x) => Some(x), Err(_) => None, } @@ -211,16 +211,16 @@ pub mod committee { }; // create teh channel if it does nto exist - if !channels_name.contains_key(&committee.name) { + if !channels_name.contains_key(&committee.name_profile) { match server - .create_channel(&ctx, |c| c.name(&committee.name).kind(ChannelType::Text).category(config.committee_category)) + .create_channel(&ctx, |c| c.name(&committee.name_profile).kind(ChannelType::Text).category(config.committee_category)) .await { Ok(x) => { // update teh channels name list channels_name.insert(x.name.to_owned(), x.to_owned()); - println!("Created channel: {}", &committee.name); + println!("Created channel: {}", &committee.name_profile); } Err(x) => { dbg!("Unable to create channel: ", x); diff --git a/src/common/wolves.rs b/src/common/wolves.rs index 0f975e3..b2c6af1 100644 --- a/src/common/wolves.rs +++ b/src/common/wolves.rs @@ -176,7 +176,8 @@ pub mod committees { #[derive(Debug, Clone, sqlx::FromRow)] pub struct Committees { pub id: i64, - pub name: String, + pub name_full: String, + pub name_profile: String, pub link: String, #[sqlx(json)] pub committee: Vec, @@ -185,10 +186,11 @@ pub mod committees { impl From for Committees { fn from(value: wolves_oxidised::WolvesCNS) -> Self { Self { - id: value.id.parse().unwrap_or(0), - name: value.name, + id: value.id, + name_full: value.name_full, + name_profile: value.name_profile, link: value.link, - committee: value.committee.iter().map(|x| x.parse::().unwrap_or(0)).collect(), + committee: value.committee, } } } @@ -217,13 +219,14 @@ pub mod committees { async fn add_committee(db: &Pool, committee: &Committees) { match sqlx::query_as::<_, Committees>( " - INSERT INTO committees (id, name, link, committee) - VALUES ($1, $2, $3, $4) + INSERT INTO committees (id, name_profile, name_full, link, committee) + VALUES ($1, $2, $3, $4, $5) ON CONFLICT(id) DO UPDATE SET committee = $4 ", ) .bind(committee.id) - .bind(&committee.name) + .bind(&committee.name_profile) + .bind(&committee.name_full) .bind(&committee.link) .bind(serde_json::to_string(&committee.committee).unwrap_or_default()) .fetch_optional(db) diff --git a/src/lib.rs b/src/lib.rs index 1b5000d..5b8d073 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,19 @@ pub mod common; +use crate::common::set_roles::normal::get_server_member_bulk; use chrono::{Datelike, SecondsFormat, Utc}; use dotenvy::dotenv; use rand::{distributions::Alphanumeric, thread_rng, Rng}; +use serde::de::DeserializeOwned; +use serde::{Deserialize, Serialize}; use serenity::client::Context; -use serenity::model::id::{ChannelId, GuildId, RoleId}; +use serenity::model::guild; +use serenity::model::id::{ChannelId, GuildId, RoleId, UserId}; use serenity::model::prelude::application_command::ApplicationCommandInteraction; use serenity::prelude::TypeMapKey; +use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions, SqliteRow}; +use sqlx::{Error, FromRow, Pool, Row, Sqlite}; +use std::str::FromStr; use std::{env, sync::Arc}; use tokio::sync::RwLock; @@ -56,6 +63,7 @@ pub fn get_config() -> Config { mail_user: "".to_string(), mail_pass: "".to_string(), wolves_url: "".to_string(), + wolves_api: "".to_string(), committee_server: GuildId(0), committee_role: RoleId(0), committee_category: ChannelId(0), @@ -418,6 +426,7 @@ pub fn random_string(len: usize) -> String { pub mod set_roles { use super::*; + use crate::common::database::DataBase; pub async fn update_server(ctx: &Context, server: &Servers, remove_roles: &[Option], members_changed: &[UserId]) { let db_lock = { let data_read = ctx.data.read().await; @@ -544,6 +553,7 @@ pub mod set_roles { pub mod get_data { use super::*; + use crate::common::database::DataBase; use crate::set_roles::update_server; use std::collections::BTreeMap; use wolves_oxidised::WolvesUser;