Skip to main content

Cooldowns

A cooldown stops somebody using a command again too soon. Without one, a /daily command can be run a hundred times a second, and a command that calls an API will get you rate limited.

The Cooldowns category

The easy way​

Create cooldown

create cooldown does the whole job in one block. Give it:

FieldWhat it is
Command nameA label for this cooldown. Any text, as long as you use the same one everywhere.
Duration (ms)How long the cooldown lasts, in milliseconds.
For userWho the cooldown applies to.
then runThe blocks that only run when the user is not on cooldown.

If the user is on cooldown, nothing inside then run happens. That is usually not enough on its own, because the user gets no feedback, so most commands pair it with the check block below.

Checking manually​

BlockReturns
is on cooldownTrue when the user is currently on cooldown for that command.
cooldown leftHow many milliseconds are left.

The usual pattern is:

  1. if is user ... on cooldown for command ...
  2. Reply with how long is left, formatted with turn milliseconds to time string from Time.
  3. else, do the work and set cooldown.

Setting and clearing​

BlockWhat it does
set cooldownPuts a user on cooldown for a command.
clear cooldownEnds one user's cooldown for one command.
clear allEnds every cooldown for everyone.

clear cooldown is useful for an admin command that lets staff reset somebody's daily, or for giving the cooldown back when a command failed halfway through.

Per user, per server, or global​

The "for user" input is just an identifier. Feed it something different and the cooldown means something different:

Plug inEffect
user of the interactionOne cooldown per person.
ID of serverOne cooldown for the whole server.
A fixed piece of textOne global cooldown for everyone.
create text with the user ID and the server IDPer person, per server.

Cooldowns are not saved​

Cooldowns live in the bot's memory. When the bot restarts, every cooldown is cleared. For a /daily command where that matters, store the last-used timestamp in a database instead and compare it against the current time.

A worked example: a daily reward​

  1. when a slash command is received, name is daily.
  2. if is user <user of the interaction> on cooldown for command daily:
    • Reply, visible only to the user, with "Come back in " and cooldown left ... formatted as a time string.
  3. else:
    • Add the reward to their balance in the database.
    • set cooldown on command daily to duration 86400000 (24 hours) for that user.
    • Reply with what they got.