Cooldown Attribute
You can apply cooldowns to your text commands with the CooldownAttribute.
The cooldown attribute 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 text command which sends a meow to the channel. This command can be executed twice every 40 seconds per channel.
[Command("meow"), Description("Meow at chat"), Cooldown(2, 40, CooldownBucketType.Channel)]
public async Task MeowAsync(CommandContext ctx)
{
await context.RespondAsync("Meow!");
}
Visual Example
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.Message.CreateReactionAsync(DiscordEmoji.FromGuildEmote(context.Client, 972904417005293609));
// or send a message
// await context.Member.SendMessageAsync("You hit a cooldown! Try again later.");
}
}
You can then apply this to your command by using the cooldownResponderType property on the attribute.
[Command("meow"), Description("Meow at chat"), Cooldown(2, 40, CooldownBucketType.Channel, typeof(CooldownResponse)))]
public async Task MeowAsync(CommandContext ctx)
{
await context.RespondAsync("Meow!");
}