Cooldown Attribute
You can apply cooldowns to your application commands with the SlashCommandCooldownAttribute & ContextMenuCooldownAttribute.
The cooldown attributes consists of three required parameters and one optional parameter.
int maxUses
- The number of times a command can be used during the definied time before being put on cooldown.double resetAfter
- After how many seconds the cooldown resets.CooldownBucketType bucketType
- The type of bucket to use. Can be combined.
Usage
// We create a slash command which sends a meow to the channel. This command can be executed twice every 40 seconds per channel.
[SlashCommand("meow", "Meow at chat"), SlashCommandCooldown(2, 40, CooldownBucketType.Channel)]
public async Task MeowAsync(InteractionContext ctx)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent("Meow!"));
}
Visual Example
Please note that the timestamp would show the actual time of the cooldown reset. We can't dynamically change the timestamp in the example, so we just use a static one.
Try again
Customizing the cooldown hit response
You can customize the response that is sent when a user hits a cooldown by creating a custom class which inherits from ICooldownResponder.
public sealed class CooldownResponse : ICooldownResponder
{
/// <inheritdoc />
public async Task Responder(BaseContext context)
{
await context.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent("You hit a cooldown! Try again later.").AsEphemeral());
}
}
You can then apply this to your command by using the cooldownResponderType
property on the attribute.
[SlashCommand("meow", "Meow at chat"), SlashCommandCooldown(2, 40, CooldownBucketType.Channel, typeof(CooldownResponse))]
public async Task CooldownTestAsync(InteractionContext ctx)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent("Meow!"));
}