Skip to content

Commit

Permalink
Migrate cooldowns to new version
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 26, 2023
1 parent 1cbfeef commit 56e2b74
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 32 deletions.
2 changes: 1 addition & 1 deletion examples/advanced_cooldowns/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn dynamic_cooldowns(ctx: Context<'_>) -> Result<(), Error> {
cooldown_durations.user = Some(std::time::Duration::from_secs(10));
}

match cooldown_tracker.remaining_cooldown_2(ctx, &cooldown_durations) {
match cooldown_tracker.remaining_cooldown(ctx, &cooldown_durations) {
Some(remaining) => {
return Err(format!("Please wait {} seconds", remaining.as_secs()).into())
}
Expand Down
7 changes: 4 additions & 3 deletions macros/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,15 @@ 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,
cooldowns: std::sync::Mutex::new(::poise::Cooldowns::new(::poise::CooldownConfig {
cooldowns: std::sync::Mutex::new(::poise::Cooldowns::new()),
cooldown_config: std::sync::RwLock::new(::poise::CooldownConfig {
global: #global_cooldown.map(std::time::Duration::from_secs),
user: #user_cooldown.map(std::time::Duration::from_secs),
guild: #guild_cooldown.map(std::time::Duration::from_secs),
channel: #channel_cooldown.map(std::time::Duration::from_secs),
member: #member_cooldown.map(std::time::Duration::from_secs),
__non_exhaustive: (),
})),
__non_exhaustive: ()
}),
reuse_response: #reuse_response,
default_member_permissions: #default_member_permissions,
required_permissions: #required_permissions,
Expand Down
29 changes: 2 additions & 27 deletions src/cooldown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ pub struct CooldownConfig {
/// cooldown handler.
#[derive(Default, Clone, Debug, PartialEq, Eq, Hash)]
pub struct CooldownTracker {
/// Stores the cooldown durations
/// Will be removed in next version in favor of passing the config on demand to functions
cooldown: CooldownConfig,

/// Stores the timestamp of the last global invocation
global_invocation: Option<Instant>,
/// Stores the timestamps of the last invocation per user
Expand All @@ -49,24 +45,8 @@ pub use CooldownTracker as Cooldowns;

impl CooldownTracker {
/// Create a new cooldown tracker
pub fn new_2() -> Self {
Self {
// Removed in next version; unused by new API
cooldown: CooldownConfig::default(),

global_invocation: None,
user_invocations: OrderedMap::new(),
guild_invocations: OrderedMap::new(),
channel_invocations: OrderedMap::new(),
member_invocations: OrderedMap::new(),
}
}

/// **Will be replaced by [`Self::new_2()`] in the next breaking version**
pub fn new(config: CooldownConfig) -> Self {
pub fn new() -> Self {
Self {
cooldown: config,

global_invocation: None,
user_invocations: OrderedMap::new(),
guild_invocations: OrderedMap::new(),
Expand All @@ -77,7 +57,7 @@ impl CooldownTracker {

/// Queries the cooldown buckets and checks if all cooldowns have expired and command
/// execution may proceed. If not, Some is returned with the remaining cooldown
pub fn remaining_cooldown_2<U, E>(
pub fn remaining_cooldown<U, E>(
&self,
ctx: crate::Context<'_, U, E>,
cooldown_durations: &CooldownConfig,
Expand Down Expand Up @@ -117,11 +97,6 @@ impl CooldownTracker {
.max()
}

/// **Will be replaced by [`Self::remaining_cooldown_2`] in the next breaking version**
pub fn remaining_cooldown<U, E>(&self, ctx: crate::Context<'_, U, E>) -> Option<Duration> {
self.remaining_cooldown_2(ctx, &self.cooldown)
}

/// Indicates that a command has been executed and all associated cooldowns should start running
pub fn start_cooldown<U, E>(&mut self, ctx: crate::Context<'_, U, E>) {
let now = Instant::now();
Expand Down
3 changes: 2 additions & 1 deletion src/dispatch/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ async fn check_permissions_and_cooldown_single<'a, U, E>(

if !ctx.framework().options().manual_cooldowns {
let cooldowns = &cmd.cooldowns;
let remaining_cooldown = cooldowns.lock().unwrap().remaining_cooldown(ctx);
let config = cmd.cooldown_config.read().unwrap();
let remaining_cooldown = cooldowns.lock().unwrap().remaining_cooldown(ctx, &config);
if let Some(remaining_cooldown) = remaining_cooldown {
return Err(crate::FrameworkError::CooldownHit {
ctx,
Expand Down
2 changes: 2 additions & 0 deletions src/structs/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub struct Command<U, E> {
pub help_text: Option<String>,
/// Handles command cooldowns. Mainly for framework internal use
pub cooldowns: std::sync::Mutex<crate::CooldownTracker>,
/// Configuration for the [`crate::CooldownTracker`]
pub cooldown_config: std::sync::RwLock<crate::CooldownConfig>,
/// After the first response, whether to post subsequent responses as edits to the initial
/// message
///
Expand Down

0 comments on commit 56e2b74

Please sign in to comment.