Skip to content

Commit

Permalink
Refactor loading of environment variables for better maintainability …
Browse files Browse the repository at this point in the history
…and error handling
  • Loading branch information
kasugamirai committed Aug 20, 2024
1 parent e18732b commit f941995
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
34 changes: 3 additions & 31 deletions src/bin/bootstrap.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use gpt_discord_bot::create_client;
use gpt_discord_bot::get_env;
use gpt_discord_bot::load_environment_variables;
use gpt_discord_bot::Handler;
use serenity::all::GatewayIntents;
use serenity::Client;
use std::env;
use std::path::Path;
use tracing::{error, info};

#[tokio::main]
Expand Down Expand Up @@ -42,31 +42,3 @@ async fn main() {
error!("Client error: {:?}", why);
}
}

fn load_environment_variables() {
let path = ".env";
// Load environment variables from .env file
if Path::new(path).exists() {
match dotenv::dotenv() {
Ok(_) => {}
Err(e) => println!("Failed to load {} file: {}", path, e),
}
}
}

fn get_env(key: &str, error_message: &str) -> String {
match env::var(key) {
Ok(val) => val,
Err(_) => panic!("{}", error_message),
}
}

async fn create_client(discord_token: &str, intents: GatewayIntents, handler: Handler) -> Client {
match Client::builder(discord_token, intents)
.event_handler(handler)
.await
{
Ok(client) => client,
Err(e) => panic!("Err creating client{}", e),
}
}
19 changes: 19 additions & 0 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::env;
use std::path::Path;

pub fn load_environment_variables() {
let path = ".env";
if Path::new(path).exists() {
match dotenv::dotenv() {
Ok(_) => {}
Err(e) => println!("Failed to load {} file: {}", path, e),
}
}
}

pub fn get_env(key: &str, error_message: &str) -> String {
match env::var(key) {
Ok(val) => val,
Err(_) => panic!("{}", error_message),
}
}
16 changes: 16 additions & 0 deletions src/discord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use serenity::async_trait;
use serenity::builder::EditMessage;
use serenity::client::{Context, EventHandler};
use serenity::model::channel::Message;
use serenity::model::gateway::GatewayIntents;
use serenity::Client;
use std::time::Duration;
use thiserror::Error;
use tokio::time::interval;
Expand Down Expand Up @@ -111,3 +113,17 @@ fn filter_msg(msg: &Message) -> bool {
}
true
}

pub async fn create_client(
discord_token: &str,
intents: GatewayIntents,
handler: Handler,
) -> Client {
match Client::builder(discord_token, intents)
.event_handler(handler)
.await
{
Ok(client) => client,
Err(e) => panic!("Err creating client{}", e),
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
mod conf;
mod discord;
pub use crate::discord::create_client;
pub use crate::discord::Handler;
pub use conf::get_env;
pub use conf::load_environment_variables;

0 comments on commit f941995

Please sign in to comment.