From ca6a199244cbb18b78ee0432f404b7d886883cc6 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Wed, 21 Feb 2024 22:34:56 +0100 Subject: [PATCH] added cat command which worked but did the bot just get rate limited? --- cmd_cat.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 cmd_cat.go diff --git a/cmd_cat.go b/cmd_cat.go new file mode 100644 index 0000000..d1d8e6b --- /dev/null +++ b/cmd_cat.go @@ -0,0 +1,65 @@ +package main + +import ( + "encoding/json" + "io" + "log" + "net/http" + + "github.com/bwmarrin/discordgo" +) + +var cat_command Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "cat", + Description: "Random cat pictures", + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + { + Type: discordgo.EmbedTypeImage, + Color: hexToDecimal(color["primary"]), + Image: &discordgo.MessageEmbedImage{ + URL: GetCatImageURL(), + }, + }, + }, + }, + }) + }, +} + +type CatImage struct { + ID string `json:"id"` + URL string `json:"url"` + Width int `json:"width"` + Height int `json:"height"` +} + +func GetCatImageURL() string { + resp, err := http.Get("https://api.thecatapi.com/v1/images/search?format=json") + if err != nil { + log.Fatal("Error making GET request:", err) + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatal("Error reading response body:", err) + } + + var images []CatImage + err = json.Unmarshal(body, &images) + if err != nil { + log.Fatal("Error unmarshalling JSON:", err) + } + + if len(images) == 0 { + log.Fatal("No images found") + } + + return images[0].URL +}