Files
acecore/plugin_src/simplefun/cmd_cat.go
2024-04-15 18:17:12 +02:00

61 lines
1.4 KiB
Go

package main
import (
"encoding/json"
"io"
"net/http"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/custom"
"github.com/vaporvee/acecore/shared"
)
var cmd_cat = shared.Command{
Definition: discord.SlashCommandCreate{
Name: "cat",
Description: "Random cat pictures",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel,
discord.InteractionContextTypeBotDM,
},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall,
discord.ApplicationIntegrationTypeUserInstall,
},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
cat, err := GetCatImageURL()
if err == nil {
err = e.CreateMessage(discord.NewMessageCreateBuilder().
AddEmbeds(discord.NewEmbedBuilder().SetDescription(cat.Fact).SetImage(cat.Image).SetColor(custom.GetColor("primary")).Build()).
Build())
if err != nil {
logrus.Error(err)
}
}
},
}
type Cat struct {
Image string `json:"image"`
Fact string `json:"fact"`
}
func GetCatImageURL() (Cat, error) {
resp, err := http.Get("https://some-random-api.com/animal/cat")
var cat Cat
if err != nil {
return cat, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return cat, err
}
err = json.Unmarshal(body, &cat)
return cat, err
}