2023-09-11 19:02:16 +00:00
|
|
|
use skynet_discord_bot::{db_init, get_config, Accounts, Config, DataBase, get_server_config_bulk, Servers};
|
2023-09-11 17:18:59 +00:00
|
|
|
use std::{process, sync::Arc};
|
2023-09-11 01:25:07 +00:00
|
|
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serenity::{
|
|
|
|
async_trait,
|
|
|
|
client::{Context, EventHandler},
|
2023-09-11 17:18:59 +00:00
|
|
|
model::{
|
|
|
|
gateway::{GatewayIntents, Ready},
|
|
|
|
id::GuildId,
|
|
|
|
},
|
2023-09-11 01:25:07 +00:00
|
|
|
Client,
|
|
|
|
};
|
|
|
|
use sqlx::{Pool, Sqlite};
|
|
|
|
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let config = get_config();
|
|
|
|
let db = match db_init(&config).await {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Intents are a bitflag, bitwise operations can be used to dictate which intents to use
|
|
|
|
let intents = GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS;
|
|
|
|
// Build our client.
|
|
|
|
let mut client = Client::builder(&config.discord_token, intents)
|
|
|
|
.event_handler(Handler {})
|
|
|
|
.await
|
|
|
|
.expect("Error creating client");
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut data = client.data.write().await;
|
|
|
|
|
|
|
|
data.insert::<Config>(Arc::new(RwLock::new(config)));
|
|
|
|
data.insert::<DataBase>(Arc::new(RwLock::new(db)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, start a single shard, and start listening to events.
|
|
|
|
//
|
|
|
|
// Shards will automatically attempt to reconnect, and will perform
|
|
|
|
// exponential backoff until it reconnects.
|
|
|
|
if let Err(why) = client.start().await {
|
|
|
|
println!("Client error: {:?}", why);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Handler;
|
|
|
|
#[async_trait]
|
|
|
|
impl EventHandler for Handler {
|
|
|
|
async fn ready(&self, ctx: Context, ready: Ready) {
|
|
|
|
println!("{} is connected!", ready.user.name);
|
|
|
|
|
|
|
|
fetch_accounts(&ctx).await;
|
|
|
|
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn fetch_accounts(ctx: &Context) {
|
|
|
|
let config_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
|
|
|
};
|
|
|
|
let config = config_lock.read().await;
|
|
|
|
|
|
|
|
let db_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<DataBase>().expect("Expected Config in TypeMap.").clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
let db = db_lock.read().await;
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
wolves_id: String,
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
add_users_skynet(&db, &config.skynet_server, &user).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 19:02:16 +00:00
|
|
|
async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) {
|
2023-09-11 01:25:07 +00:00
|
|
|
match sqlx::query_as::<_, Accounts>(
|
|
|
|
"
|
|
|
|
UPDATE accounts
|
|
|
|
SET discord = ?
|
|
|
|
WHERE server = ? AND wolves_id = ?
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.bind(&user.discord)
|
|
|
|
.bind(*server.as_u64() as i64)
|
|
|
|
.bind(&user.wolves_id)
|
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Failure to insert into {} {:?}", server.as_u64(), user);
|
|
|
|
println!("{:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 19:02:16 +00:00
|
|
|
|
|
|
|
#[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 mut result: Vec<WolvesResult> = vec![];
|
|
|
|
|
|
|
|
for user in result {
|
|
|
|
add_users_wolves(&db, &server, &user).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResult) {
|
|
|
|
match sqlx::query_as::<_, Accounts>(
|
|
|
|
"
|
|
|
|
INSERT OR REPLACE INTO accounts (server, wolves_id, email, expiry)
|
|
|
|
VALUES (?1, ?2, ?3, ?4)
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|