From 590e7b866d2af121297bafb2722cec012414da4f Mon Sep 17 00:00:00 2001 From: lyx0 Date: Mon, 18 Oct 2021 18:33:49 +0200 Subject: [PATCH] add randomdog command --- pkg/api/randomdog.go | 29 +++++++++++++++++++++++++++++ pkg/commands/randomdog.go | 12 ++++++++++++ pkg/handlers/command.go | 3 +++ 3 files changed, 44 insertions(+) create mode 100644 pkg/api/randomdog.go create mode 100644 pkg/commands/randomdog.go diff --git a/pkg/api/randomdog.go b/pkg/api/randomdog.go new file mode 100644 index 0000000..306b911 --- /dev/null +++ b/pkg/api/randomdog.go @@ -0,0 +1,29 @@ +package api + +import ( + "encoding/json" + "io/ioutil" + "log" + "net/http" +) + +type randomDogResponse struct { + Url string `json:"url"` +} + +func RandomDog() string { + response, err := http.Get("https://random.dog/woof.json") + if err != nil { + log.Fatalln(err) + } + + responseData, err := ioutil.ReadAll(response.Body) + if err != nil { + log.Fatalln(err) + } + + var responseObject randomDogResponse + json.Unmarshal(responseData, &responseObject) + + return string(responseObject.Url) +} diff --git a/pkg/commands/randomdog.go b/pkg/commands/randomdog.go new file mode 100644 index 0000000..ece7677 --- /dev/null +++ b/pkg/commands/randomdog.go @@ -0,0 +1,12 @@ +package commands + +import ( + "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/pkg/api" +) + +func RandomDog(channel string, client *twitch.Client) { + reply := api.RandomDog() + + client.Say(channel, reply) +} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index f5e1a84..1a87d90 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 "randomdog": + commands.RandomDog(message.Channel, twitchClient) + return case "randomfox": commands.RandomFox(message.Channel, twitchClient) return