From d6efd141390aa8de9198150e3c9ef551b0eab412 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Wed, 14 Feb 2024 22:13:07 +0100 Subject: [PATCH] add command to remove a websub subscription --- cmd/nourybot/commands.go | 7 ++++ cmd/nourybot/eventsub.go | 88 +++++++++++++++++++++++++++++++++++----- 2 files changed, 84 insertions(+), 11 deletions(-) diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index adc7d16..839a858 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -133,6 +133,13 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { //reply = app.createFollowSubscription(target, cmdParams[2]) } } + case "unnotify": + switch cmdParams[1] { + case "live": + if userLevel >= 100 { + reply = app.deleteLiveSubscription(target, cmdParams[2]) + } + } case "ddg": reply = commands.DuckDuckGo(message.Message[6:len(message.Message)]) diff --git a/cmd/nourybot/eventsub.go b/cmd/nourybot/eventsub.go index 4099945..6fbdcdc 100644 --- a/cmd/nourybot/eventsub.go +++ b/cmd/nourybot/eventsub.go @@ -7,20 +7,32 @@ import ( "github.com/nicklaw5/helix/v2" ) +const ( + eventSubResponseOK = 200 + eventSubResponseAccepted = 202 + eventSubResponseNoContent = 204 + eventSubResponseBadRequest = 400 + eventSubResponseUnauthorized = 401 + eventSubResponseForbidden = 403 +) + func (app *application) createLiveSubscription(target, channel string) string { - client, err := helix.NewClient(&helix.Options{ - ClientID: app.Config.twitchClientId, - AppAccessToken: app.HelixClient.GetAppAccessToken(), - }) - if err != nil { - app.Log.Errorw("Error creating new helix client", - "err", err, - ) - } + // client, err := helix.NewClient(&helix.Options{ + // ClientID: app.Config.twitchClientId, + // AppAccessToken: app.HelixClient.GetAppAccessToken(), + // }) + // if err != nil { + // app.Log.Errorw("Error creating new helix client", + // "err", err, + // ) + // } uid := ivr.IDByUsername(channel) + if uid == "xd" { + return fmt.Sprintf("Could not find user with name %v", channel) + } - resp, err := client.CreateEventSubSubscription(&helix.EventSubSubscription{ + resp, err := app.HelixClient.CreateEventSubSubscription(&helix.EventSubSubscription{ Type: helix.EventSubTypeStreamOnline, Version: "1", Condition: helix.EventSubCondition{ @@ -33,7 +45,7 @@ func (app *application) createLiveSubscription(target, channel string) string { }, }) if err != nil { - app.Log.Errorw("Error creating EvenSub subscription", + app.Log.Errorw("Error creating EventSub subscription", "resp", resp, "err", err, ) @@ -42,3 +54,57 @@ func (app *application) createLiveSubscription(target, channel string) string { app.Log.Infof("%+v\n", resp) return fmt.Sprintf("Created subscription for channel %v; uid=%v", channel, uid) } + +func (app *application) deleteLiveSubscription(target, channel string) string { + client, err := helix.NewClient(&helix.Options{ + ClientID: app.Config.twitchClientId, + AppAccessToken: app.HelixClient.GetAppAccessToken(), + }) + if err != nil { + app.Log.Errorw("Error creating new helix client", + "err", err, + ) + } + + uid := ivr.IDByUsername(channel) + if uid == "xd" { + return fmt.Sprintf("Could not find user with name %v", channel) + } + + resp, err := client.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{ + UserID: uid, + }) + if err != nil { + app.Log.Errorw("Error getting EventSub subscriptions", + "resp", resp, + "err", err, + ) + return "Something went wrong FeelsBadMan" + } + + app.Log.Infof("%+v\n", resp) + + if resp.StatusCode != eventSubResponseOK { + return "Subscription was not found FeelsBadMan" + } + if len(resp.Data.EventSubSubscriptions) == 0 { + return "No such subscription found" + } + eventSubID := resp.Data.EventSubSubscriptions[0].ID + + resp2, err := client.RemoveEventSubSubscription(eventSubID) + if err != nil { + app.Log.Errorw("Error getting EventSub subscriptions", + "resp", resp, + "err", err, + ) + return "Something went wrong FeelsBadMan" + } + + if resp2.StatusCode != eventSubResponseNoContent { + return "Subscription was not found FeelsBadMan" + } + + app.Log.Infof("%+v\n", resp2) + return fmt.Sprintf("Successfully deleted live notification for channel %s; id=%v", channel, uid) +}