From e951b3012e043b55a1f279d4616085c5a653bcd4 Mon Sep 17 00:00:00 2001 From: Victor Mignot Date: Tue, 18 Oct 2022 23:23:54 -0400 Subject: [PATCH] Provide basic MongoDB connection --- src/client.rs | 66 +++++++++++++++++++++++++++++++++++++++++---------- src/lib.rs | 3 ++- src/main.rs | 17 ++++++++++--- 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/src/client.rs b/src/client.rs index 8bf394d..c97f4a2 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,3 +1,8 @@ +//! Module containing the Yorokobot client and used structs + +use crate::errors::ClientsError; + +use mongodb::{options::ClientOptions as MongoClientOptions, Client as MongoClient}; use serenity::{prelude::GatewayIntents, Client as DiscordClient}; /// Yorokobot's client. @@ -20,38 +25,75 @@ use serenity::{prelude::GatewayIntents, Client as DiscordClient}; /// /// # } /// ``` -/// pub struct Client { + /// The Serenity Discord Client discord_client: DiscordClient, + + /// The MongoDB Client + mongodb_client: Option, + + /// MongoDB Client Options + mongodb_options: MongoClientOptions, } /// Yorokobot connection credentials pub struct ClientCredentials<'a> { /// Token for Discord API pub discord_token: &'a String, + + /// MongoDB connection string. + pub mongo_uri: &'a String, } impl<'a> Client { /// Create a Yorokobot client - pub async fn new(credentials: ClientCredentials<'a>) -> Client { - let discord_client = - DiscordClient::builder(credentials.discord_token, GatewayIntents::empty()) - .await - .expect("Could not create Discord Client"); + pub async fn new(credentials: ClientCredentials<'a>) -> Result { + let discord_client = match DiscordClient::builder( + credentials.discord_token, + GatewayIntents::empty(), + ) + .await + { + Ok(c) => c, + Err(e) => return Err(ClientsError::Discord(e)), + }; - Client { discord_client } + let mongodb_options = match MongoClientOptions::parse(credentials.mongo_uri).await { + Ok(o) => o, + Err(e) => return Err(ClientsError::Database(e)), + }; + + Ok(Client { + discord_client, + mongodb_options, + mongodb_client: None, + }) } /// Start connection to Discord API. /// Wrap [`serenity::client::Client`] start method. - pub async fn connect_discord(&mut self) { - if let Err(error) = self.discord_client.start().await { - println!("Could not connect to Discord: {:?}", error); + pub async fn connect_discord(&mut self) -> Result<(), ClientsError> { + match self.discord_client.start().await { + Ok(_) => Ok(()), + Err(e) => return Err(ClientsError::Discord(e)), } } + /// Connect to the Mongo Database + pub fn connect_mongodb(&mut self) -> Result<(), ClientsError> { + self.mongodb_client = match MongoClient::with_options(self.mongodb_options.clone()) { + Ok(c) => Some(c), + Err(e) => return Err(ClientsError::Database(e)), + }; + + Ok(()) + } + /// Connect client to the Mongo database then to the Discord API. - pub async fn connect(&mut self) { - self.connect_discord().await; + pub async fn connect(&mut self) -> Result<(), ClientsError> { + self.connect_mongodb()?; + self.connect_discord().await?; + + Ok(()) } } diff --git a/src/lib.rs b/src/lib.rs index c18a41e..27b41a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,5 +6,6 @@ #![deny(missing_docs)] #![deny(warnings)] -/// Module containing the Yorokobot client and used structs pub mod client; + +pub mod errors; diff --git a/src/main.rs b/src/main.rs index e135ec0..3b488d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,27 @@ use std::env; -use yorokobot::client::{Client, ClientCredentials}; +use yorokobot::{ + client::{Client, ClientCredentials}, + errors::ClientsError, +}; #[tokio::main] async fn main() { let discord_token = env::var("DISCORD_TOKEN").expect("Cannot fetch Discord token"); + let mongodb_uri = env::var("MONGODB_URI").expect("Cannot fetch Mongo URI"); + let credentials = ClientCredentials { discord_token: &discord_token, + mongo_uri: &mongodb_uri, }; - let mut client = Client::new(credentials).await; + let mut client = Client::new(credentials) + .await + .expect("Could not create client"); - client.connect_discord().await; + client.connect().await.unwrap_or_else(|error| match error { + ClientsError::Database(e) => panic!("Could not connect to database: {:?}", e), + ClientsError::Discord(e) => panic!("Could not connect to Discord: {:?}", e), + }); }