From 3a437db2b6b4e1fdab34d58af1269e62b314815a Mon Sep 17 00:00:00 2001 From: Victor Mignot Date: Sun, 20 Nov 2022 23:45:40 -0500 Subject: [PATCH] Advanced program structure for commands --- src/client.rs | 70 +++++++++++------------------- src/discord.rs | 3 +- src/discord/client.rs | 40 ----------------- src/discord/commands.rs | 0 src/discord/commands/create_tag.rs | 0 src/discord/event_handler.rs | 8 +++- src/lib.rs | 2 +- src/main.rs | 2 +- 8 files changed, 35 insertions(+), 90 deletions(-) delete mode 100644 src/discord/client.rs create mode 100644 src/discord/commands.rs create mode 100644 src/discord/commands/create_tag.rs diff --git a/src/client.rs b/src/client.rs index 8c3d895..d9ad68b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,60 +1,42 @@ -//! Module containing the Yorokobot client and used structs +use serenity::{prelude::GatewayIntents, Client as SerenityClient}; -use crate::database::Client as DatabaseClient; +use crate::{database::Client as DatabaseClient, environment::get_env_variable}; + +use crate::discord::event_handler::Handler; -use crate::discord::client::DiscordClient; -/// Yorokobot's client. -/// Used for connecting to the Discord API and your MongoDB database -/// -/// # Example -/// ```rust,no_run -/// # async fn run() { -/// use yorokobot::{Client, ClientCredentials, DatabaseCredentials}; -/// -/// let discord_token = String::from("Your discord token"); -/// let mongo_uri = String::from("Your Mongo URI"); -/// -/// let credentials = ClientCredentials { -/// discord_token: &discord_token, -/// db_credentials: DatabaseCredentials::parse(mongo_uri).await.expect("Failed parsing credentials"), -/// }; -/// -/// let mut client = Client::new(credentials).await.expect("Error creating client"); -/// -/// client.connect().await; -/// -/// # } -/// ``` pub struct Client { - /// The Serenity Discord Client - discord_client: DiscordClient, - - /// The database client + serenity_client: SerenityClient, database_client: DatabaseClient, } -impl Client { - /// Create a Yorokobot client - pub async fn new() -> Client { - let discord_client = DiscordClient::new().await; - +impl<'a> Client { + async fn new() -> Self { + let discord_token = get_env_variable("DISCORD_TOKEN"); + let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT; let database_client = DatabaseClient::new(); + let event_handler = Handler { + database: &database_client, + }; + + let serenity_client = match SerenityClient::builder(discord_token, intents) + .event_handler(event_handler) + .await + { + Ok(c) => c, + Err(e) => panic!("Failed to instantiate Discord Client: {e}"), + }; Client { - discord_client, + serenity_client, database_client, } } - /// Start connection to Discord API. - /// Wrap [`serenity::client::Client`] start method. - 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) { + pub async fn start(&mut self) { self.database_client.connect().await; - self.connect_discord().await; + + if let Err(e) = self.serenity_client.start().await { + panic!("Could not connect the bot: {e}"); + } } } diff --git a/src/discord.rs b/src/discord.rs index 9d54696..f0cc38c 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -1,2 +1 @@ -pub mod client; -mod event_handler; +pub mod event_handler; diff --git a/src/discord/client.rs b/src/discord/client.rs deleted file mode 100644 index e253548..0000000 --- a/src/discord/client.rs +++ /dev/null @@ -1,40 +0,0 @@ -use serenity::{prelude::GatewayIntents, Client}; - -use crate::environment::get_env_variable; - -use super::event_handler::Handler; - -pub struct DiscordClient { - serenity_client: Option, -} - -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) - .event_handler(Handler) - .await - { - Ok(c) => c, - Err(e) => panic!("Failed to instantiate Discord Client: {e}"), - }; - - DiscordClient { - 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/discord/commands.rs b/src/discord/commands.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/discord/commands/create_tag.rs b/src/discord/commands/create_tag.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/discord/event_handler.rs b/src/discord/event_handler.rs index 49bb252..f6bdede 100644 --- a/src/discord/event_handler.rs +++ b/src/discord/event_handler.rs @@ -8,12 +8,16 @@ use serenity::{ prelude::{Context, EventHandler}, }; +use crate::database::Client as DatabaseClient; + const MAX_ARGS_NUMBER: u32 = 25; -pub struct Handler; +pub struct Handler<'a> { + pub database: &'a DatabaseClient, +} #[async_trait] -impl EventHandler for Handler { +impl EventHandler for Handler<'_> { async fn ready(&self, ctx: Context, ready: Ready) { println!("Successfully connected as {}", ready.user.name); diff --git a/src/lib.rs b/src/lib.rs index ccb1e9c..f2183ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! //! [`Serenity`]: https://github.com/serenity-rs/serenity -#![deny(missing_docs)] +//#![deny(missing_docs)] #![deny(warnings)] mod client; diff --git a/src/main.rs b/src/main.rs index acf0d57..7cc05c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,5 +7,5 @@ async fn main() { let mut client = Client::new().await; - client.connect().await; + client.start().await; }