Generate doc

This commit is contained in:
Victor Mignot 2022-10-17 13:41:28 -04:00
parent a66546c5b0
commit bd87344834
No known key found for this signature in database
GPG key ID: FFE4EF056FB5E0D0
3 changed files with 46 additions and 8 deletions

View file

@ -1,25 +1,54 @@
use serenity::{prelude::GatewayIntents, Client as DiscordClient}; use serenity::{prelude::GatewayIntents, Client as DiscordClient};
/// Yorokobot's client.
/// Used for connecting to the Discord API and your MongoDB database
///
/// # Example
/// ```rust,no_run
/// use yorokobot::client::{Client, ClientCredentials};
///
/// let discord_token = "Your discord token";
///
/// let credentials = ClientCredentials {
/// discord_token,
/// };
///
/// let client = Client::new(credentials);
///
/// client.connect().await
/// ```
///
pub struct Client { pub struct Client {
discord_client: DiscordClient, discord_client: DiscordClient,
} }
pub struct ClientOptions<'a> { /// Yorokobot connection credentials
pub struct ClientCredentials<'a> {
/// Token for Discord API
pub discord_token: &'a String, pub discord_token: &'a String,
} }
impl<'a> Client { impl<'a> Client {
pub async fn new(options: ClientOptions<'a>) -> Client { /// Create a Yorokobot client
let discord_client = DiscordClient::builder(options.discord_token, GatewayIntents::empty()) pub async fn new(credentials: ClientCredentials<'a>) -> Client {
let discord_client =
DiscordClient::builder(credentials.discord_token, GatewayIntents::empty())
.await .await
.expect("Could not create Discord Client"); .expect("Could not create Discord Client");
Client { discord_client } Client { discord_client }
} }
/// Start connection to Discord API.
/// Wrap [`serenity::client::Client`] start method.
pub async fn connect_discord(&mut self) { pub async fn connect_discord(&mut self) {
if let Err(error) = self.discord_client.start().await { if let Err(error) = self.discord_client.start().await {
println!("Could not connect to Discord: {:?}", error); println!("Could not connect to Discord: {:?}", error);
} }
} }
/// Connect client to the Mongo database then to the Discord API.
pub async fn connect(&mut self) {
self.connect_discord().await;
}
} }

View file

@ -1 +1,10 @@
//! Discord bot providing a subscription system to topics.
//! Powered by [`Serenity`]
//!
//! [`Serenity`]: https://github.com/serenity-rs/serenity
#![deny(missing_docs)]
#![deny(warnings)]
/// Module containing the Yorokobot client and used structs
pub mod client; pub mod client;

View file

@ -1,16 +1,16 @@
use std::env; use std::env;
use yorokobot::client::{Client, ClientOptions}; use yorokobot::client::{Client, ClientCredentials};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let discord_token = env::var("DISCORD_TOKEN").expect("Cannot fetch Discord token"); let discord_token = env::var("DISCORD_TOKEN").expect("Cannot fetch Discord token");
let options = ClientOptions { let credentials = ClientCredentials {
discord_token: &discord_token, discord_token: &discord_token,
}; };
let mut client = Client::new(options).await; let mut client = Client::new(credentials).await;
client.connect_discord().await; client.connect_discord().await;
} }