mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-18 23:15:48 +00:00
[commands] Allow subtracting multiple tokens from cooldown
This commit is contained in:
parent
832d2c0542
commit
0e5c4c391d
@ -112,7 +112,8 @@ class Cooldown:
|
|||||||
if not current:
|
if not current:
|
||||||
current = time.time()
|
current = time.time()
|
||||||
|
|
||||||
tokens = self._tokens
|
# the calculated tokens should be non-negative
|
||||||
|
tokens = max(self._tokens, 0)
|
||||||
|
|
||||||
if current > self._window + self.per:
|
if current > self._window + self.per:
|
||||||
tokens = self.rate
|
tokens = self.rate
|
||||||
@ -140,7 +141,7 @@ class Cooldown:
|
|||||||
|
|
||||||
return 0.0
|
return 0.0
|
||||||
|
|
||||||
def update_rate_limit(self, current: Optional[float] = None) -> Optional[float]:
|
def update_rate_limit(self, current: Optional[float] = None, *, tokens: int = 1) -> Optional[float]:
|
||||||
"""Updates the cooldown rate limit.
|
"""Updates the cooldown rate limit.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@ -148,6 +149,8 @@ class Cooldown:
|
|||||||
current: Optional[:class:`float`]
|
current: Optional[:class:`float`]
|
||||||
The time in seconds since Unix epoch to update the rate limit at.
|
The time in seconds since Unix epoch to update the rate limit at.
|
||||||
If not supplied, then :func:`time.time()` is used.
|
If not supplied, then :func:`time.time()` is used.
|
||||||
|
tokens: :class:`int`
|
||||||
|
The amount of tokens to deduct from the rate limit.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
@ -163,12 +166,12 @@ class Cooldown:
|
|||||||
if self._tokens == self.rate:
|
if self._tokens == self.rate:
|
||||||
self._window = current
|
self._window = current
|
||||||
|
|
||||||
# check if we are rate limited
|
# decrement tokens by specified number
|
||||||
if self._tokens == 0:
|
self._tokens -= tokens
|
||||||
return self.per - (current - self._window)
|
|
||||||
|
|
||||||
# we're not so decrement our tokens
|
# check if we are rate limited and return retry-after
|
||||||
self._tokens -= 1
|
if self._tokens < 0:
|
||||||
|
return self.per - (current - self._window)
|
||||||
|
|
||||||
def reset(self) -> None:
|
def reset(self) -> None:
|
||||||
"""Reset the cooldown to its initial state."""
|
"""Reset the cooldown to its initial state."""
|
||||||
|
@ -140,9 +140,9 @@ class CooldownMapping:
|
|||||||
|
|
||||||
return bucket
|
return bucket
|
||||||
|
|
||||||
def update_rate_limit(self, message: Message, current: Optional[float] = None) -> Optional[float]:
|
def update_rate_limit(self, message: Message, current: Optional[float] = None, tokens: int = 1) -> Optional[float]:
|
||||||
bucket = self.get_bucket(message, current)
|
bucket = self.get_bucket(message, current)
|
||||||
return bucket.update_rate_limit(current)
|
return bucket.update_rate_limit(current, tokens=tokens)
|
||||||
|
|
||||||
|
|
||||||
class DynamicCooldownMapping(CooldownMapping):
|
class DynamicCooldownMapping(CooldownMapping):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user