From d4ef9632da03aba9f7b8e62e9ae46ae4f7b2a2cc Mon Sep 17 00:00:00 2001 From: Victor Mignot Date: Sun, 20 Nov 2022 18:55:20 -0500 Subject: [PATCH] Set discord token fetching into library --- src/client.rs | 31 ++++++++----------------------- src/discord.rs | 2 +- src/discord/client.rs | 22 +++++++++++++++------- src/main.rs | 13 +------------ 4 files changed, 25 insertions(+), 43 deletions(-) diff --git a/src/client.rs b/src/client.rs index 5300eb7..2fa6d97 100644 --- a/src/client.rs +++ b/src/client.rs @@ -2,8 +2,7 @@ use crate::{database::Client as DatabaseClient, errors::ClientError, DatabaseCredentials}; -use serenity::{prelude::GatewayIntents, Client as DiscordClient}; - +use crate::discord::client::DiscordClient; /// Yorokobot's client. /// Used for connecting to the Discord API and your MongoDB database /// @@ -35,26 +34,15 @@ pub struct Client { } /// Yorokobot connection credentials -pub struct ClientCredentials<'a> { - /// Token for Discord API - pub discord_token: &'a String, - +pub struct ClientCredentials { /// MongoDB connection string. pub db_credentials: DatabaseCredentials, } -impl<'a> Client { +impl Client { /// Create a Yorokobot 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(ClientError::Discord(e)), - }; + pub async fn new(credentials: ClientCredentials) -> Result { + let discord_client = DiscordClient::new().await; let database_client = DatabaseClient::new(credentials.db_credentials); @@ -66,17 +54,14 @@ impl<'a> Client { /// Start connection to Discord API. /// Wrap [`serenity::client::Client`] start method. - pub async fn connect_discord(&mut self) -> Result<(), ClientError> { - match self.discord_client.start().await { - Ok(_) => Ok(()), - Err(e) => Err(ClientError::Discord(e)), - } + pub async fn connect_discord(&mut self) { + self.discord_client.start().await; } /// Connect client to the Mongo database then to the Discord API. pub async fn connect(&mut self) -> Result<(), ClientError> { self.database_client.connect().await?; - self.connect_discord().await?; + self.connect_discord().await; Ok(()) } diff --git a/src/discord.rs b/src/discord.rs index 85bfb09..9d54696 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -1,2 +1,2 @@ -mod client; +pub mod client; mod event_handler; diff --git a/src/discord/client.rs b/src/discord/client.rs index 31f3d19..e253548 100644 --- a/src/discord/client.rs +++ b/src/discord/client.rs @@ -4,19 +4,16 @@ use crate::environment::get_env_variable; use super::event_handler::Handler; -const INTENTS: GatewayIntents = GatewayIntents::empty(); - -struct DiscordClient { +pub struct DiscordClient { serenity_client: Option, - - discord_token: String, } impl DiscordClient { pub async fn new() -> Self { let discord_token = get_env_variable("DISCORD_TOKEN"); + let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT; - let serenity_client = match Client::builder(&discord_token, INTENTS) + let serenity_client = match Client::builder(&discord_token, intents) .event_handler(Handler) .await { @@ -25,8 +22,19 @@ impl DiscordClient { }; DiscordClient { - discord_token, serenity_client: Some(serenity_client), } } + + pub async fn start(&mut self) { + if let Err(e) = self + .serenity_client + .as_mut() + .expect("No bot instance was created.") + .start() + .await + { + panic!("Could not connect the bot: {e}"); + } + } } diff --git a/src/main.rs b/src/main.rs index 528a5d2..edd315c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,14 +8,6 @@ async fn main() -> std::process::ExitCode { // Start the logger log4rs::init_file("log4rs_config.yaml", Default::default()).unwrap(); - let discord_token = match env::var("DISCORD_TOKEN") { - Ok(t) => t, - Err(_) => { - error!(target: "bot_warn_errors", "Could not find DISCORD_TOKEN environment variable."); - return std::process::ExitCode::FAILURE; - } - }; - let mongodb_uri = match env::var("MONGODB_URI") { Ok(u) => u, Err(_) => { @@ -32,10 +24,7 @@ async fn main() -> std::process::ExitCode { } }; - let credentials = ClientCredentials { - discord_token: &discord_token, - db_credentials, - }; + let credentials = ClientCredentials { db_credentials }; let mut client = match Client::new(credentials).await { Ok(c) => c,