From 0130ec538cbda1d40926d28d54fbbea7aa8182ea Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Tue, 22 Oct 2024 16:19:31 -0600 Subject: [PATCH] Better error handling --- lib/{timer.go => cooldown.go} | 36 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 23 deletions(-) rename lib/{timer.go => cooldown.go} (52%) diff --git a/lib/timer.go b/lib/cooldown.go similarity index 52% rename from lib/timer.go rename to lib/cooldown.go index 7f82090..11a022e 100644 --- a/lib/timer.go +++ b/lib/cooldown.go @@ -48,29 +48,19 @@ func (cm *CooldownManager) CheckCooldown(userID, commandName string) (bool, time } func CheckAndApplyCooldown(s *discordgo.Session, i *discordgo.InteractionCreate, commandName string, duration time.Duration) bool { - cooldownManager := GetCooldownManager() - user, err := GetUser(i) - if err != nil { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "Error processing command: " + err.Error(), - }, - }) - return false - } + cooldownManager := GetCooldownManager() + user, err := GetUser(i) + if err != nil { + RespondWithError(s, i, "Error processing command: "+err.Error()) + return false + } - canUse, remaining := cooldownManager.CheckCooldown(user.ID, commandName) - if !canUse { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: fmt.Sprintf("You can use this command again in %v", remaining.Round(time.Second)), - }, - }) - return false - } + canUse, remaining := cooldownManager.CheckCooldown(user.ID, commandName) + if !canUse { + RespondWithError(s, i, fmt.Sprintf("You can use this command again in %v", remaining.Round(time.Second))) + return false + } - cooldownManager.SetCooldown(user.ID, commandName, duration) - return true + cooldownManager.SetCooldown(user.ID, commandName, duration) + return true }