Compare commits

..

No commits in common. "5b22f699d687fd41880c91a10938123fafccf85a" and "7a6421469ca98c753fa7057464d794f71a4ba78d" have entirely different histories.

5 changed files with 30 additions and 65 deletions

View file

@ -305,8 +305,7 @@ pub mod link {
sqlx::query_as::<_, Wolves>( sqlx::query_as::<_, Wolves>(
" "
INSERT INTO wolves (id_wolves, email) INSERT INTO wolves (id_wolves, email)
VALUES ($1, $2) VALUES (?1, ?2)
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
", ",
) )
.bind(id_wolves) .bind(id_wolves)

View file

@ -133,9 +133,9 @@ pub mod normal {
pub mod committee { pub mod committee {
use crate::common::database::{DataBase, Wolves}; use crate::common::database::{DataBase, Wolves};
use crate::common::wolves::committees::Committees; use crate::common::wolves::committees::Committees;
use crate::Config;
use serenity::client::Context; use serenity::client::Context;
use serenity::model::guild::Member; use serenity::model::guild::Member;
use serenity::model::id::{GuildId, RoleId};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
@ -148,26 +148,15 @@ pub mod committee {
let db = db_lock.read().await; let db = db_lock.read().await;
let config_lock = { let server = GuildId(1220150752656363520);
let data_read = ctx.data.read().await;
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
};
let config_global = config_lock.read().await;
let server = config_global.committee_server;
// because to use it to update a single user we need to pre-get the members of teh server
let mut members = server.members(&ctx, None, None).await.unwrap_or_default(); let mut members = server.members(&ctx, None, None).await.unwrap_or_default();
update_committees(&db, &ctx, &config_global, &mut members).await; update_committees(&db, &ctx, &mut members).await;
} }
/** pub async fn update_committees(db: &Pool<Sqlite>, ctx: &Context, members: &mut Vec<Member>) {
This function can take a vec of members (or just one) and gives tehm the appropiate roles on teh committee server let server = GuildId(1220150752656363520);
*/ let committee_member = RoleId(1226602779968274573);
pub async fn update_committees(db: &Pool<Sqlite>, ctx: &Context, config: &Config, members: &mut Vec<Member>) {
let server = config.committee_server;
let committee_member = config.committee_role;
let committees = get_committees(db).await; let committees = get_committees(db).await;
// information about the server // information about the server

View file

@ -277,24 +277,25 @@ pub mod committees {
}; };
let config = config_lock.read().await; let config = config_lock.read().await;
// TODO: proper api key management
let api_key = "";
// request data from wolves // request data from wolves
for committee_wolves in get_committees(&config).await { for committee in get_committees(&config, api_key).await {
let committee = Committees::from(committee_wolves); let tmp = Committees::from(committee);
add_committee(&db, &committee).await; add_committee(&db, &tmp).await;
} }
} }
async fn get_committees(config: &Config) -> Vec<WolvesResultCNS> { async fn get_committees(config: &Config, wolves_api: &str) -> Vec<WolvesResultCNS> {
if config.wolves_url.is_empty() { if config.wolves_url.is_empty() {
return vec![]; return vec![];
} }
// TODO: Change teh stored env value to teh base domain // TODO: Change teh stored env value to teh base domain
// TODO: this address may change
let url = format!("{}/get_cns", &config.wolves_url); let url = format!("{}/get_cns", &config.wolves_url);
// get wolves data // get wolves data
if let Ok(mut res) = surf::post(&url).header("X-AM-Identity", &config.wolves_api).await { if let Ok(mut res) = surf::post(&url).header("X-AM-Identity", wolves_api).await {
if let Ok(WolvesResult { if let Ok(WolvesResult {
success, success,
result, result,

View file

@ -4,7 +4,6 @@ use chrono::{Datelike, SecondsFormat, Utc};
use dotenvy::dotenv; use dotenvy::dotenv;
use rand::{distributions::Alphanumeric, thread_rng, Rng}; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serenity::client::Context; use serenity::client::Context;
use serenity::model::id::{GuildId, RoleId};
use serenity::model::prelude::application_command::ApplicationCommandInteraction; use serenity::model::prelude::application_command::ApplicationCommandInteraction;
use serenity::prelude::TypeMapKey; use serenity::prelude::TypeMapKey;
use std::{env, sync::Arc}; use std::{env, sync::Arc};
@ -27,10 +26,6 @@ pub struct Config {
pub wolves_url: String, pub wolves_url: String,
// API key for accessing more general resources // API key for accessing more general resources
pub wolves_api: String, pub wolves_api: String,
// discord server for committee
pub committee_server: GuildId,
pub committee_role: RoleId,
} }
impl TypeMapKey for Config { impl TypeMapKey for Config {
type Value = Arc<RwLock<Config>>; type Value = Arc<RwLock<Config>>;
@ -52,8 +47,6 @@ pub fn get_config() -> Config {
mail_pass: "".to_string(), mail_pass: "".to_string(),
wolves_url: "".to_string(), wolves_url: "".to_string(),
wolves_api: "".to_string(), wolves_api: "".to_string(),
committee_server: GuildId(0),
committee_role: RoleId(0),
}; };
if let Ok(x) = env::var("DATABASE_HOME") { if let Ok(x) = env::var("DATABASE_HOME") {
@ -88,17 +81,6 @@ pub fn get_config() -> Config {
config.wolves_api = x.trim().to_string(); config.wolves_api = x.trim().to_string();
} }
if let Ok(x) = env::var("COMMITTEE_DISCORD") {
if let Ok(x) = x.trim().parse::<u64>() {
config.committee_server = GuildId(x);
}
}
if let Ok(x) = env::var("COMMITTEE_DISCORD") {
if let Ok(x) = x.trim().parse::<u64>() {
config.committee_role = RoleId(x);
}
}
config config
} }

View file

@ -2,6 +2,7 @@ pub mod commands;
use crate::commands::role_adder::tools::on_role_change; use crate::commands::role_adder::tools::on_role_change;
use serenity::model::guild::Member; use serenity::model::guild::Member;
use serenity::model::id::GuildId;
use serenity::{ use serenity::{
async_trait, async_trait,
client::{Context, EventHandler}, client::{Context, EventHandler},
@ -31,36 +32,29 @@ impl EventHandler for Handler {
}; };
let db = db_lock.read().await; let db = db_lock.read().await;
let config = match get_server_config(&db, &new_member.guild_id).await {
let config_lock = {
let data_read = ctx.data.read().await;
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
};
let config_global = config_lock.read().await;
// committee server takes priority
if new_member.guild_id.eq(&config_global.committee_server) {
let mut member = vec![new_member.clone()];
update_committees(&db, &ctx, &config_global, &mut member).await;
return;
}
let config_server = match get_server_config(&db, &new_member.guild_id).await {
None => return, None => return,
Some(x) => x, Some(x) => x,
}; };
// committee server takes priority
if new_member.guild_id.eq(&GuildId(1220150752656363520)) {
let mut member = vec![new_member.clone()];
update_committees(&db, &ctx, &mut member).await;
return;
}
if get_server_member(&db, &new_member.guild_id, &new_member).await.is_ok() { if get_server_member(&db, &new_member.guild_id, &new_member).await.is_ok() {
let mut roles = vec![]; let mut roles = vec![];
if let Some(role) = &config_server.role_past { if let Some(role) = &config.role_past {
if !new_member.roles.contains(role) { if !new_member.roles.contains(role) {
roles.push(role.to_owned()); roles.push(role.to_owned());
} }
} }
if !new_member.roles.contains(&config_server.role_current) { if !new_member.roles.contains(&config.role_current) {
roles.push(config_server.role_current.to_owned()); roles.push(config.role_current.to_owned());
} }
if let Err(e) = new_member.add_roles(&ctx, &roles).await { if let Err(e) = new_member.add_roles(&ctx, &roles).await {
@ -73,10 +67,10 @@ Welcome {} to the {} server!
Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use ``/link_wolves`` to get full access. Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use ``/link_wolves`` to get full access.
"#, "#,
new_member.display_name(), new_member.display_name(),
&config_server.server_name, &config.server_name,
&config_server.wolves_link, &config.wolves_link,
&config_server.server, &config.server,
&config_server.bot_channel_id &config.bot_channel_id
); );
if let Err(err) = new_member.user.direct_message(&ctx, |m| m.content(&msg)).await { if let Err(err) = new_member.user.direct_message(&ctx, |m| m.content(&msg)).await {