made command response error when cataas is down

This commit is contained in:
2024-03-12 09:50:28 +01:00
parent 44f3ea6c37
commit 0e921bbb9f

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"log"
"net/http" "net/http"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@@ -15,12 +14,15 @@ var cmd_cat Command = Command{
Description: "Random cat pictures", Description: "Random cat pictures",
}, },
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
cat, err := GetCatImageURL()
if err == nil {
respondEmbed(i.Interaction, discordgo.MessageEmbed{ respondEmbed(i.Interaction, discordgo.MessageEmbed{
Type: discordgo.EmbedTypeImage, Type: discordgo.EmbedTypeImage,
Color: hexToDecimal(color["primary"]), Color: hexToDecimal(color["primary"]),
Image: &discordgo.MessageEmbedImage{ Image: &discordgo.MessageEmbedImage{
URL: GetCatImageURL(), URL: cat,
}}, false) }}, false)
}
}, },
} }
@@ -28,23 +30,18 @@ type CatImage struct {
ID string `json:"_id"` ID string `json:"_id"`
} }
func GetCatImageURL() string { func GetCatImageURL() (string, error) {
resp, err := http.Get("https://cataas.com/cat?json=true") resp, err := http.Get("https://cataas.com/cat?json=true")
if err != nil { if err != nil {
log.Print("Error making GET request:", err) return "", err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Print("Error reading response body:", err) return "", err
} }
var images CatImage var images CatImage
err = json.Unmarshal(body, &images) err = json.Unmarshal(body, &images)
if err != nil {
log.Print("Error unmarshalling JSON:", err)
}
return "https://cataas.com/cat/" + images.ID return "https://cataas.com/cat/" + images.ID, err
} }