Skip to content
Open
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
14 changes: 14 additions & 0 deletions examples/feature_showcase/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ pub async fn cooldowns(ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}

// Burstable cooldowns
#[poise::command(
prefix_command,
track_edits,
slash_command,
user_cooldown = 20,
user_cooldown_burst = 3
)]
pub async fn burstable_cooldown(ctx: Context<'_>) -> Result<(), Error> {
ctx.say("You successfully called this burstable command")
.await?;
Ok(())
}

#[poise::command(prefix_command, slash_command)]
pub async fn minmax(
ctx: Context<'_>,
Expand Down
1 change: 1 addition & 0 deletions examples/feature_showcase/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async fn main() {
checks::delete(),
checks::ferrisparty(),
checks::cooldowns(),
checks::burstable_cooldown(),
checks::minmax(),
checks::get_guild_name(),
checks::only_in_dms(),
Expand Down
16 changes: 16 additions & 0 deletions macros/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ pub struct CommandArgs {
guild_cooldown: Option<u64>,
channel_cooldown: Option<u64>,
member_cooldown: Option<u64>,

global_cooldown_burst: Option<u64>,
user_cooldown_burst: Option<u64>,
guild_cooldown_burst: Option<u64>,
channel_cooldown_burst: Option<u64>,
member_cooldown_burst: Option<u64>,
}

/// Representation of the function parameter attribute arguments
Expand Down Expand Up @@ -429,18 +435,28 @@ fn generate_cooldown_config(args: &CommandArgs) -> proc_macro2::TokenStream {
let to_seconds_path = quote::quote!(std::time::Duration::from_secs);

let global_cooldown = wrap_option_and_map(args.global_cooldown, &to_seconds_path);
let global_burst = wrap_option(args.global_cooldown_burst);
let user_cooldown = wrap_option_and_map(args.user_cooldown, &to_seconds_path);
let user_burst = wrap_option(args.user_cooldown_burst);
let guild_cooldown = wrap_option_and_map(args.guild_cooldown, &to_seconds_path);
let guild_burst = wrap_option(args.guild_cooldown_burst);
let channel_cooldown = wrap_option_and_map(args.channel_cooldown, &to_seconds_path);
let channel_burst = wrap_option(args.channel_cooldown_burst);
let member_cooldown = wrap_option_and_map(args.member_cooldown, &to_seconds_path);
let member_burst = wrap_option(args.member_cooldown_burst);

quote::quote!(
std::sync::RwLock::new(::poise::CooldownConfig {
global: #global_cooldown,
global_burst_amount: #global_burst,
user: #user_cooldown,
user_burst_amount: #user_burst,
guild: #guild_cooldown,
guild_burst_amount: #guild_burst,
channel: #channel_cooldown,
channel_burst_amount: #channel_burst,
member: #member_cooldown,
member_burst_amount: #member_burst,
__non_exhaustive: ()
})
)
Expand Down
3 changes: 2 additions & 1 deletion macros/src/command/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub fn generate_prefix_action(inv: &Invocation) -> Result<proc_macro2::TokenStre
.unwrap_or_else(|| ctx.framework.options.manual_cooldowns);

if is_framework_cooldown {
ctx.command.cooldowns.lock().unwrap().start_cooldown(ctx.cooldown_context());
let cooldown_config = ctx.command().cooldown_config.read().unwrap();
ctx.command().cooldowns.lock().unwrap().increment_usage(ctx.cooldown_context(), <std::sync::RwLockReadGuard<'_, poise::CooldownConfig> as core::ops::Deref>::deref(&cooldown_config));
}

inner(ctx.into(), #( #param_idents, )* )
Expand Down
3 changes: 2 additions & 1 deletion macros/src/command/slash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ pub fn generate_slash_action(inv: &Invocation) -> Result<proc_macro2::TokenStrea
.unwrap_or_else(|| ctx.framework.options.manual_cooldowns);

if is_framework_cooldown {
ctx.command.cooldowns.lock().unwrap().start_cooldown(ctx.cooldown_context());
let cooldown_config = ctx.command.cooldown_config.read().unwrap();
ctx.command.cooldowns.lock().unwrap().increment_usage(ctx.cooldown_context(), <std::sync::RwLockReadGuard<'_, poise::CooldownConfig> as core::ops::Deref>::deref(&cooldown_config));
}

inner(ctx.into(), #( #param_identifiers, )*)
Expand Down
5 changes: 5 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ for example for command-specific help (i.e. `~help command_name`). Escape newlin
## 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
- `global_cooldown_burst`: The number of times the command can be invoked within the cooldown period, globally. The default is 1.
- `user_cooldown`: Minimum duration in seconds between invocations, per user
- `user_cooldown_burst`: The number of times the command can be invoked within the cooldown period, per user. The default is 1.
- `guild_cooldown`: Minimum duration in seconds between invocations, per guild
- `guild_cooldown_burst`: The number of times the command can be invoked within the cooldown period, per guild. The default is 1.
- `channel_cooldown`: Minimum duration in seconds between invocations, per channel
- `channel_cooldown_burst`: The number of times the command can be invoked within the cooldown period, per channel. The default is 1.
- `member_cooldown`: Minimum duration in seconds between invocations, per guild member
- `member_cooldown_burst`: The number of times the command can be invoked within the cooldown period, per guild member. The default is 1.

## Other

Expand Down
Loading