make a better env parser
This commit is contained in:
parent
ac8cf3b489
commit
5593c56831
4
.env
4
.env
@ -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"
|
||||||
|
@ -2,3 +2,12 @@
|
|||||||
|
|
||||||
A quick rust-based discord bot template.
|
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.
|
||||||
|
|
||||||
|
33
src/main.rs
33
src/main.rs
@ -1,15 +1,35 @@
|
|||||||
use poise::serenity_prelude as serenity;
|
use poise::serenity_prelude::{self as serenity, GatewayIntents};
|
||||||
use dotenv;
|
use dotenv;
|
||||||
mod command;
|
mod command;
|
||||||
|
|
||||||
pub struct Data {} // User data, which is stored and accessible in all command invocations
|
pub struct Data {} // User data, which is stored and accessible in all command invocations
|
||||||
type Context<'a> = poise::Context<'a, Data, anyhow::Error>;
|
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]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
// ======================================== READ ENV ========================================
|
||||||
dotenv::dotenv().ok();
|
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#"
|
println!(r#"
|
||||||
|
|
||||||
/$$$$$$$$ /$$ /$$ /$$$$$$$ /$$
|
/$$$$$$$$ /$$ /$$ /$$$$$$$ /$$
|
||||||
@ -26,12 +46,15 @@ async fn main() {
|
|||||||
|
|
||||||
Template Bot.
|
Template Bot.
|
||||||
Invite this bot with:
|
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 =
|
let intents =
|
||||||
serenity::GatewayIntents::GUILD_VOICE_STATES |
|
intents |
|
||||||
serenity::GatewayIntents::non_privileged();
|
GatewayIntents::non_privileged();
|
||||||
|
|
||||||
|
|
||||||
let framework = poise::Framework::builder()
|
let framework = poise::Framework::builder()
|
||||||
|
Loading…
Reference in New Issue
Block a user