Skip to content

Commit

Permalink
Merge branch 'single-command-manual-cooldown' into serenity-next-merged
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbt365 committed Jun 14, 2024
2 parents cd3d50b + 2064440 commit 785244f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 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.

3 changes: 3 additions & 0 deletions macros/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct CommandArgs {
install_context: Option<syn::punctuated::Punctuated<syn::Ident, syn::Token![|]>>,
#[cfg(feature = "unstable")]
interaction_context: Option<syn::punctuated::Punctuated<syn::Ident, syn::Token![|]>>,
manual_cooldowns: Option<bool>,

// In seconds
global_cooldown: Option<u64>,
Expand Down Expand Up @@ -325,6 +326,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 @@ -462,6 +464,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 @@ -69,7 +69,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 @@ -63,6 +63,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

0 comments on commit 785244f

Please sign in to comment.