Skip to content

Commit a8f5a7f

Browse files
feat: add reset_with_key and get_cooldown_time_with_key (#1345)
* made get_cooldown_with_key async for consistency, added reset_with_key and get_cooldown_time_with_key * ci: correct from checks. * fixed pre-commit problems * comment on #1345, resolved * made Cooldown.reset_all async #1345 * bot.load => bot.load_extension * reverted 8217f4e * made get_cooldown_time_with_key and reset_with_key sync --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 47319ed commit a8f5a7f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

interactions/models/internal/cooldowns.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,22 @@ async def get_cooldown_time(self, context: "BaseContext") -> float:
387387
cooldown = await self.get_cooldown(context)
388388
return cooldown.get_cooldown_time()
389389

390+
def get_cooldown_time_with_key(self, key: Any, *, create: bool = False) -> float:
391+
"""
392+
Get the remaining cooldown time with a key instead of the context.
393+
394+
Note:
395+
The preferred way to get the cooldown system is to use `get_cooldown` as it will use the context to get the correct key.
396+
397+
Args:
398+
key: The key to get the cooldown system for
399+
create: Whether to create a new cooldown system if one does not exist
400+
"""
401+
cooldown = self.get_cooldown_with_key(key, create=create)
402+
if cooldown is not None:
403+
return cooldown.get_cooldown_time()
404+
return 0
405+
390406
async def on_cooldown(self, context: "BaseContext") -> bool:
391407
"""
392408
Returns the cooldown state of the command.
@@ -423,6 +439,25 @@ async def reset(self, context: "BaseContext") -> None:
423439
cooldown = await self.get_cooldown(context)
424440
cooldown.reset()
425441

442+
def reset_with_key(self, key: Any) -> bool:
443+
"""
444+
Resets the cooldown for the bucket associated with the provided key.
445+
446+
Note:
447+
The preferred way to reset the cooldown system is to use `reset_cooldown` as it will use the context to reset the correct cooldown.
448+
449+
Args:
450+
key: The key to reset the cooldown system for
451+
452+
Returns:
453+
True if the key existed and was reset successfully, False if the key didn't exist.
454+
"""
455+
cooldown = self.get_cooldown_with_key(key)
456+
if cooldown is not None:
457+
cooldown.reset()
458+
return True
459+
return False
460+
426461

427462
class MaxConcurrency:
428463
"""

0 commit comments

Comments
 (0)