Skip to content

Commit

Permalink
Add support for modifying the last invocation of a command
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbt365 committed May 20, 2024
1 parent 5b369bb commit 39f4caf
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/cooldown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ pub struct CooldownTracker {
member_invocations: HashMap<(serenity::UserId, serenity::GuildId), Instant>,
}

/// Possible types of command cooldowns.
///
/// Currently used for [CooldownTracker::set_last_invocation]
#[non_exhaustive]
pub enum CooldownType {
/// A global cooldown that applies to all users, channels, and guilds.
Global,
/// A cooldown specific to individual users.
User(serenity::UserId),
/// A cooldown that applies to an entire guild.
Guild(serenity::GuildId),
/// A cooldown specific to individual channels.
Channel(serenity::ChannelId),
/// A cooldown specific to individual members within a guild.
Member((serenity::UserId, serenity::GuildId)),
}

/// **Renamed to [`CooldownTracker`]**
pub use CooldownTracker as Cooldowns;

Expand Down Expand Up @@ -122,6 +139,24 @@ impl CooldownTracker {
self.member_invocations.insert((ctx.user_id, guild_id), now);
}
}
/// Sets the last invocation for the specified cooldown bucket.
pub fn set_last_invocation(&mut self, cooldown_type: CooldownType, instant: Instant) {
match cooldown_type {
CooldownType::Global => self.global_invocation = Some(instant),
CooldownType::User(user_id) => {
self.user_invocations.insert(user_id, instant);
}
CooldownType::Guild(guild_id) => {
self.guild_invocations.insert(guild_id, instant);
}
CooldownType::Channel(channel_id) => {
self.channel_invocations.insert(channel_id, instant);
}
CooldownType::Member(member) => {
self.member_invocations.insert(member, instant);
}
}
}
}

impl<'a> From<&'a serenity::Message> for CooldownContext {
Expand Down

0 comments on commit 39f4caf

Please sign in to comment.