Set discord token fetching into library
This commit is contained in:
parent
b03625443a
commit
d4ef9632da
|
@ -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<Client, ClientError> {
|
||||
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<Client, ClientError> {
|
||||
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(())
|
||||
}
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
mod client;
|
||||
pub mod client;
|
||||
mod event_handler;
|
||||
|
|
|
@ -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<Client>,
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
13
src/main.rs
13
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,
|
||||
|
|
Loading…
Reference in a new issue