make a better env parser

This commit is contained in:
2023-11-02 18:20:15 -06:00
parent ac8cf3b489
commit 5593c56831
3 changed files with 40 additions and 6 deletions

View File

@@ -1,15 +1,35 @@
use poise::serenity_prelude as serenity;
use poise::serenity_prelude::{self as serenity, GatewayIntents};
use dotenv;
mod command;
pub struct Data {} // User data, which is stored and accessible in all command invocations
type Context<'a> = poise::Context<'a, Data, anyhow::Error>;
const DISCORD_INTENTS: &'static str = "DISCORD_INTENTS";
const DISCORD_TOKEN: &'static str = "DISCORD_TOKEN";
const DISCORD_ID: &'static str = "DISCORD_ID";
#[tokio::main]
async fn main() {
// ======================================== READ ENV ========================================
dotenv::dotenv().ok();
let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
let id: String = std::env::var(DISCORD_ID).expect(&format!("ERROR: Missing {DISCORD_ID}"));
let token: String = std::env::var(DISCORD_TOKEN).expect(&format!("ERROR: Missing {DISCORD_TOKEN}"));
let perm_env: String = std::env::var(DISCORD_INTENTS)
.unwrap_or_else(|msg| {
println!("DEBUG: what is: {msg}");
println!("ERROR: Missing {DISCORD_INTENTS}");
"0".to_string()
});
// parse into u64, then into GatewayIntents
let perm_u64 = perm_env.parse::<u64>().unwrap_or(0u64);
let intents: GatewayIntents = GatewayIntents::from_bits(perm_u64).unwrap_or(GatewayIntents::empty());
// Re-build into string, just incase it has changed
let perm_string = intents.bits().to_string();
// ======================================== ENV DONE ========================================
// Generate sick text like this:
// http://www.patorjk.com/software/taag/#p=testall&f=Graffiti&t=hello%20world
println!(r#"
/$$$$$$$$ /$$ /$$ /$$$$$$$ /$$
@@ -26,12 +46,15 @@ async fn main() {
Template Bot.
Invite this bot with:
https://discord.com/api/oauth2/authorize?client_id={id}&permissions={perm_string}&scope=bot
"#);
// Use bitlogic "OR" to combine different intents.
let intents =
serenity::GatewayIntents::GUILD_VOICE_STATES |
serenity::GatewayIntents::non_privileged();
intents |
GatewayIntents::non_privileged();
let framework = poise::Framework::builder()