feat: now use env vars to get teh server and roles for committee
This commit is contained in:
parent
d673dce6fa
commit
6739c7e068
3 changed files with 43 additions and 9 deletions
|
@ -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
|
||||
|
|
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
|
||||
}
|
||||
|
||||
|
|
11
src/main.rs
11
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},
|
||||
|
@ -37,10 +36,16 @@ impl EventHandler for Handler {
|
|||
Some(x) => x,
|
||||
};
|
||||
|
||||
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(&GuildId(1220150752656363520)) {
|
||||
if new_member.guild_id.eq(&config_global.committee_server) {
|
||||
let mut member = vec![new_member.clone()];
|
||||
update_committees(&db, &ctx, &mut member).await;
|
||||
update_committees(&db, &ctx, &config_global, &mut member).await;
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue