diff --git a/.env.example b/.env.example index 6ffb155..237dcc1 100644 --- a/.env.example +++ b/.env.example @@ -2,7 +2,7 @@ DISCORD_TOKEN="Enter your Discord secret token" # MongoDB connection string -MONGO_URI="Enter your Mongo connection string" +MONGODB_URI="Enter your Mongo connection string" # YorokoBot database name (if not set in Mongo URI) -MONGO_DEFAULT_DB="Enter the MongoDB database name" +MONGODB_DATABASE="Enter the MongoDB database name" diff --git a/src/client.rs b/src/client.rs index 2fa6d97..8c3d895 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,6 +1,6 @@ //! Module containing the Yorokobot client and used structs -use crate::{database::Client as DatabaseClient, errors::ClientError, DatabaseCredentials}; +use crate::database::Client as DatabaseClient; use crate::discord::client::DiscordClient; /// Yorokobot's client. @@ -33,23 +33,17 @@ pub struct Client { database_client: DatabaseClient, } -/// Yorokobot connection credentials -pub struct ClientCredentials { - /// MongoDB connection string. - pub db_credentials: DatabaseCredentials, -} - impl Client { /// Create a Yorokobot client - pub async fn new(credentials: ClientCredentials) -> Result { + pub async fn new() -> Client { let discord_client = DiscordClient::new().await; - let database_client = DatabaseClient::new(credentials.db_credentials); + let database_client = DatabaseClient::new(); - Ok(Client { + Client { discord_client, database_client, - }) + } } /// Start connection to Discord API. @@ -59,10 +53,8 @@ impl Client { } /// Connect client to the Mongo database then to the Discord API. - pub async fn connect(&mut self) -> Result<(), ClientError> { - self.database_client.connect().await?; + pub async fn connect(&mut self) { + self.database_client.connect().await; self.connect_discord().await; - - Ok(()) } } diff --git a/src/database.rs b/src/database.rs index a1c1f48..e205e1b 100644 --- a/src/database.rs +++ b/src/database.rs @@ -3,4 +3,4 @@ pub mod client; mod models; -pub use {client::Client, mongodb::options::ClientOptions as DatabaseCredentials}; +pub use client::Client; diff --git a/src/database/client.rs b/src/database/client.rs index 4b2ec5c..f18c28c 100644 --- a/src/database/client.rs +++ b/src/database/client.rs @@ -8,8 +8,6 @@ use mongodb::{ use serde::{Deserialize, Serialize}; use crate::environment::get_env_variable; -use crate::errors::ClientError; -use crate::DatabaseCredentials; use super::models::{YorokobotModel, COLLECTIONS_NAMES}; @@ -17,31 +15,29 @@ use super::models::{YorokobotModel, COLLECTIONS_NAMES}; pub struct Client { mongo_client: Option, database: Option, - credentials: DatabaseCredentials, } impl Client { /// Create a new database client - pub fn new(credentials: DatabaseCredentials) -> Client { + pub fn new() -> Client { Client { - credentials, mongo_client: None, database: None, } } /// Connect the client - pub async fn connect(&mut self) -> Result<(), ClientError> { - self.mongo_client = match MongoClient::with_options(self.credentials.to_owned()) { + pub async fn connect(&mut self) { + self.mongo_client = match MongoClient::with_uri_str(get_env_variable("MONGODB_URI")).await { Ok(c) => Some(c), - Err(e) => return Err(ClientError::Database(e)), + Err(e) => panic!("Failed to connect to Mongo database: {e}"), }; self.database = Some( self.mongo_client .as_ref() .unwrap() - .database(get_env_variable("MONGO_DEFAULT_DB").as_str()), + .database(get_env_variable("MONGODB_DATABASE").as_str()), ); // TODO: @@ -49,8 +45,6 @@ impl Client { // Ex: DatabaseConnection self.check_init_error().await; - - Ok(()) } async fn check_init_error(&mut self) { diff --git a/src/errors.rs b/src/errors.rs deleted file mode 100644 index 5891260..0000000 --- a/src/errors.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Common Yorokobot errors - -pub use mongodb::error::Error as DatabaseError; -pub use serenity::prelude::SerenityError as DiscordError; - -/// The kind of errors that can be returned by Client::new -#[derive(Debug)] -pub enum ClientError { - /// Serenity error while building client - Discord(DiscordError), - - ///Mongo error while parsing options - Database(DatabaseError), -} diff --git a/src/lib.rs b/src/lib.rs index 6c0be12..ccb1e9c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,9 +10,5 @@ mod client; mod database; mod discord; mod environment; -pub mod errors; -pub use crate::{ - client::{Client, ClientCredentials}, - database::DatabaseCredentials, -}; +pub use crate::client::Client; diff --git a/src/main.rs b/src/main.rs index edd315c..acf0d57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,51 +1,11 @@ -use log::error; -use std::env; - -use yorokobot::{errors::ClientError, Client, ClientCredentials, DatabaseCredentials}; +use yorokobot::Client; #[tokio::main] -async fn main() -> std::process::ExitCode { +async fn main() { // Start the logger log4rs::init_file("log4rs_config.yaml", Default::default()).unwrap(); - let mongodb_uri = match env::var("MONGODB_URI") { - Ok(u) => u, - Err(_) => { - error!(target: "bot_warn_errors", "Could not find MONGODB_URI environment variable."); - return std::process::ExitCode::FAILURE; - } - }; + let mut client = Client::new().await; - let db_credentials = match DatabaseCredentials::parse(mongodb_uri).await { - Ok(c) => c, - Err(_) => { - error!(target: "bot_warn_errors", "Could not parse database credentials."); - return std::process::ExitCode::FAILURE; - } - }; - - let credentials = ClientCredentials { db_credentials }; - - let mut client = match Client::new(credentials).await { - Ok(c) => c, - Err(_) => { - error!(target: "bot_warn_errors", "Could not instantiate the bot client."); - return std::process::ExitCode::FAILURE; - } - }; - - if let Err(error) = client.connect().await { - match error { - ClientError::Database(e) => { - error!(target: "bot_warn_errors", "Could not connect to database: {:?}", e) - } - ClientError::Discord(e) => { - error!(target: "bot_warn_errors", "Could not connect to Discord: {:?}", e) - } - }; - - return std::process::ExitCode::FAILURE; - } - - std::process::ExitCode::SUCCESS + client.connect().await; }