Compare commits
4 commits
7a6421469c
...
5b22f699d6
Author | SHA1 | Date | |
---|---|---|---|
5b22f699d6 | |||
6739c7e068 | |||
d673dce6fa | |||
015f23b922 |
5 changed files with 65 additions and 30 deletions
|
@ -305,7 +305,8 @@ pub mod link {
|
|||
sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
INSERT INTO wolves (id_wolves, email)
|
||||
VALUES (?1, ?2)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
||||
",
|
||||
)
|
||||
.bind(id_wolves)
|
||||
|
|
|
@ -133,9 +133,9 @@ pub mod normal {
|
|||
pub mod committee {
|
||||
use crate::common::database::{DataBase, Wolves};
|
||||
use crate::common::wolves::committees::Committees;
|
||||
use crate::Config;
|
||||
use serenity::client::Context;
|
||||
use serenity::model::guild::Member;
|
||||
use serenity::model::id::{GuildId, RoleId};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
@ -148,15 +148,26 @@ pub mod committee {
|
|||
|
||||
let db = db_lock.read().await;
|
||||
|
||||
let server = GuildId(1220150752656363520);
|
||||
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;
|
||||
|
||||
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();
|
||||
|
||||
update_committees(&db, &ctx, &mut members).await;
|
||||
update_committees(&db, &ctx, &config_global, &mut members).await;
|
||||
}
|
||||
|
||||
pub async fn update_committees(db: &Pool<Sqlite>, ctx: &Context, members: &mut Vec<Member>) {
|
||||
let server = GuildId(1220150752656363520);
|
||||
let committee_member = RoleId(1226602779968274573);
|
||||
/**
|
||||
This function can take a vec of members (or just one) and gives tehm the appropiate roles on teh committee server
|
||||
*/
|
||||
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;
|
||||
|
||||
// information about the server
|
||||
|
|
|
@ -277,25 +277,24 @@ pub mod committees {
|
|||
};
|
||||
let config = config_lock.read().await;
|
||||
|
||||
// TODO: proper api key management
|
||||
let api_key = "";
|
||||
// request data from wolves
|
||||
for committee in get_committees(&config, api_key).await {
|
||||
let tmp = Committees::from(committee);
|
||||
add_committee(&db, &tmp).await;
|
||||
for committee_wolves in get_committees(&config).await {
|
||||
let committee = Committees::from(committee_wolves);
|
||||
add_committee(&db, &committee).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_committees(config: &Config, wolves_api: &str) -> Vec<WolvesResultCNS> {
|
||||
async fn get_committees(config: &Config) -> Vec<WolvesResultCNS> {
|
||||
if config.wolves_url.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
// TODO: Change teh stored env value to teh base domain
|
||||
// TODO: this address may change
|
||||
let url = format!("{}/get_cns", &config.wolves_url);
|
||||
|
||||
// get wolves data
|
||||
if let Ok(mut res) = surf::post(&url).header("X-AM-Identity", wolves_api).await {
|
||||
if let Ok(mut res) = surf::post(&url).header("X-AM-Identity", &config.wolves_api).await {
|
||||
if let Ok(WolvesResult {
|
||||
success,
|
||||
result,
|
||||
|
|
18
src/lib.rs
18
src/lib.rs
|
@ -4,6 +4,7 @@ use chrono::{Datelike, SecondsFormat, Utc};
|
|||
use dotenvy::dotenv;
|
||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||
use serenity::client::Context;
|
||||
use serenity::model::id::{GuildId, RoleId};
|
||||
use serenity::model::prelude::application_command::ApplicationCommandInteraction;
|
||||
use serenity::prelude::TypeMapKey;
|
||||
use std::{env, sync::Arc};
|
||||
|
@ -26,6 +27,10 @@ pub struct Config {
|
|||
pub wolves_url: String,
|
||||
// API key for accessing more general resources
|
||||
pub wolves_api: String,
|
||||
|
||||
// discord server for committee
|
||||
pub committee_server: GuildId,
|
||||
pub committee_role: RoleId,
|
||||
}
|
||||
impl TypeMapKey for Config {
|
||||
type Value = Arc<RwLock<Config>>;
|
||||
|
@ -47,6 +52,8 @@ pub fn get_config() -> Config {
|
|||
mail_pass: "".to_string(),
|
||||
wolves_url: "".to_string(),
|
||||
wolves_api: "".to_string(),
|
||||
committee_server: GuildId(0),
|
||||
committee_role: RoleId(0),
|
||||
};
|
||||
|
||||
if let Ok(x) = env::var("DATABASE_HOME") {
|
||||
|
@ -81,6 +88,17 @@ pub fn get_config() -> Config {
|
|||
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
|
||||
}
|
||||
|
||||
|
|
38
src/main.rs
38
src/main.rs
|
@ -2,7 +2,6 @@ pub mod commands;
|
|||
|
||||
use crate::commands::role_adder::tools::on_role_change;
|
||||
use serenity::model::guild::Member;
|
||||
use serenity::model::id::GuildId;
|
||||
use serenity::{
|
||||
async_trait,
|
||||
client::{Context, EventHandler},
|
||||
|
@ -32,29 +31,36 @@ impl EventHandler for Handler {
|
|||
};
|
||||
|
||||
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,
|
||||
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() {
|
||||
let mut roles = vec![];
|
||||
|
||||
if let Some(role) = &config.role_past {
|
||||
if let Some(role) = &config_server.role_past {
|
||||
if !new_member.roles.contains(role) {
|
||||
roles.push(role.to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
if !new_member.roles.contains(&config.role_current) {
|
||||
roles.push(config.role_current.to_owned());
|
||||
if !new_member.roles.contains(&config_server.role_current) {
|
||||
roles.push(config_server.role_current.to_owned());
|
||||
}
|
||||
|
||||
if let Err(e) = new_member.add_roles(&ctx, &roles).await {
|
||||
|
@ -67,10 +73,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.
|
||||
"#,
|
||||
new_member.display_name(),
|
||||
&config.server_name,
|
||||
&config.wolves_link,
|
||||
&config.server,
|
||||
&config.bot_channel_id
|
||||
&config_server.server_name,
|
||||
&config_server.wolves_link,
|
||||
&config_server.server,
|
||||
&config_server.bot_channel_id
|
||||
);
|
||||
|
||||
if let Err(err) = new_member.user.direct_message(&ctx, |m| m.content(&msg)).await {
|
||||
|
|
Loading…
Reference in a new issue