feat: now using an env file

This commit is contained in:
silver 2023-08-27 17:30:54 +01:00
parent 1694c4eebf
commit 9b42187b22
4 changed files with 80 additions and 28 deletions

3
.gitignore vendored
View file

@ -1,2 +1,5 @@
/target
/.idea
.env

7
Cargo.lock generated
View file

@ -510,6 +510,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
[[package]]
name = "dotenvy"
version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
[[package]]
name = "encoding_rs"
version = "0.8.33"
@ -1691,6 +1697,7 @@ dependencies = [
name = "skynet_discord_bot"
version = "0.1.0"
dependencies = [
"dotenvy",
"serenity",
"surf",
"tokio",

View file

@ -8,5 +8,8 @@ edition = "2021"
[dependencies]
serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "cache"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
# to make the http requests
surf = "2.3.2"
dotenvy = "0.15.7"

View file

@ -1,7 +1,8 @@
use std::env;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use dotenvy::dotenv;
use serenity::async_trait;
use serenity::model::gateway::{GatewayIntents, Ready};
use serenity::model::guild::Member;
@ -84,18 +85,6 @@ impl EventHandler for Handler {
}
}
struct Config {
server: GuildId,
member_role_current: RoleId,
member_role_past: RoleId,
auth: String,
timing_update: u64,
timing_fetch: u64,
}
impl TypeMapKey for Config {
type Value = Arc<RwLock<Config>>;
}
#[derive(Default, Debug)]
struct MembersCount {
members: i32,
@ -184,7 +173,8 @@ async fn fetch_accounts(ctx: Arc<Context>){
};
let config = config_lock.read().await;
let auth = &config.auth;
let url = format!("http://127.0.0.1:8087/ldap/discord?auth={}", auth);
let ldap_api = &config.ldap_api;
let url = format!("{}/ldap/discord?auth={}", ldap_api, auth);
if let Ok(result) = surf::get(url).recv_json::<Vec<String>>().await {
let members_lock = {
let data_read = ctx.data.read().await;
@ -197,14 +187,12 @@ async fn fetch_accounts(ctx: Arc<Context>){
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
//let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = String::from("");
let config = get_config();
// 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(token, intents)
let mut client = Client::builder(&config.discord_token, intents)
.event_handler(Handler { is_loop_running: AtomicBool::new(false)})
.await
.expect("Error creating client");
@ -218,15 +206,6 @@ async fn main() {
// a list of all current members
data.insert::<Members>(Arc::new(RwLock::new(vec![])));
let config = Config {
server: GuildId(957961810147938334),
member_role_current: RoleId::from(1144760670995370094),
member_role_past: RoleId::from(1144760548072886353),
auth: String::from("abcdef"),
timing_update: 30,
timing_fetch: 50,
};
data.insert::<Config>(Arc::new(RwLock::new(config)));
}
@ -239,3 +218,63 @@ async fn main() {
println!("Client error: {:?}", why);
}
}
struct Config {
server: GuildId,
member_role_current: RoleId,
member_role_past: RoleId,
ldap_api: String,
auth: String,
timing_update: u64,
timing_fetch: u64,
discord_token: String,
}
impl TypeMapKey for Config {
type Value = Arc<RwLock<Config>>;
}
fn get_config() -> Config {
dotenv().ok();
// reasonable defaults
let mut config = Config {
server: Default::default(),
member_role_current: Default::default(),
member_role_past: Default::default(),
ldap_api: "https://api.account.skynet.ie".to_string(),
auth: "".to_string(),
timing_update: 0,
timing_fetch: 0,
discord_token: "".to_string(),
};
if let Ok(x) = env::var("DISCORD_SERVER") {
config.server = GuildId::from(str_to_num::<u64>(&x));
}
if let Ok(x) = env::var("DISCORD_ROLE_CURRENT") {
config.member_role_current = RoleId::from(str_to_num::<u64>(&x));
}
if let Ok(x) = env::var("DISCORD_ROLE_PAST") {
config.member_role_past = RoleId::from(str_to_num::<u64>(&x));
}
if let Ok(x) = env::var("LDAP_API") {
config.ldap_api = x.trim().to_string();
}
if let Ok(x) = env::var("LDAP_DISCORD_AUTH") {
config.auth = x.trim().to_string();
}
if let Ok(x) = env::var("DISCORD_TIMING_UPDATE") {
config.timing_update = str_to_num::<u64>(&x);
}
if let Ok(x) = env::var("DISCORD_TIMING_FETCH") {
config.timing_fetch = str_to_num::<u64>(&x);
}
if let Ok(x) = env::var("DISCORD_TOKEN") {
config.discord_token = x.trim().to_string();
}
config
}
fn str_to_num<T: std::str::FromStr + Default>(x: &str) -> T {
x.trim().parse::<T>().unwrap_or_default()
}