Cooldowns
in package
How long somebody must wait before running the same command again.
Each Action declares its own cooldown, and it used to be enforced by
whichever command client registered it — DiscordPHP's for prefix commands,
TwitchPHP's for chat. Once every surface routes through one qualifier per
connector, neither client sees the individual commands any more, so the
cooldown has to live somewhere every adapter can reach. This is that place,
and it is the same on every surface: !twitch clip on Twitch and
/twitch stream clip on Discord share one clock.
Per person, per command. A per-command cooldown shared by everyone would let one person lock a command for a whole chat.
The clock is injected, so none of this sleeps in a test.
Tags
Table of Contents
Constants
- SWEEP_AT : mixed = 1000
- Past this many entries, expired ones are swept on the next claim.
Properties
Methods
- __construct() : mixed
- claim() : int
- Claims a run of `$action` for `$invoker`, or says how long is left.
- count() : int
- How many cooldowns are being tracked, for tests.
Constants
SWEEP_AT
Past this many entries, expired ones are swept on the next claim.
private
mixed
SWEEP_AT
= 1000
Properties
$clock read-only
private
callable(): float
$clock
$until
private
array<string, float>
$until
= []
key => when the cooldown ends
Methods
__construct()
public
__construct([callable(): float|null $clock = null ]) : mixed
Parameters
- $clock : callable(): float|null = null
-
Defaults to
microtime(true).
claim()
Claims a run of `$action` for `$invoker`, or says how long is left.
public
claim(Action $action, string $invoker) : int
Returns 0 and starts the cooldown when the command may run now; returns
the whole seconds remaining, rounded up, when it may not — and does not
restart the clock, so somebody hammering a command is not punished with
an ever-longer wait.
Parameters
- $action : Action
- $invoker : string
Return values
intcount()
How many cooldowns are being tracked, for tests.
public
count() : int