discord-bot/src/bin/update_data.rs

242 lines
5.6 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,
};
2023-09-16 18:47:39 +00:00
// handle wolves api here
get_wolves_csv(&db, &config).await;
// handle wolves api here
get_wolves(&db).await;
// get from skynet for the compsoc server only
get_skynet(&db, &config).await;
}
2023-09-16 18:47:39 +00:00
async fn get_wolves_csv(db: &Pool<Sqlite>, config: &Config) {
if let Ok(accounts) = get_csv(config) {
for account in accounts {
add_users_wolves_csv(db, &config.skynet_server, &account).await;
}
}
}
#[derive(Debug, serde::Deserialize)]
struct RecordCSV {
#[serde(rename = "MemID")]
mem_id: String,
#[serde(rename = "Contact Email")]
email: String,
#[serde(rename = "Expiry")]
expiry: String,
}
impl From<RecordCSV> for Accounts {
fn from(input: RecordCSV) -> Self {
Self {
server: Default::default(),
id_wolves: "".to_string(),
id_member: input.mem_id,
email: input.email,
expiry: input.expiry,
discord: None,
minecraft: None,
}
}
}
fn get_csv(config: &Config) -> Result<Vec<Accounts>, Box<dyn std::error::Error>> {
let mut records: Vec<Accounts> = vec![];
let csv = format!("{}/{}", &config.home, &config.csv);
if let Ok(mut rdr) = csv::Reader::from_path(csv) {
for result in rdr.deserialize() {
// Notice that we need to provide a type hint for automatic
// deserialization.
let record: RecordCSV = result?;
if record.mem_id.is_empty() {
continue;
}
records.push(Accounts::from(record));
}
}
Ok(records)
}
async fn add_users_wolves_csv(db: &Pool<Sqlite>, server: &GuildId, user: &Accounts) {
let existing = match sqlx::query_as::<_, Accounts>(
r#"
SELECT *
FROM accounts
WHERE server = ? AND id_member = ?
"#,
)
.bind(*server.as_u64() as i64)
.bind(&user.id_member)
.fetch_one(db)
.await
{
Ok(acc) => acc.id_wolves,
Err(_) => String::new(),
};
match sqlx::query_as::<_, Accounts>(
"
INSERT OR REPLACE INTO accounts (server, id_wolves, id_member, email, expiry)
VALUES (?1, ?2, ?3, ?4, ?5)
",
)
.bind(*server.as_u64() as i64)
.bind(&existing)
.bind(&user.id_member)
.bind(&user.email)
.bind(&user.expiry)
.fetch_optional(db)
.await
{
Ok(_) => {}
Err(e) => {
println!("Failure to insert into {} {:?}", server.as_u64(), user);
println!("{:?}", e);
}
}
}
#[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 = ?
2023-09-16 18:47:39 +00:00
WHERE server = ? AND id_wolves = ?
",
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,
2023-09-16 18:47:39 +00:00
pub id_member: 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,
2023-09-16 18:47:39 +00:00
//wolves_api,
..
} = server_config;
2023-09-11 19:03:56 +00:00
// get the data here
2023-09-16 18:47:39 +00:00
let result: Vec<WolvesResult> = vec![WolvesResult {
id_wolves: "12345".to_string(),
id_member: "166425".to_string(),
email: "ul.wolves2@brendan.ie".to_string(),
expiry: "2024-08-31".to_string(),
}];
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 18:47:39 +00:00
UPDATE accounts
SET id_wolves = ?
WHERE server = ? AND id_member = ?
",
)
.bind(&user.id_wolves)
.bind(*server.as_u64() as i64)
.bind(&user.id_member)
.fetch_optional(db)
.await
{
Ok(_) => {}
Err(e) => {
println!("Failure to update into {} {:?}", server.as_u64(), user);
println!("{:?}", e);
}
}
match sqlx::query_as::<_, Accounts>(
"
INSERT OR REPLACE INTO accounts (server, id_wolves, id_member, email, expiry)
VALUES (?1, ?2, ?3, ?4, ?5)
",
)
2023-09-11 19:03:56 +00:00
.bind(*server.as_u64() as i64)
.bind(&user.id_wolves)
2023-09-16 18:47:39 +00:00
.bind(&user.id_member)
2023-09-11 19:03:56 +00:00
.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
}