replace ivr api with twitch api calls

This commit is contained in:
lyx0 2024-02-29 21:35:45 +01:00
parent 6911603401
commit 2860df87b9
4 changed files with 25 additions and 122 deletions

View file

@ -4,16 +4,19 @@ import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v4" "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 // the Twitch login and userID into the database. If there is no error thrown the
// TwitchClient joins the channel afterwards. // TwitchClient joins the channel afterwards.
func (app *application) AddChannel(login string, message twitch.PrivateMessage) { 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 { if err != nil {
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err) reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
app.Send(message.Channel, reply, message) app.Send(message.Channel, reply, message)

View file

@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/lyx0/nourybot/pkg/ivr"
"github.com/nicklaw5/helix/v2" "github.com/nicklaw5/helix/v2"
) )
@ -18,9 +17,10 @@ const (
// createLiveSubscription creates a stream.online twitch eventsub subscription for the specified channel // createLiveSubscription creates a stream.online twitch eventsub subscription for the specified channel
func (app *application) createLiveSubscription(target, channel string) string { func (app *application) createLiveSubscription(target, channel string) string {
uid := ivr.IDByUsername(channel) uid, err := app.getUserID(channel)
if uid == "xd" { if err != nil {
return fmt.Sprintf("Could not find user with name %v", channel) app.Log.Error(err)
return fmt.Sprint(ErrGenericErrorMessage)
} }
resp, err := app.HelixClient.CreateEventSubSubscription(&helix.EventSubSubscription{ 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 // deleteLiveSubscription deletes a stream.live twitch eventsub subscription for the specified channel
func (app *application) deleteLiveSubscription(target, channel string) string { func (app *application) deleteLiveSubscription(target, channel string) string {
uid := ivr.IDByUsername(channel) uid, err := app.getUserID(channel)
if uid == "xd" { if err != nil {
return fmt.Sprintf("Could not find user with name %v", channel) app.Log.Error(err)
return fmt.Sprint(ErrGenericErrorMessage)
} }
resp, err := app.HelixClient.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{ 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 // createOfflineSubscription creates a stream.offline twitch eventsub
// subscription for the specified channel. // subscription for the specified channel.
func (app *application) createOfflineSubscription(target, channel string) string { func (app *application) createOfflineSubscription(target, channel string) string {
uid := ivr.IDByUsername(channel) uid, err := app.getUserID(channel)
if uid == "xd" { if err != nil {
return fmt.Sprintf("Could not find user with name %v", channel) app.Log.Error(err)
return fmt.Sprint(ErrGenericErrorMessage)
} }
resp, err := app.HelixClient.CreateEventSubSubscription(&helix.EventSubSubscription{ 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 // deleteOfflineSubscription deletes a stream.offline twitch eventsub
// subscription for the specified channel // subscription for the specified channel
func (app *application) deleteOfflineSubscription(target, channel string) string { func (app *application) deleteOfflineSubscription(target, channel string) string {
uid := ivr.IDByUsername(channel) uid, err := app.getUserID(channel)
if uid == "xd" { if err != nil {
return fmt.Sprintf("Could not find user with name %v", channel) app.Log.Error(err)
return fmt.Sprint(ErrGenericErrorMessage)
} }
resp, err := app.HelixClient.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{ resp, err := app.HelixClient.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{

View file

@ -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. // TODO: Make it so it splits at a space instead and not in the middle of a word.
// Message was fine. // Message was fine.
identifier := uuid.NewString() 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) go app.TwitchClient.Say(target, message)
return return
} }

View file

@ -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
}