make a better env parser

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

4
.env
View File

@ -1 +1,3 @@
DISCORD_TOKEN="PUT YOUR BOT TOKEN HERE"
DISCORD_INTENTS="intents / permissions integer"
DISCORD_TOKEN="Your bot token"
DISCORD_ID="Your bot id"

View File

@ -2,3 +2,12 @@
A quick rust-based discord bot template.
## Getting started
* goto: [Discord applicatoins](https://discord.com/developers/applications) and
create an application.
* Next goto the "bot" tab on the left, and "Reset Token". KEEP THIS PRIVATE.
When are testing / deploying your bot put this token in the `.env` file.
* Next decide what permissions your bot needs. Make sure to edit `intents` in `src/main.rs`
to match this. When you are creating the invite link on discord.com you will
have to select these permissions as well.

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#"
/$$$$$$$$ /$$ /$$ /$$$$$$$ /$$
@ -27,11 +47,14 @@ 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()