This commit is contained in:
2024-11-04 01:23:57 -06:00
parent af3f3ab355
commit b805f27d0e
10 changed files with 246 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package lib
import (
"log"
"time"
"github.com/bwmarrin/discordgo"
@ -10,34 +11,54 @@ type CommandFunc func(s *discordgo.Session, i *discordgo.InteractionCreate) (str
func HandleCommand(commandName string, cooldownDuration time.Duration, handler CommandFunc) func(s *discordgo.Session, i *discordgo.InteractionCreate) {
return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Get user information first
user, userErr := GetUser(i)
if userErr != nil {
RespondWithError(s, i, "Error processing command: "+userErr.Error())
return
}
// Store user in database
dbErr := StoreUser(user.ID, user.Username)
if dbErr != nil {
// Log the error but don't stop command execution
log.Printf("Error storing user: %v", dbErr)
}
// Rest of your existing HandleCommand logic...
if !CheckAndApplyCooldown(s, i, commandName, cooldownDuration) {
return
}
if !CheckAndApplyCooldown(s, i, commandName, cooldownDuration) {
return
}
// Acknowledge the interaction immediately
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
interactErr := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
})
if err != nil {
ThrowWithError(commandName, "Error deferring response: "+err.Error())
if interactErr != nil {
ThrowWithError(commandName, "Error deferring response: "+interactErr.Error())
return
}
// Execute the command handler
response, err := handler(s, i)
response, handlerErr := handler(s, i)
if err != nil {
RespondWithError(s, i, "Error processing command: "+err.Error())
if handlerErr != nil {
RespondWithError(s, i, "Error processing command: "+handlerErr.Error())
return
}
// Send the follow-up message with the response
_, err = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
_, followErr := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: response,
})
if err != nil {
ThrowWithError(commandName, "Error sending follow-up message: "+err.Error())
if followErr != nil {
ThrowWithError(commandName, "Error sending follow-up message: "+followErr.Error())
}
}
}