Fixed 7tv & Twitch reconnection. Added adbreak, follow, subscription handlers for Twitch. Added multi-chat support. Added support to unsubscribe from Twitch event subs.

This commit is contained in:
Tom
2024-08-06 19:29:29 +00:00
parent 75fcb8e0f8
commit 8014c12bc5
60 changed files with 1064 additions and 672 deletions

View File

@@ -1,4 +1,3 @@
using CommonSocketLibrary.Abstract;
using Serilog;
using TwitchChatTTS.Twitch.Socket.Messages;
@@ -8,26 +7,23 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
{
public string Name => "session_welcome";
private readonly HermesApiClient _hermes;
private readonly TwitchApiClient _api;
private readonly User _user;
private readonly ILogger _logger;
public SessionWelcomeHandler(TwitchApiClient api, User user, ILogger logger)
public SessionWelcomeHandler(HermesApiClient hermes, TwitchApiClient api, User user, ILogger logger)
{
_hermes = hermes;
_api = api;
_user = user;
_logger = logger;
}
public async Task Execute(TwitchWebsocketClient sender, object? data)
public async Task Execute(TwitchWebsocketClient sender, object data)
{
if (sender == null)
return;
if (data == null)
{
_logger.Warning("Twitch websocket message data is null.");
return;
}
if (data is not SessionWelcomeMessage message)
return;
if (_api == null)
@@ -39,6 +35,11 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
return;
}
await _hermes.AuthorizeTwitch();
var token = await _hermes.FetchTwitchBotToken();
_api.Initialize(token);
string broadcasterId = _user.TwitchUserId.ToString();
string[] subscriptionsv1 = [
"channel.chat.message",
"channel.chat.message_delete",
@@ -53,17 +54,36 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
string[] subscriptionsv2 = [
"channel.follow",
];
string broadcasterId = _user.TwitchUserId.ToString();
string? pagination = null;
int size = 0;
do
{
var subscriptionsData = await _api.GetSubscriptions(status: "enabled", broadcasterId: broadcasterId, after: pagination);
var subscriptionNames = subscriptionsData?.Data == null ? [] : subscriptionsData.Data.Select(s => s.Type).ToArray();
if (subscriptionNames.Length == 0)
break;
foreach (var d in subscriptionsData!.Data!)
sender.AddSubscription(broadcasterId, d.Type, d.Id);
subscriptionsv1 = subscriptionsv1.Except(subscriptionNames).ToArray();
subscriptionsv2 = subscriptionsv2.Except(subscriptionNames).ToArray();
pagination = subscriptionsData?.Pagination?.Cursor;
size = subscriptionNames.Length;
} while (size >= 100 && pagination != null && subscriptionsv1.Length + subscriptionsv2.Length > 0);
foreach (var subscription in subscriptionsv1)
await Subscribe(subscription, message.Session.Id, broadcasterId, "1");
await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "1");
foreach (var subscription in subscriptionsv2)
await Subscribe(subscription, message.Session.Id, broadcasterId, "2");
sender.SessionId = message.Session.Id;
sender.Identified = sender.SessionId != null;
await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "2");
sender.Identify(message.Session.Id);
}
private async Task Subscribe(string subscriptionName, string sessionId, string broadcasterId, string version)
private async Task Subscribe(TwitchWebsocketClient sender, string subscriptionName, string sessionId, string broadcasterId, string version)
{
try
{
@@ -83,6 +103,10 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
_logger.Error($"Failed to create an event subscription [subscription type: {subscriptionName}][reason: data is empty]");
return;
}
foreach (var d in response.Data)
sender.AddSubscription(broadcasterId, d.Type, d.Id);
_logger.Information($"Sucessfully added subscription to Twitch websockets [subscription type: {subscriptionName}]");
}
catch (Exception ex)