Skip to content

Commit

Permalink
Add support for using manual cooldowns on single commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbt365 committed May 20, 2024
1 parent 5b369bb commit 4025afa
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion macros/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions macros/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub struct CommandArgs {
category: Option<String>,
custom_data: Option<syn::Expr>,

manual_cooldowns: bool,

// In seconds
global_cooldown: Option<u64>,
user_cooldown: Option<u64>,
Expand Down Expand Up @@ -280,6 +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 cooldown_config = generate_cooldown_config(&inv.args);

let default_member_permissions = &inv.default_member_permissions;
Expand Down Expand Up @@ -354,6 +357,7 @@ fn generate_command(mut inv: Invocation) -> Result<proc_macro2::TokenStream, dar
description_localizations: #description_localizations,
help_text: #help_text,
hide_in_help: #hide_in_help,
manual_cooldowns: #manual_cooldowns,
cooldowns: std::sync::Mutex::new(::poise::Cooldowns::new()),
cooldown_config: #cooldown_config,
reuse_response: #reuse_response,
Expand Down
2 changes: 1 addition & 1 deletion macros/src/command/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn generate_prefix_action(inv: &Invocation) -> Result<proc_macro2::TokenStre
error,
))?;

if !ctx.framework.options.manual_cooldowns {
if !ctx.framework.options.manual_cooldowns && !ctx.command.manual_cooldowns {
ctx.command.cooldowns.lock().unwrap().start_cooldown(ctx.cooldown_context());
}

Expand Down
4 changes: 2 additions & 2 deletions macros/src/command/slash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ 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 {
if !ctx.framework.options.manual_cooldowns && !ctx.command.manual_cooldowns {
ctx.command.cooldowns.lock().unwrap().start_cooldown(ctx.cooldown_context());
}

Expand Down Expand Up @@ -215,7 +215,7 @@ 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 {
if !ctx.framework.options.manual_cooldowns && !ctx.command.manual_cooldowns {
ctx.command.cooldowns.lock().unwrap().start_cooldown(ctx.cooldown_context());
}

Expand Down
5 changes: 5 additions & 0 deletions src/structs/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ pub struct Command<U, E> {
/// Multiline description with detailed usage instructions. Displayed in the command specific
/// help: `~help command_name`
pub help_text: Option<String>,
/// if `true`, disables automatic cooldown handling before this commands invocation.
///
/// Will override [`crate::FrameworkOptions::manual_cooldowns`] allowing manual cooldowns
/// on select commands.
pub manual_cooldowns: 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 4025afa

Please sign in to comment.