Files
2025-03-23 13:38:56 -04:00

64 lines
1.4 KiB
Go

package commands
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
Helpers "ion606_bot/Bot/Helpers"
"github.com/bwmarrin/discordgo"
)
var MeowCommand = &discordgo.ApplicationCommand{
Name: "meow",
Description: "Sends a random cat image",
}
type SefinekAPIResponse struct {
Success bool `json:"success"`
Status int `json:"status"`
Info struct {
Category string `json:"category"`
Endpoint string `json:"endpoint"`
} `json:"info"`
Message string `json:"message"`
}
func HandleMeow(s *discordgo.Session, i *discordgo.InteractionCreate) {
client := http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Get("https://api.sefinek.net/api/v2/random/animal/cat")
if err != nil {
Helpers.HandleError(s, i, fmt.Errorf("fetching cat image: %w", err))
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Helpers.HandleError(s, i, fmt.Errorf("reading cat image response: %w", err))
return
}
var apiResp SefinekAPIResponse
err = json.Unmarshal(body, &apiResp)
if err != nil || !apiResp.Success {
Helpers.HandleError(s, i, fmt.Errorf("parsing cat image response"))
return
}
// Use the "message" field which contains the image URL.
imageURL := apiResp.Message
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: imageURL,
},
})
}