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 modifying the last invocation of a command #270

Merged
merged 2 commits into from
Jun 4, 2024
Merged
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
40 changes: 40 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,29 @@ impl CooldownTracker {
self.member_invocations.insert((ctx.user_id, guild_id), now);
}
}

/// Sets the last invocation for the specified cooldown bucket.
jamesbt365 marked this conversation as resolved.
Show resolved Hide resolved
///
/// This function is not usually needed for regular usage. It was added to allow for extra
/// flexibility in cases where you might want to shorten or lengthen a cooldown after
/// invocation.
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
Loading