refactor: improve loading token var

This commit is contained in:
Lucaskyy 2022-07-09 18:30:21 +02:00
parent 704d05f45b
commit 25d343dfa5
No known key found for this signature in database
GPG Key ID: 1530BFF96D1EEB89

View File

@ -2,7 +2,6 @@ use std::sync::Arc;
use std::{env, process}; use std::{env, process};
use chrono::{DateTime, Duration, NaiveDateTime, Utc}; use chrono::{DateTime, Duration, NaiveDateTime, Utc};
use tracing::{error, info, debug};
use model::application::Configuration; use model::application::Configuration;
use regex::Regex; use regex::Regex;
use serenity::client::{Context, EventHandler}; use serenity::client::{Context, EventHandler};
@ -12,6 +11,7 @@ use serenity::model::prelude::command::Command;
use serenity::model::prelude::interaction::{Interaction, InteractionResponseType, MessageFlags}; use serenity::model::prelude::interaction::{Interaction, InteractionResponseType, MessageFlags};
use serenity::prelude::{GatewayIntents, RwLock, TypeMapKey}; use serenity::prelude::{GatewayIntents, RwLock, TypeMapKey};
use serenity::{async_trait, Client}; use serenity::{async_trait, Client};
use tracing::{debug, error, info};
mod logger; mod logger;
mod model; mod model;
@ -228,15 +228,16 @@ async fn main() {
// Set up the configuration. // Set up the configuration.
let configuration = load_configuration(); let configuration = load_configuration();
// Get the Discord authorization token. // Load environment variables from .env file
dotenv::dotenv().ok(); dotenv::dotenv().ok();
let token = match env::vars().find(|(key, _)| key == "DISCORD_AUTHORIZATION_TOKEN") {
Some((_, value)) => value, // Get the Discord authorization token.
None => { let token = env::var("DISCORD_AUTHORIZATION_TOKEN")
error!("Environment variable DISCORD_AUTHORIZATION_TOKEN unset."); .expect("Could not load Discord authorization token");
process::exit(1); if token.len() != 70 {
}, error!("Invalid Discord authorization token.");
}; process::exit(1);
}
// Create the Discord bot client. // Create the Discord bot client.
let mut client = Client::builder( let mut client = Client::builder(