From ef89fadb9b2d6407497e8ebc30ffcd35b5d8bf6f Mon Sep 17 00:00:00 2001 From: lyx0 Date: Mon, 18 Oct 2021 14:31:44 +0200 Subject: [PATCH] add randomfox --- pkg/api/randomfox.go | 30 ++++++++++++++++++++++++++++++ pkg/commands/randomfox.go | 12 ++++++++++++ pkg/handlers/command.go | 3 +++ 3 files changed, 45 insertions(+) create mode 100644 pkg/api/randomfox.go create mode 100644 pkg/commands/randomfox.go diff --git a/pkg/api/randomfox.go b/pkg/api/randomfox.go new file mode 100644 index 0000000..777d85b --- /dev/null +++ b/pkg/api/randomfox.go @@ -0,0 +1,30 @@ +package api + +import ( + "encoding/json" + "io/ioutil" + "log" + "net/http" +) + +type randomFoxResponse struct { + Image string `json:"image"` + Link string `json:"link"` +} + +func RandomFox() string { + response, err := http.Get("https://randomfox.ca/floof/") + if err != nil { + log.Fatalln(err) + } + + responseData, err := ioutil.ReadAll(response.Body) + if err != nil { + log.Fatalln(err) + } + + var responseObject randomFoxResponse + json.Unmarshal(responseData, &responseObject) + + return string(responseObject.Image) +} diff --git a/pkg/commands/randomfox.go b/pkg/commands/randomfox.go new file mode 100644 index 0000000..97c7e5e --- /dev/null +++ b/pkg/commands/randomfox.go @@ -0,0 +1,12 @@ +package commands + +import ( + "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/pkg/api" +) + +func RandomFox(channel string, client *twitch.Client) { + reply := api.RandomFox() + + client.Say(channel, reply) +} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index 2f17447..68dcf39 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -165,6 +165,9 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u case "randomcat": commands.RandomCat(message.Channel, twitchClient) return + case "randomfox": + commands.RandomFox(message.Channel, twitchClient) + return case "subage": if msgLen < 3 { twitchClient.Say(message.Channel, "Usage: ()subage [user] [streamer]")