diff --git a/discord/src/commands/auth.rs b/discord/src/commands/auth.rs new file mode 100644 index 0000000..312f835 --- /dev/null +++ b/discord/src/commands/auth.rs @@ -0,0 +1,32 @@ +// commands/auth.rs + +use crate::events::connection::handle_user_connection; +use crate::types::{Context, Error}; +use anyhow::Result; + +// Wrapper function that handles the authentication check +pub async fn with_auth<'a, F, Fut>(ctx: Context<'a>, f: F) -> Result<(), Error> +where + F: FnOnce(Context<'a>) -> Fut, + Fut: std::future::Future> + 'a, +{ + // Check if the user is already registered + if let Err(e) = handle_user_connection(ctx.clone()).await { + ctx.say("Error handling user connection").await?; + return Err(e); + } + + // Execute the wrapped command + f(ctx).await +} + +// Macro to reduce boilerplate even further +#[macro_export] +macro_rules! command_with_auth { + ($name:ident, $cmd:expr) => { + #[poise::command(slash_command, prefix_command)] + pub async fn $name(ctx: Context<'_>) -> Result<(), Error> { + crate::commands::auth::with_auth(ctx, $cmd).await + } + }; +} diff --git a/discord/src/commands/mod.rs b/discord/src/commands/mod.rs index d9e90a1..06b7262 100644 --- a/discord/src/commands/mod.rs +++ b/discord/src/commands/mod.rs @@ -1,3 +1,4 @@ // commands/mod.rs +pub mod auth; pub mod ping; diff --git a/discord/src/commands/ping.rs b/discord/src/commands/ping.rs index d5cd34f..b3b5b9e 100644 --- a/discord/src/commands/ping.rs +++ b/discord/src/commands/ping.rs @@ -1,22 +1,10 @@ // commands/ping.rs - -use crate::events::connection::handle_user_connection; use crate::services::api_client; use crate::types::{Context, Error}; +use crate::command_with_auth; use anyhow::Result; -// Fetch data from the `/ping` endpoint -#[poise::command(slash_command, prefix_command)] -pub async fn ping(ctx: Context<'_>) -> Result<(), Error> { - - // Check if the user is already registered - if let Err(e) = handle_user_connection(ctx).await { - // Handle error and notify the user - ctx.say("Error handling user connection").await?; - return Err(e); // Propagate the error from handle_user_connection - } - - // Call the API client to make a GET request to `/ping` +async fn ping_handler(ctx: Context<'_>) -> Result<(), Error> { match api_client::get("ping").await { Ok(data) => { ctx.say(data.to_string()).await?; @@ -27,3 +15,5 @@ pub async fn ping(ctx: Context<'_>) -> Result<(), Error> { } Ok(()) } + +command_with_auth!(ping, ping_handler); diff --git a/discord/src/events/mod.rs b/discord/src/events/mod.rs index 4c40c25..528a805 100644 --- a/discord/src/events/mod.rs +++ b/discord/src/events/mod.rs @@ -1,5 +1,4 @@ // events/mod.rs - pub mod connection; pub mod error; pub mod ready; @@ -21,10 +20,6 @@ pub async fn handle_event( serenity::FullEvent::Message { new_message } => { println!("Message received: {:?}", new_message.content); } - serenity::FullEvent::TypingStart { event } => { - println!("User typing: {:?}", event.user_id); - } - // Add more event cases here, e.g. Message, Reaction, etc. _ => {} } Ok(()) diff --git a/discord/src/events/ready.rs b/discord/src/events/ready.rs index 5597b38..ea7d761 100644 --- a/discord/src/events/ready.rs +++ b/discord/src/events/ready.rs @@ -4,5 +4,5 @@ use poise::serenity_prelude::{Context, Ready}; // This function is called when the bot is ready to start receiving events pub async fn on_ready(_ctx: Context, ready: Ready) { - println!("{} is connected!", ready.user.name); + println!("[{}] is connected!", ready.user.name); }