If your commands perform costly operations or can potentially be abused, you may want to set a limit on the number of times the command can be used. The Cooldown API will allow you to deal with these situations without hassle.
cooldown() method from
InteractionListener, and specify the cooldown using
The integer represents the maximum number of times the command can be executed within a timeframe, and the duration represents the timeframe itself. Therefore, this example corresponds to a cooldown of once per minute.
Cooldowns are applied per user. Two different users using the same command will always have their own separate usage limits.
When a user reaches the maximum number of permits and attempts to use the command again, the command will fail with a
CooldownException. You can handle this exception via the command error handler, which will be covered in the next section.