diff --git "a/\\" "b/\\" new file mode 100644 index 0000000..3bafa7d --- /dev/null +++ "b/\\" @@ -0,0 +1,48 @@ +package commands + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + + bot "github.com/lyx0/nourybot-go/bot" +) + +// https://api.ivr.fi +type followageApiResponse struct { + User string `json:"user"` + UserID string `json:"userid"` + Channel string `json:"channel"` + ChannelId string `json:"channelid"` + FollowedAt string `json:"followedAt"` + Error string `json:"error"` +} + +func Followage(streamer string, username string) (string, error) { + resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/subage/%s/%s", username, streamer)) + if err != nil { + log.Fatalln(err) + } + + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + + var responseObject followageApiResponse + json.Unmarshal(body, &responseObject) + + // User or channel was not found + if responseObject.Error != "" { + return fmt.Sprintf(responseObject.Error+" FeelsBadMan")), nil + } else if responseObject.FollowedAt == "" { + bot.SendTwitchMessage(channel, fmt.Sprintf(username+" is not following "+streamer), nil + } else { + d := responseObject.FollowedAt[:10] + return fmt.Sprintf(username+" has been following "+streamer+" since "+d+"."), nil + } +} diff --git a/pkg/api/ivr/followage.go b/pkg/api/ivr/followage.go new file mode 100644 index 0000000..e237f26 --- /dev/null +++ b/pkg/api/ivr/followage.go @@ -0,0 +1,46 @@ +package ivr + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" +) + +// https://api.ivr.fi +type followageApiResponse struct { + User string `json:"user"` + UserID string `json:"userid"` + Channel string `json:"channel"` + ChannelId string `json:"channelid"` + FollowedAt string `json:"followedAt"` + Error string `json:"error"` +} + +func Followage(streamer string, username string) (string, error) { + resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/subage/%s/%s", username, streamer)) + if err != nil { + log.Fatalln(err) + } + + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + + var responseObject followageApiResponse + json.Unmarshal(body, &responseObject) + + // User or channel was not found + if responseObject.Error != "" { + return fmt.Sprintf(responseObject.Error + " FeelsBadMan"), nil + } else if responseObject.FollowedAt == "" { + return fmt.Sprintf(username + " is not following " + streamer), nil + } else { + d := responseObject.FollowedAt[:10] + return fmt.Sprintf(username + " has been following " + streamer + " since " + d + "."), nil + } +} diff --git a/pkg/commands/followage.go b/pkg/commands/followage.go new file mode 100644 index 0000000..0825050 --- /dev/null +++ b/pkg/commands/followage.go @@ -0,0 +1,16 @@ +package commands + +import ( + "fmt" + + "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/pkg/api/ivr" +) + +func Followage(channel string, streamer string, username string, client *twitch.Client) { + ivrResponse, err := ivr.Followage(streamer, username) + if err != nil { + client.Say(channel, fmt.Sprint(err)) + } + client.Say(channel, ivrResponse) +} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index 289e2a6..09eb92a 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -94,6 +94,14 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u commands.Firstline(message.Channel, cmdParams[1], cmdParams[2], twitchClient) return } + case "followage": + if msgLen <= 2 { + twitchClient.Say(message.Channel, "Usage: ()followage [channel] [user]") + return + } else { + commands.Followage(message.Channel, cmdParams[1], cmdParams[2], twitchClient) + return + } case "game": if msgLen == 1 { twitchClient.Say(message.Channel, "Usage: ()game [channel]")