From ada20061a75c1547efae07deb75fc3cfd7a3cfdc Mon Sep 17 00:00:00 2001 From: lyx0 Date: Sun, 17 Oct 2021 18:24:39 +0200 Subject: [PATCH] change subage api --- pkg/api/ivr/subage.go | 23 ++++++++++++++--------- pkg/commands/subage.go | 8 +++++++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pkg/api/ivr/subage.go b/pkg/api/ivr/subage.go index 3915fa1..f726df8 100644 --- a/pkg/api/ivr/subage.go +++ b/pkg/api/ivr/subage.go @@ -17,23 +17,28 @@ type subageApiResponse struct { SubageHidden bool `json:"hidden"` Subscribed bool `json:"subscribed"` FollowedAt string `json:"followedAt"` - Cumulative Cumulative `json:"cumulative"` - Streak SubStreak `json:"streak"` + Cumulative cumulative `json:"cumulative"` + Streak subStreak `json:"streak"` Error string `json:"error"` } -type Cumulative struct { +type cumulative struct { Months int `json:"months"` } -type SubStreak struct { +type subStreak struct { Months int `json:"months"` } -func Subage(username string, streamer string) string { - resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/subage/%s/%s", username, streamer)) +var ( + subageBaseUrl = "https://api.ivr.fi/twitch/subage" +) + +func Subage(username string, streamer string) (string, error) { + resp, err := http.Get(fmt.Sprintf("%s/%s/%s", subageBaseUrl, username, streamer)) if err != nil { log.Error(err) + return "Something went wrong FeelsBadMan", err } defer resp.Body.Close() @@ -50,16 +55,16 @@ func Subage(username string, streamer string) string { if responseObject.Error != "" { reply := fmt.Sprintf(responseObject.Error + " FeelsBadMan") // client.Say(channel, fmt.Sprintf(responseObject.Error+" FeelsBadMan")) - return reply + return reply, nil } if responseObject.SubageHidden { reply := fmt.Sprintf(username + " has their subscription status hidden. FeelsBadMan") - return reply + return reply, nil } else { months := fmt.Sprint(responseObject.Cumulative.Months) reply := fmt.Sprintf(username + " has been subscribed to " + streamer + " for " + months + " months.") - return reply + return reply, nil } } diff --git a/pkg/commands/subage.go b/pkg/commands/subage.go index 182f17f..c101818 100644 --- a/pkg/commands/subage.go +++ b/pkg/commands/subage.go @@ -1,12 +1,18 @@ package commands import ( + "fmt" + "github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/pkg/api/ivr" ) func Subage(channel string, username string, streamer string, client *twitch.Client) { - ivrResponse := ivr.Subage(username, streamer) + ivrResponse, err := ivr.Subage(username, streamer) + if err != nil { + client.Say(channel, fmt.Sprint(err)) + return + } client.Say(channel, ivrResponse) }