-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: moved command authentication to a macro template
- made sure different type of commands can use them - improved command management
- Loading branch information
Showing
5 changed files
with
38 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// commands/mod.rs | ||
|
||
pub mod auth; | ||
pub mod ping; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters