Better command abstraction

This commit is contained in:
2024-10-22 17:07:53 -06:00
parent 0130ec538c
commit 13bf11abce
5 changed files with 65 additions and 77 deletions

View File

@ -3,40 +3,23 @@ package command
import (
"fmt"
"himbot/lib"
"time"
"github.com/bwmarrin/discordgo"
)
func HsCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
if !lib.CheckAndApplyCooldown(s, i, "hs", 10*time.Second) {
return
}
func HsCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (string, error) {
options := i.ApplicationCommandData().Options
if len(options) == 0 || options[0].Type != discordgo.ApplicationCommandOptionString {
return "", fmt.Errorf("please provide a nickname")
}
nickname := options[0].StringValue()
options := i.ApplicationCommandData().Options
if len(options) == 0 || options[0].Type != discordgo.ApplicationCommandOptionString {
lib.RespondWithError(s, i, "Please provide a nickname.")
return
}
nickname := options[0].StringValue()
user, err := lib.GetUser(i)
if err != nil {
return "", fmt.Errorf("error processing command: %w", err)
}
user, err := lib.GetUser(i)
if err != nil {
lib.RespondWithError(s, i, "Error processing command: "+err.Error())
return
}
response := fmt.Sprintf("%s was %s's nickname in high school!", nickname, user.Username)
response := fmt.Sprintf("%s was %s's nickname in high school!", nickname, user.Username)
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: response,
},
})
if err != nil {
fmt.Println("Error responding to interaction:", err)
lib.RespondWithError(s, i, "An error occurred while processing the command")
}
return response, nil
}