From 2860df87b9e593fef96a1a0477136b78a26e148c Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 29 Feb 2024 21:35:45 +0100 Subject: [PATCH] replace ivr api with twitch api calls --- cmd/nourybot/channel.go | 11 +++-- cmd/nourybot/eventsub.go | 29 ++++++----- cmd/nourybot/send.go | 3 +- pkg/ivr/user.go | 104 --------------------------------------- 4 files changed, 25 insertions(+), 122 deletions(-) delete mode 100644 pkg/ivr/user.go diff --git a/cmd/nourybot/channel.go b/cmd/nourybot/channel.go index 08233ed..3918f86 100644 --- a/cmd/nourybot/channel.go +++ b/cmd/nourybot/channel.go @@ -4,16 +4,19 @@ import ( "fmt" "github.com/gempir/go-twitch-irc/v4" - "github.com/lyx0/nourybot/pkg/ivr" ) -// AddChannel calls ivr.IDByUsername with the provided login and tries to insert +// AddChannel looks up the userID of the provided login and tries to insert // the Twitch login and userID into the database. If there is no error thrown the // TwitchClient joins the channel afterwards. func (app *application) AddChannel(login string, message twitch.PrivateMessage) { - userID := ivr.IDByUsername(login) + userID, err := app.getUserID(login) + if err != nil { + app.Log.Error(err) + return + } - err := app.Models.Channels.Insert(login, userID) + err = app.Models.Channels.Insert(login, userID) if err != nil { reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err) app.Send(message.Channel, reply, message) diff --git a/cmd/nourybot/eventsub.go b/cmd/nourybot/eventsub.go index 95e2d33..a142edc 100644 --- a/cmd/nourybot/eventsub.go +++ b/cmd/nourybot/eventsub.go @@ -3,7 +3,6 @@ package main import ( "fmt" - "github.com/lyx0/nourybot/pkg/ivr" "github.com/nicklaw5/helix/v2" ) @@ -18,9 +17,10 @@ const ( // createLiveSubscription creates a stream.online twitch eventsub subscription for the specified channel func (app *application) createLiveSubscription(target, channel string) string { - uid := ivr.IDByUsername(channel) - if uid == "xd" { - return fmt.Sprintf("Could not find user with name %v", channel) + uid, err := app.getUserID(channel) + if err != nil { + app.Log.Error(err) + return fmt.Sprint(ErrGenericErrorMessage) } resp, err := app.HelixClient.CreateEventSubSubscription(&helix.EventSubSubscription{ @@ -48,9 +48,10 @@ func (app *application) createLiveSubscription(target, channel string) string { // deleteLiveSubscription deletes a stream.live twitch eventsub subscription for the specified channel func (app *application) deleteLiveSubscription(target, channel string) string { - uid := ivr.IDByUsername(channel) - if uid == "xd" { - return fmt.Sprintf("Could not find user with name %v", channel) + uid, err := app.getUserID(channel) + if err != nil { + app.Log.Error(err) + return fmt.Sprint(ErrGenericErrorMessage) } resp, err := app.HelixClient.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{ @@ -106,9 +107,10 @@ func (app *application) deleteLiveSubscription(target, channel string) string { // createOfflineSubscription creates a stream.offline twitch eventsub // subscription for the specified channel. func (app *application) createOfflineSubscription(target, channel string) string { - uid := ivr.IDByUsername(channel) - if uid == "xd" { - return fmt.Sprintf("Could not find user with name %v", channel) + uid, err := app.getUserID(channel) + if err != nil { + app.Log.Error(err) + return fmt.Sprint(ErrGenericErrorMessage) } resp, err := app.HelixClient.CreateEventSubSubscription(&helix.EventSubSubscription{ @@ -137,9 +139,10 @@ func (app *application) createOfflineSubscription(target, channel string) string // deleteOfflineSubscription deletes a stream.offline twitch eventsub // subscription for the specified channel func (app *application) deleteOfflineSubscription(target, channel string) string { - uid := ivr.IDByUsername(channel) - if uid == "xd" { - return fmt.Sprintf("Could not find user with name %v", channel) + uid, err := app.getUserID(channel) + if err != nil { + app.Log.Error(err) + return fmt.Sprint(ErrGenericErrorMessage) } resp, err := app.HelixClient.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{ diff --git a/cmd/nourybot/send.go b/cmd/nourybot/send.go index 819e794..45bc0b1 100644 --- a/cmd/nourybot/send.go +++ b/cmd/nourybot/send.go @@ -265,7 +265,8 @@ func (app *application) SendNoLimit(target, message string) { // TODO: Make it so it splits at a space instead and not in the middle of a word. // Message was fine. identifier := uuid.NewString() - go app.Models.SentMessagesLogs.Insert(target, message, "unavailable", "unavailable", "unavailable", "unavailable", identifier, "unavailable") + go app.Models.SentMessagesLogs.Insert( + target, message, "unavailable", "unavailable", "unavailable", "unavailable", identifier, "unavailable") go app.TwitchClient.Say(target, message) return } diff --git a/pkg/ivr/user.go b/pkg/ivr/user.go deleted file mode 100644 index 0e8935f..0000000 --- a/pkg/ivr/user.go +++ /dev/null @@ -1,104 +0,0 @@ -package ivr - -import ( - "encoding/json" - "fmt" - "net/http" -) - -type ivrResponse struct { - ID string `json:"id"` - Stream ivrStream `json:"stream"` -} - -type ivrStream struct { - Title string `json:"title"` - Game ivrGame `json:"game"` -} - -type ivrGame struct { - DisplayName string `json:"displayName"` -} - -// TitleByUsername returns the current title of a supplied twitch channel. -func TitleByUsername(login string) string { - baseUrl := "https://api.ivr.fi/v2/twitch/user?login=" - - resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, login)) - if err != nil { - return "xd" - } - - defer resp.Body.Close() - - responseList := make([]ivrResponse, 0) - _ = json.NewDecoder(resp.Body).Decode(&responseList) - if len(responseList) == 0 { - return "xd" - } - - return responseList[0].Stream.Title -} - -// GameByUsername returns the current game of a supplied twitch channel. -func GameByUsername(login string) string { - baseUrl := "https://api.ivr.fi/v2/twitch/user?login=" - - resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, login)) - if err != nil { - return "xd" - } - - defer resp.Body.Close() - - responseList := make([]ivrResponse, 0) - _ = json.NewDecoder(resp.Body).Decode(&responseList) - if len(responseList) == 0 { - return "xd" - } - - return responseList[0].Stream.Game.DisplayName -} - -// IDByUsernameReply returns the twitch user id of a supplied -// twitch username in the format of "username=id" -func IDByUsernameReply(username string) string { - baseUrl := "https://api.ivr.fi/v2/twitch/user?login=" - - resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, username)) - if err != nil { - return "xd" - } - - responseList := make([]ivrResponse, 0) - err = json.NewDecoder(resp.Body).Decode(&responseList) - if err != nil { - return "xd" - } - if len(responseList) == 0 { - return "xd" - } - - reply := fmt.Sprintf("%s=%s", username, responseList[0].ID) - return reply -} - -// IDByUsername returns the twitch user id for a given twitch username. -func IDByUsername(username string) string { - baseUrl := "https://api.ivr.fi/v2/twitch/user?login=" - - resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, username)) - if err != nil { - return "xd" - } - - defer resp.Body.Close() - - responseList := make([]ivrResponse, 0) - _ = json.NewDecoder(resp.Body).Decode(&responseList) - if len(responseList) == 0 { - return "xd" - } - - return responseList[0].ID -}