add subage command

This commit is contained in:
lyx0 2021-10-16 00:56:08 +02:00
parent 9c7f918a07
commit 828790c3e4
2 changed files with 70 additions and 0 deletions

62
pkg/commands/subage.go Normal file
View file

@ -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."))
}
}

View file

@ -99,6 +99,14 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u
} else { } else {
twitchClient.Say(message.Channel, "Pleb's can't pyramid FeelsBadMan") 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": case "uptime":
if msgLen == 1 { if msgLen == 1 {
commands.Uptime(message.Channel, message.Channel, twitchClient) commands.Uptime(message.Channel, message.Channel, twitchClient)