discord-bot/src/bin/update_data.rs

125 lines
2.9 KiB
Rust
Raw Normal View History

use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Accounts, Config, Servers};
use serde::Deserialize;
use serenity::model::id::GuildId;
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
get_wolves(&db).await;
// get from skynet for the compsoc server only
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:03:56 +00:00
async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
let url = format!("{}/ldap/discord?auth={}", &config.ldap_api, &config.auth);
if let Ok(result) = surf::get(url).recv_json::<Vec<SkynetResult>>().await {
for user in result {
2023-09-11 19:03:56 +00:00
add_users_skynet(db, &config.skynet_server, &user).await;
}
}
}
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>(
"
UPDATE accounts
SET discord = ?
WHERE server = ? AND wolves_id = ?
",
2023-09-16 17:02:15 +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 = ?
",
)
.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);
}
}
}
}
#[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>) {
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
// get the data here
2023-09-11 19:03:56 +00:00
let result: Vec<WolvesResult> = vec![];
for user in result {
2023-09-11 19:03:56 +00:00
add_users_wolves(db, &server, &user).await;
}
}
}
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)
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
{
Ok(_) => {}
Err(e) => {
println!("Failure to insert into {} {:?}", server.as_u64(), user);
println!("{:?}", e);
}
}
2023-09-11 19:03:56 +00:00
}