2023-09-16 23:14:50 +00:00
|
|
|
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Config, ServerMembers, Servers, Wolves};
|
2023-09-11 01:25:07 +00:00
|
|
|
|
2023-10-27 00:39:03 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-09-16 17:07:13 +00:00
|
|
|
use serenity::model::id::GuildId;
|
2023-09-11 01:25:07 +00:00
|
|
|
use sqlx::{Pool, Sqlite};
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let config = get_config();
|
|
|
|
let db = match db_init(&config).await {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
2023-09-16 18:47:39 +00:00
|
|
|
// handle wolves api here
|
2023-10-27 00:39:03 +00:00
|
|
|
get_wolves(&db, &config).await;
|
2023-09-16 18:47:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 00:39:03 +00:00
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
struct WolvesResultUser {
|
|
|
|
committee: String,
|
|
|
|
wolves_id: String,
|
|
|
|
first_name: String,
|
|
|
|
last_name: String,
|
2023-10-27 10:53:54 +00:00
|
|
|
contact_email: String,
|
2023-10-27 00:39:03 +00:00
|
|
|
student_id: Option<String>,
|
|
|
|
note: Option<String>,
|
2023-09-16 18:47:39 +00:00
|
|
|
expiry: String,
|
2023-10-27 00:39:03 +00:00
|
|
|
requested: String,
|
|
|
|
approved: String,
|
|
|
|
sitename: String,
|
|
|
|
domain: String,
|
2023-09-16 18:47:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 00:39:03 +00:00
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
struct WolvesResult {
|
|
|
|
success: i8,
|
|
|
|
result: Vec<WolvesResultUser>,
|
2023-09-11 19:02:16 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 00:39:03 +00:00
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
struct WolvesResultLocal {
|
2023-09-11 19:02:16 +00:00
|
|
|
pub id_wolves: String,
|
|
|
|
pub email: String,
|
|
|
|
pub expiry: String,
|
|
|
|
}
|
2023-10-27 00:39:03 +00:00
|
|
|
async fn get_wolves(db: &Pool<Sqlite>, config: &Config) {
|
2023-09-11 19:02:16 +00:00
|
|
|
for server_config in get_server_config_bulk(db).await {
|
|
|
|
let Servers {
|
|
|
|
server,
|
2023-10-27 00:39:03 +00:00
|
|
|
wolves_api,
|
2023-09-11 19:02:16 +00:00
|
|
|
..
|
|
|
|
} = server_config;
|
2023-09-11 19:03:56 +00:00
|
|
|
|
2023-10-27 00:39:03 +00:00
|
|
|
for user in get_wolves_sub(config, &wolves_api).await {
|
2023-09-11 19:03:56 +00:00
|
|
|
add_users_wolves(db, &server, &user).await;
|
2023-09-11 19:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 00:39:03 +00:00
|
|
|
async fn get_wolves_sub(config: &Config, wolves_api: &str) -> Vec<WolvesResultUser> {
|
|
|
|
if config.wolves_url.is_empty() {
|
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get wolves data
|
|
|
|
if let Ok(mut res) = surf::post(&config.wolves_url).header("X-AM-Identity", wolves_api).await {
|
|
|
|
if let Ok(WolvesResult {
|
|
|
|
success,
|
|
|
|
result,
|
|
|
|
}) = res.body_json().await
|
|
|
|
{
|
|
|
|
if success != 1 {
|
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResultUser) {
|
2023-09-16 23:14:50 +00:00
|
|
|
// expiry
|
|
|
|
match sqlx::query_as::<_, Wolves>(
|
2023-09-11 19:03:56 +00:00
|
|
|
"
|
2023-09-17 23:03:25 +00:00
|
|
|
INSERT INTO wolves (id_wolves, email)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
2023-09-16 18:47:39 +00:00
|
|
|
",
|
|
|
|
)
|
2023-10-27 00:39:03 +00:00
|
|
|
.bind(&user.wolves_id)
|
2023-10-27 10:53:54 +00:00
|
|
|
.bind(&user.contact_email)
|
2023-09-16 18:47:39 +00:00
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
2023-09-17 17:09:02 +00:00
|
|
|
println!("Failure to insert into Wolves {:?}", user);
|
2023-09-16 18:47:39 +00:00
|
|
|
println!("{:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 23:14:50 +00:00
|
|
|
match sqlx::query_as::<_, ServerMembers>(
|
2023-09-16 18:47:39 +00:00
|
|
|
"
|
2023-09-16 23:14:50 +00:00
|
|
|
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
|
|
|
VALUES (?1, ?2, ?3)
|
2023-09-11 19:02:16 +00:00
|
|
|
",
|
|
|
|
)
|
2023-09-11 19:03:56 +00:00
|
|
|
.bind(*server.as_u64() as i64)
|
2023-10-27 00:39:03 +00:00
|
|
|
.bind(&user.wolves_id)
|
2023-09-11 19:03:56 +00:00
|
|
|
.bind(&user.expiry)
|
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
2023-09-11 19:02:16 +00:00
|
|
|
{
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
2023-09-16 23:14:50 +00:00
|
|
|
println!("Failure to insert into ServerMembers {} {:?}", server.as_u64(), user);
|
2023-09-11 19:02:16 +00:00
|
|
|
println!("{:?}", e);
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 19:03:56 +00:00
|
|
|
}
|