I'm making hazard/fantasy discord bot for friends and as a project for school. So I got blocked by cooldowns. Currently if player uses /blackjack with amount lower than user have it displays appropriate error but still giving cooldown for that user. I tried to find answer in C# but there ain't any. I just don't get how cooldowns are stored on Discord or how could I make my own attribute to save on local database. Hope to get some directions :D
[SlashCommand("blackjack", "Play a game of blackjack!")]
[SlashCooldown(1, 60 * 5, SlashCooldownBucketType.User)]
public async Task Blackjack(InteractionContext ctx,
[Option("amount", "Amount to bet (Minimum 50$)")] double amountGiven)
{
await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource);
var userProfile = await _playerService.GetOrCreatePlayerAsync(ctx.Member.Id);
var amount = (int)amountGiven;
DiscordEmbedBuilder? moneyErrorMessage = null;
if(amount < MINIMUM_MONEY_REQ)
{
moneyErrorMessage = new DiscordEmbedBuilder()
.WithDescription("You need atleast 50$ to play blackjack!")
.WithColor(DiscordColor.Red);
}
else if(amount > userProfile.Balance)
{
moneyErrorMessage = new DiscordEmbedBuilder()
.WithDescription($"You wanted to bet {amount}$ but you have only {userProfile.Balance}$")
.WithColor(DiscordColor.Red);
}
if (moneyErrorMessage != null)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(moneyErrorMessage));
// TODO: Some way to reset the cooldown of that command
return;
}
}
For now I display message on error that get the cooldown values, but I can't do anything with that.
private async Task OnErrorOccured(SlashCommandsExtension sender, SlashCommandErrorEventArgs e)
{
await e.Context.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource);
var errorEmbed = new DiscordEmbedBuilder()
.WithColor(DiscordColor.Red);
if(e.Exception is SlashExecutionChecksFailedException)
{
var castedException = (SlashExecutionChecksFailedException)e.Exception;
var timeLeft = String.Empty;
foreach (var error in castedException.FailedChecks)
{
var cooldown = (SlashCooldownAttribute)error;
TimeSpan rawTime = cooldown.GetRemainingCooldown(e.Context);
if (rawTime.Days != 0) timeLeft += $"{rawTime.Days} days, ";
if (rawTime.Hours != 0) timeLeft += $"{rawTime.Hours} hours, ";
if (rawTime.Minutes!= 0) timeLeft += $"{rawTime.Minutes} minutes, ";
if (rawTime.Seconds != 0) timeLeft += $"{rawTime.Seconds} seconds";
}
errorEmbed
.WithTitle("**HOLD UP!**")
.WithDescription($"To use that command you need to wait {timeLeft}");
}
}