From ef3561d86bd51b695ef48ef6209346d0c7a87246 Mon Sep 17 00:00:00 2001 From: Vilmos Zsombor TANCZOS Date: Fri, 2 May 2025 16:07:31 +0200 Subject: [PATCH] extract DiscordClient logic to it's own struct --- src/client.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 58 +++----------------------------------------- 2 files changed, 71 insertions(+), 54 deletions(-) create mode 100644 src/client.rs diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..54c4d19 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,67 @@ +use poise::PrefixFrameworkOptions; + +use crate::{commands, prelude::*}; + +pub struct DiscordClient { + client: serenity::Client, +} + +impl DiscordClient { + pub async fn new(token: &str) -> Result { + let intents = + serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT; + + let framework = poise::Framework::builder() + .options(poise::FrameworkOptions { + commands: commands::get(), + prefix_options: PrefixFrameworkOptions { + prefix: Some(",".into()), + ..Default::default() + }, + on_error: |error| Box::pin(on_error(error)), + ..Default::default() + }) + .setup(|ctx, ready, framework| { + Box::pin(async move { + info!("Logged in as {}", ready.user.name); + poise::builtins::register_globally(ctx, &framework.options().commands).await?; + Ok(KBotData {}) + }) + }) + .build(); + + let client = serenity::ClientBuilder::new(token, intents) + .framework(framework) + .await?; + Ok(Self { client }) + } + + pub async fn start(&mut self) -> Result<(), serenity::Error> { + self.client.start().await + } +} + +async fn on_error(error: poise::FrameworkError<'_, KBotData, Error>) { + match error { + poise::FrameworkError::Command { error, ctx, .. } => { + let error = error; + error!("Error in command `{}`: {}", ctx.command().name, error,); + let _ = ctx.say("Failed to run command.".to_string()).await; + } + // poise::FrameworkError::ArgumentParse { + // error, input, ctx, .. + // } => { + // debug!( + // "Failed to parse argument `{:?}` for command `{}`: {}", + // input, + // ctx.command().name, + // error + // ); + // } + error => { + if let Err(e) = poise::builtins::on_error(error).await { + error!("Error while handling error: {}", e); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 1c1ea92..743258a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ -use poise::PrefixFrameworkOptions; -use tracing::{error, info}; +use client::DiscordClient; mod checks; +mod client; mod commands; pub mod prelude { @@ -10,8 +10,8 @@ pub mod prelude { pub type Error = Box; pub type Context<'a> = poise::Context<'a, KBotData, Error>; pub type CommandResult = Result<(), Error>; + pub use tracing::{debug, error, info, trace, warn}; } -use prelude::*; #[tokio::main] async fn main() { @@ -20,56 +20,6 @@ async fn main() { dotenv::dotenv().ok(); let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"); - - let intents = - serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT; - - let framework = poise::Framework::builder() - .options(poise::FrameworkOptions { - commands: commands::get(), - prefix_options: PrefixFrameworkOptions { - prefix: Some(",".into()), - ..Default::default() - }, - on_error: |error| Box::pin(on_error(error)), - ..Default::default() - }) - .setup(|ctx, ready, framework| { - Box::pin(async move { - info!("Logged in as {}", ready.user.name); - poise::builtins::register_globally(ctx, &framework.options().commands).await?; - Ok(KBotData {}) - }) - }) - .build(); - - let client = serenity::ClientBuilder::new(token, intents) - .framework(framework) - .await; + let client = DiscordClient::new(&token).await; client.unwrap().start().await.unwrap(); } - -async fn on_error(error: poise::FrameworkError<'_, KBotData, Error>) { - match error { - poise::FrameworkError::Command { error, ctx, .. } => { - let error = error; - error!("Error in command `{}`: {}", ctx.command().name, error,); - let _ = ctx.say("Failed to run command.".to_string()).await; - } - // poise::FrameworkError::ArgumentParse { - // error, input, ctx, .. - // } => { - // debug!( - // "Failed to parse argument `{:?}` for command `{}`: {}", - // input, - // ctx.command().name, - // error - // ); - // } - error => { - if let Err(e) = poise::builtins::on_error(error).await { - error!("Error while handling error: {}", e); - } - } - } -}