From 828790c3e4bcc4e1a3922d02883fd68da7a69434 Mon Sep 17 00:00:00 2001 From: lyx0 Date: Sat, 16 Oct 2021 00:56:08 +0200 Subject: [PATCH] add subage command --- pkg/commands/subage.go | 62 +++++++++++++++++++++++++++++++++++++++++ pkg/handlers/command.go | 8 ++++++ 2 files changed, 70 insertions(+) create mode 100644 pkg/commands/subage.go diff --git a/pkg/commands/subage.go b/pkg/commands/subage.go new file mode 100644 index 0000000..583424d --- /dev/null +++ b/pkg/commands/subage.go @@ -0,0 +1,62 @@ +package commands + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + "github.com/gempir/go-twitch-irc/v2" + log "github.com/sirupsen/logrus" +) + +type subageApiResponse struct { + User string `json:"user"` + UserID string `json:"userId"` + Channel string `json:"channel"` + ChannelId string `json:"channelid"` + SubageHidden bool `json:"hidden"` + Subscribed bool `json:"subscribed"` + FollowedAt string `json:"followedAt"` + Cumulative Cumulative `json:"cumulative"` + Streak SubStreak `json:"streak"` + Error string `json:"error"` +} + +type Cumulative struct { + Months int `json:"months"` +} + +type SubStreak struct { + Months int `json:"months"` +} + +func Subage(channel string, username string, streamer string, client *twitch.Client) { + resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/subage/%s/%s", username, streamer)) + if err != nil { + log.Error(err) + } + + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + + var responseObject subageApiResponse + json.Unmarshal(body, &responseObject) + + // User or channel was not found + if responseObject.Error != "" { + client.Say(channel, fmt.Sprintf(responseObject.Error+" FeelsBadMan")) + return + } + + if responseObject.SubageHidden { + client.Say(channel, fmt.Sprintf(username+" has their subscription status hidden. FeelsBadMan")) + } else { + months := fmt.Sprint(responseObject.Cumulative.Months) + client.Say(channel, fmt.Sprintf(username+" has been subscribed to "+streamer+" for "+months+" months.")) + } +} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index 043ea6c..d90a005 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -99,6 +99,14 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u } else { twitchClient.Say(message.Channel, "Pleb's can't pyramid FeelsBadMan") } + + case "subage": + if msgLen < 3 { + twitchClient.Say(message.Channel, "Usage: ()subage [user] [streamer]") + return + } else { + commands.Subage(message.Channel, cmdParams[1], cmdParams[2], twitchClient) + } case "uptime": if msgLen == 1 { commands.Uptime(message.Channel, message.Channel, twitchClient)