2023-09-16 17:07:13 +00:00
|
|
|
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Accounts, Config, Servers};
|
2023-09-11 01:25:07 +00:00
|
|
|
|
|
|
|
use serde::Deserialize;
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
// handle wolves api here
|
2023-09-11 19:02:16 +00:00
|
|
|
get_wolves(&db).await;
|
2023-09-11 01:25:07 +00:00
|
|
|
|
|
|
|
// get from skynet for the compsoc server only
|
2023-09-11 19:02:16 +00:00
|
|
|
get_skynet(&db, &config).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct SkynetResult {
|
|
|
|
discord: String,
|
2023-09-16 17:02:15 +00:00
|
|
|
id_wolves: String,
|
|
|
|
id_member: String,
|
2023-09-11 19:02:16 +00:00
|
|
|
}
|
2023-09-11 19:03:56 +00:00
|
|
|
async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
|
2023-09-11 01:25:07 +00:00
|
|
|
let url = format!("{}/ldap/discord?auth={}", &config.ldap_api, &config.auth);
|
2023-09-11 19:02:16 +00:00
|
|
|
if let Ok(result) = surf::get(url).recv_json::<Vec<SkynetResult>>().await {
|
2023-09-11 01:25:07 +00:00
|
|
|
for user in result {
|
2023-09-11 19:03:56 +00:00
|
|
|
add_users_skynet(db, &config.skynet_server, &user).await;
|
2023-09-11 01:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 19:02:16 +00:00
|
|
|
async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) {
|
2023-09-16 17:02:15 +00:00
|
|
|
if !user.id_wolves.is_empty() {
|
|
|
|
match sqlx::query_as::<_, Accounts>(
|
|
|
|
"
|
2023-09-11 01:25:07 +00:00
|
|
|
UPDATE accounts
|
|
|
|
SET discord = ?
|
|
|
|
WHERE server = ? AND wolves_id = ?
|
|
|
|
",
|
2023-09-16 17:02:15 +00:00
|
|
|
)
|
2023-09-16 17:07:13 +00:00
|
|
|
.bind(&user.discord)
|
|
|
|
.bind(*server.as_u64() as i64)
|
|
|
|
.bind(&user.id_wolves)
|
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
2023-09-16 17:02:15 +00:00
|
|
|
{
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Failure to insert into {} {:?}", server.as_u64(), user);
|
|
|
|
println!("{:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !user.id_member.is_empty() {
|
|
|
|
match sqlx::query_as::<_, Accounts>(
|
|
|
|
"
|
|
|
|
UPDATE accounts
|
|
|
|
SET discord = ?
|
|
|
|
WHERE server = ? AND id_member = ?
|
|
|
|
",
|
|
|
|
)
|
2023-09-16 17:07:13 +00:00
|
|
|
.bind(&user.discord)
|
|
|
|
.bind(*server.as_u64() as i64)
|
|
|
|
.bind(&user.id_member)
|
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
2023-09-16 17:02:15 +00:00
|
|
|
{
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Failure to insert into {} {:?}", server.as_u64(), user);
|
|
|
|
println!("{:?}", e);
|
|
|
|
}
|
2023-09-11 01:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 19:02:16 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct WolvesResult {
|
|
|
|
pub id_wolves: String,
|
|
|
|
pub email: String,
|
|
|
|
pub expiry: String,
|
|
|
|
}
|
2023-09-11 19:03:56 +00:00
|
|
|
async fn get_wolves(db: &Pool<Sqlite>) {
|
2023-09-11 19:02:16 +00:00
|
|
|
for server_config in get_server_config_bulk(db).await {
|
|
|
|
let Servers {
|
|
|
|
server,
|
|
|
|
wolves_api,
|
|
|
|
..
|
|
|
|
} = server_config;
|
2023-09-11 19:03:56 +00:00
|
|
|
|
2023-09-11 19:02:16 +00:00
|
|
|
// get the data here
|
2023-09-11 19:03:56 +00:00
|
|
|
let result: Vec<WolvesResult> = vec![];
|
2023-09-11 19:02:16 +00:00
|
|
|
|
|
|
|
for user in result {
|
2023-09-11 19:03:56 +00:00
|
|
|
add_users_wolves(db, &server, &user).await;
|
2023-09-11 19:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResult) {
|
|
|
|
match sqlx::query_as::<_, Accounts>(
|
2023-09-11 19:03:56 +00:00
|
|
|
"
|
2023-09-16 17:02:15 +00:00
|
|
|
INSERT OR REPLACE INTO accounts (server, id_wolves, email, expiry)
|
2023-09-11 19:02:16 +00:00
|
|
|
VALUES (?1, ?2, ?3, ?4)
|
|
|
|
",
|
|
|
|
)
|
2023-09-11 19:03:56 +00:00
|
|
|
.bind(*server.as_u64() as i64)
|
|
|
|
.bind(&user.id_wolves)
|
|
|
|
.bind(&user.email)
|
|
|
|
.bind(&user.expiry)
|
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
2023-09-11 19:02:16 +00:00
|
|
|
{
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Failure to insert into {} {:?}", server.as_u64(), user);
|
|
|
|
println!("{:?}", e);
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 19:03:56 +00:00
|
|
|
}
|