feat: now use env vars to get teh server and roles for committee

This commit is contained in:
silver 2024-11-09 14:55:26 +00:00
parent d673dce6fa
commit 6739c7e068
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
3 changed files with 43 additions and 9 deletions

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,15 +148,26 @@ pub mod committee {
let db = db_lock.read().await; 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(); 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); This function can take a vec of members (or just one) and gives tehm the appropiate roles on teh committee server
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

@ -4,6 +4,7 @@ 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};
@ -26,6 +27,10 @@ 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>>;
@ -47,6 +52,8 @@ 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") {
@ -81,6 +88,17 @@ 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,7 +2,6 @@ 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},
@ -37,10 +36,16 @@ impl EventHandler for Handler {
Some(x) => x, 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 // 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()]; let mut member = vec![new_member.clone()];
update_committees(&db, &ctx, &mut member).await; update_committees(&db, &ctx, &config_global, &mut member).await;
return; return;
} }