Skip to content

Commit

Permalink
refactor: moved command authentication to a macro template
Browse files Browse the repository at this point in the history
- made sure different type of commands can use them

- improved command management
  • Loading branch information
miguelcsx committed Nov 7, 2024
1 parent 20bd37f commit 642e1e7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
32 changes: 32 additions & 0 deletions discord/src/commands/auth.rs
Original file line number Diff line number Diff line change
@@ -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<Output = Result<(), Error>> + '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
}
};
}
1 change: 1 addition & 0 deletions discord/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// commands/mod.rs

pub mod auth;
pub mod ping;
18 changes: 4 additions & 14 deletions discord/src/commands/ping.rs
Original file line number Diff line number Diff line change
@@ -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?;
Expand All @@ -27,3 +15,5 @@ pub async fn ping(ctx: Context<'_>) -> Result<(), Error> {
}
Ok(())
}

command_with_auth!(ping, ping_handler);
5 changes: 0 additions & 5 deletions discord/src/events/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// events/mod.rs

pub mod connection;
pub mod error;
pub mod ready;
Expand All @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion discord/src/events/ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 642e1e7

Please sign in to comment.