forked from Skynet/discord-bot
227 lines
5.2 KiB
Rust
227 lines
5.2 KiB
Rust
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Config, ServerMembers, Servers, Wolves};
|
|
|
|
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_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;
|
|
}
|
|
|
|
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 Wolves {
|
|
fn from(input: &RecordCSV) -> Self {
|
|
Self {
|
|
id_wolves: input.mem_id.to_owned(),
|
|
email: input.email.to_owned(),
|
|
discord: None,
|
|
minecraft: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<&RecordCSV> for ServerMembers {
|
|
fn from(input: &RecordCSV) -> Self {
|
|
Self {
|
|
server: Default::default(),
|
|
id_wolves: input.mem_id.to_owned(),
|
|
expiry: input.expiry.to_owned(),
|
|
}
|
|
}
|
|
}
|
|
|
|
struct Csv {
|
|
wolves: Wolves,
|
|
server_members: ServerMembers,
|
|
}
|
|
|
|
fn get_csv(config: &Config) -> Result<Vec<Csv>, Box<dyn std::error::Error>> {
|
|
let mut result = vec![];
|
|
|
|
let csv = format!("{}/{}", &config.home, &config.csv);
|
|
if let Ok(mut rdr) = csv::Reader::from_path(csv) {
|
|
for r in rdr.deserialize() {
|
|
// Notice that we need to provide a type hint for automatic
|
|
// deserialization.
|
|
let record: RecordCSV = r?;
|
|
if record.mem_id.is_empty() {
|
|
continue;
|
|
}
|
|
|
|
result.push(Csv {
|
|
wolves: Wolves::from(&record),
|
|
server_members: ServerMembers::from(&record),
|
|
});
|
|
}
|
|
}
|
|
|
|
Ok(result)
|
|
}
|
|
async fn add_users_wolves_csv(db: &Pool<Sqlite>, server: &GuildId, user: &Csv) {
|
|
match sqlx::query_as::<_, Wolves>(
|
|
"
|
|
INSERT OR REPLACE INTO wolves (id_wolves, email)
|
|
VALUES (?1, ?2)
|
|
",
|
|
)
|
|
.bind(&user.wolves.id_wolves)
|
|
.bind(&user.wolves.email)
|
|
.fetch_optional(db)
|
|
.await
|
|
{
|
|
Ok(_) => {}
|
|
Err(e) => {
|
|
println!("Failure to insert into {} {:?}", server.as_u64(), user.wolves);
|
|
println!("{:?}", e);
|
|
}
|
|
}
|
|
|
|
match sqlx::query_as::<_, ServerMembers>(
|
|
"
|
|
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
|
VALUES (?1, ?2, ?3)
|
|
",
|
|
)
|
|
.bind(*server.as_u64() as i64)
|
|
.bind(&user.server_members.id_wolves)
|
|
.bind(&user.server_members.expiry)
|
|
.fetch_optional(db)
|
|
.await
|
|
{
|
|
Ok(_) => {}
|
|
Err(e) => {
|
|
println!("Failure to insert into {} {:?}", server.as_u64(), user.server_members);
|
|
println!("{:?}", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct SkynetResult {
|
|
discord: String,
|
|
id_member: String,
|
|
}
|
|
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 {
|
|
add_users_skynet(db, &config.skynet_server, &user).await;
|
|
}
|
|
}
|
|
}
|
|
async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) {
|
|
match sqlx::query_as::<_, Wolves>(
|
|
"
|
|
UPDATE wolves
|
|
SET discord = ?
|
|
WHERE id_wolves = ?
|
|
",
|
|
)
|
|
.bind(&user.discord)
|
|
.bind(&user.id_member)
|
|
.fetch_optional(db)
|
|
.await
|
|
{
|
|
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,
|
|
}
|
|
async fn get_wolves(db: &Pool<Sqlite>) {
|
|
for server_config in get_server_config_bulk(db).await {
|
|
let Servers {
|
|
server,
|
|
//wolves_api,
|
|
..
|
|
} = server_config;
|
|
|
|
// get the data here
|
|
let result: Vec<WolvesResult> = vec![WolvesResult {
|
|
id_wolves: "12345".to_string(),
|
|
email: "ul.wolves2@brendan.ie".to_string(),
|
|
expiry: "2024-08-31".to_string(),
|
|
}];
|
|
|
|
for user in result {
|
|
add_users_wolves(db, &server, &user).await;
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResult) {
|
|
// expiry
|
|
match sqlx::query_as::<_, Wolves>(
|
|
"
|
|
INSERT OR REPLACE INTO wolves (id_wolves, email)
|
|
VALUES (?1, ?2)
|
|
",
|
|
)
|
|
.bind(&user.id_wolves)
|
|
.bind(&user.email)
|
|
.fetch_optional(db)
|
|
.await
|
|
{
|
|
Ok(_) => {}
|
|
Err(e) => {
|
|
println!("Failure to insert into Wolves {} {:?}", server.as_u64(), user);
|
|
println!("{:?}", e);
|
|
}
|
|
}
|
|
|
|
match sqlx::query_as::<_, ServerMembers>(
|
|
"
|
|
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
|
VALUES (?1, ?2, ?3)
|
|
",
|
|
)
|
|
.bind(*server.as_u64() as i64)
|
|
.bind(&user.id_wolves)
|
|
.bind(&user.expiry)
|
|
.fetch_optional(db)
|
|
.await
|
|
{
|
|
Ok(_) => {}
|
|
Err(e) => {
|
|
println!("Failure to insert into ServerMembers {} {:?}", server.as_u64(), user);
|
|
println!("{:?}", e);
|
|
}
|
|
}
|
|
}
|