Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using manual cooldowns on single commands #269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Option<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 = 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 @@ -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
5 changes: 4 additions & 1 deletion macros/src/command/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ pub fn generate_prefix_action(inv: &Invocation) -> Result<proc_macro2::TokenStre
error,
))?;

if !ctx.framework.options.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
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 {
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 {
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
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: Option<bool>,
/// Handles command cooldowns. Mainly for framework internal use
pub cooldowns: std::sync::Mutex<crate::CooldownTracker>,
/// Configuration for the [`crate::CooldownTracker`]
Expand Down
Loading