Skip to content

Commit

Permalink
Refactor create_client to return Result<Client> instead of Client
Browse files Browse the repository at this point in the history
  • Loading branch information
kasugamirai committed Aug 23, 2024
1 parent e7f63f5 commit 843bccf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/bin/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn main() {
};

// Create a new client with the discord token
let mut client = create_client(
let client_result = create_client(
&get_env(
"DISCORD_TOKEN",
"Expected a discord token in the environment",
Expand All @@ -37,6 +37,14 @@ async fn main() {
)
.await;

let mut client = match client_result {
Ok(client) => client,
Err(e) => {
error!("Error creating client: {}", e);
return;
}
};

// Start listening for events
info!("Bot is now running. Press Ctrl+C to stop.");
if let Err(why) = client.start().await {
Expand Down
12 changes: 7 additions & 5 deletions src/discord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ use tracing::{debug, error};
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
ModelConfigurationBuilderError(#[from] chatgpt::config::ModelConfigurationBuilderError),
ModelConfigurationBuilder(#[from] chatgpt::config::ModelConfigurationBuilderError),
#[error(transparent)]
ChatGPT(#[from] chatgpt::err::Error),
#[error(transparent)]
Client(#[from] serenity::Error),
}

type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -143,9 +145,9 @@ pub async fn create_client(
discord_token: &str,
intents: GatewayIntents,
handler: Handler,
) -> Client {
Client::builder(discord_token, intents)
) -> Result<Client> {
let ret = Client::builder(discord_token, intents)
.event_handler(handler)
.await
.expect("Error creating client")
.await?;
Ok(ret)
}

0 comments on commit 843bccf

Please sign in to comment.