Skip to content

Commit

Permalink
Make manual cooldowns on commands optional & document
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbt365 committed Jun 4, 2024
1 parent 7af98ac commit 7fcdf3e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
6 changes: 3 additions & 3 deletions macros/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct CommandArgs {
category: Option<String>,
custom_data: Option<syn::Expr>,

manual_cooldowns: bool,
manual_cooldowns: Option<bool>,

// In seconds
global_cooldown: Option<u64>,
Expand Down Expand Up @@ -282,7 +282,7 @@ fn generate_command(mut inv: Invocation) -> Result<proc_macro2::TokenStream, dar
let description = wrap_option_to_string(inv.description.as_ref());
let category = wrap_option_to_string(inv.args.category.as_ref());

let manual_cooldowns = inv.args.manual_cooldowns;
let manual_cooldowns = wrap_option(inv.args.manual_cooldowns);
let cooldown_config = generate_cooldown_config(&inv.args);

let default_member_permissions = &inv.default_member_permissions;
Expand Down Expand Up @@ -418,4 +418,4 @@ fn generate_cooldown_config(args: &CommandArgs) -> proc_macro2::TokenStream {
__non_exhaustive: ()
})
)
}
}
7 changes: 5 additions & 2 deletions macros/src/command/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ pub fn generate_prefix_action(inv: &Invocation) -> Result<proc_macro2::TokenStre
error,
))?;

if !ctx.framework.options.manual_cooldowns && !ctx.command.manual_cooldowns {
let is_framework_cooldown = ctx.command.manual_cooldowns
.unwrap_or_else(|| ctx.framework.options.manual_cooldowns);

if is_framework_cooldown {
ctx.command.cooldowns.lock().unwrap().start_cooldown(ctx.cooldown_context());
}

inner(ctx.into(), #( #param_idents, )* )
.await
.map_err(|error| poise::FrameworkError::new_command(
Expand Down
10 changes: 8 additions & 2 deletions macros/src/command/slash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ pub fn generate_slash_action(inv: &Invocation) -> Result<proc_macro2::TokenStrea
#( (#param_names: #param_types), )*
).await.map_err(|error| error.to_framework_error(ctx))?;

if !ctx.framework.options.manual_cooldowns && !ctx.command.manual_cooldowns {
let is_framework_cooldown = ctx.command.manual_cooldowns
.unwrap_or_else(|| ctx.framework.options.manual_cooldowns);

if is_framework_cooldown {
ctx.command.cooldowns.lock().unwrap().start_cooldown(ctx.cooldown_context());
}

Expand Down Expand Up @@ -215,7 +218,10 @@ pub fn generate_context_menu_action(
Ok(quote::quote! {
<#param_type as ::poise::ContextMenuParameter<_, _>>::to_action(|ctx, value| {
Box::pin(async move {
if !ctx.framework.options.manual_cooldowns && !ctx.command.manual_cooldowns {
let is_framework_cooldown = ctx.command.manual_cooldowns
.unwrap_or_else(|| ctx.framework.options.manual_cooldowns);

if is_framework_cooldown {
ctx.command.cooldowns.lock().unwrap().start_cooldown(ctx.cooldown_context());
}

Expand Down
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ for example for command-specific help (i.e. `~help command_name`). Escape newlin
- `reuse_response`: After the first response, post subsequent responses as edits to the initial message (prefix only)
## Cooldown
- `manual_cooldowns`: Allows overriding the framework's built-in cooldowns tracking without affecting other commands.
- `global_cooldown`: Minimum duration in seconds between invocations, globally
- `user_cooldown`: Minimum duration in seconds between invocations, per user
- `guild_cooldown`: Minimum duration in seconds between invocations, per guild
Expand Down
2 changes: 1 addition & 1 deletion src/structs/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct Command<U, E> {
///
/// Will override [`crate::FrameworkOptions::manual_cooldowns`] allowing manual cooldowns
/// on select commands.
pub manual_cooldowns: bool,
pub manual_cooldowns: Option<bool>,
/// Handles command cooldowns. Mainly for framework internal use
pub cooldowns: std::sync::Mutex<crate::CooldownTracker>,
/// Configuration for the [`crate::CooldownTracker`]
Expand Down

0 comments on commit 7fcdf3e

Please sign in to comment.