diff --git a/Chat/Commands/SkipCommand.cs b/Chat/Commands/SkipCommand.cs index 92e46c6..8e23421 100644 --- a/Chat/Commands/SkipCommand.cs +++ b/Chat/Commands/SkipCommand.cs @@ -1,7 +1,6 @@ using Serilog; using TwitchChatTTS.Chat.Soeech; using TwitchChatTTS.Hermes.Socket; -using TwitchChatTTS.Twitch.Socket; using TwitchChatTTS.Twitch.Socket.Messages; using static TwitchChatTTS.Chat.Commands.TTSCommands; diff --git a/Chat/Speech/TTSPlayer.cs b/Chat/Speech/TTSPlayer.cs index 748dc33..efc0afd 100644 --- a/Chat/Speech/TTSPlayer.cs +++ b/Chat/Speech/TTSPlayer.cs @@ -9,7 +9,6 @@ namespace TwitchChatTTS.Chat.Soeech private readonly Mutex _mutex; private readonly Mutex _mutex2; - //public TTSGroupedMessage? PlayingGroup { get; set; } public TTSGroupedMessage? Playing { get; set; } public TTSPlayer() @@ -200,7 +199,6 @@ namespace TwitchChatTTS.Chat.Soeech public DateTime Timestamp { get; set; } public int Priority { get; set; } public IList Messages { get; set; } - //public IList Audios { get; set; } public ISampleProvider? Audio { get; set; } @@ -212,7 +210,6 @@ namespace TwitchChatTTS.Chat.Soeech Messages = messages; Timestamp = timestamp; Priority = priority; - //Audios = new List(); } } } \ No newline at end of file diff --git a/Hermes/Socket/Handlers/LoginAckHandler.cs b/Hermes/Socket/Handlers/LoginAckHandler.cs index 3ca981a..2089dcb 100644 --- a/Hermes/Socket/Handlers/LoginAckHandler.cs +++ b/Hermes/Socket/Handlers/LoginAckHandler.cs @@ -1,3 +1,4 @@ +using System.Text.RegularExpressions; using CommonSocketLibrary.Abstract; using CommonSocketLibrary.Common; using HermesSocketLibrary.Socket.Data; @@ -42,11 +43,23 @@ namespace TwitchChatTTS.Hermes.Socket.Handlers _user.OwnerId = message.OwnerId; _user.DefaultTTSVoice = message.DefaultTTSVoice; _user.VoicesAvailable = message.TTSVoicesAvailable; - _user.RegexFilters = message.WordFilters.ToArray(); _user.VoicesEnabled = new HashSet(message.EnabledTTSVoices); _user.TwitchConnection = message.Connections.FirstOrDefault(c => c.Default && c.Type == "twitch"); _user.NightbotConnection = message.Connections.FirstOrDefault(c => c.Default && c.Type == "nightbot"); + var filters = message.WordFilters.Where(f => f.Search != null && f.Replace != null).ToArray(); + foreach (var filter in filters) + { + try + { + var re = new Regex(filter.Search!, RegexOptions.Compiled); + re.Match(string.Empty); + filter.Regex = re; + } + catch (Exception) { } + } + _user.RegexFilters = filters; + client.LoggedIn = true; _logger.Information($"Logged in as {_user.TwitchUsername} {(message.WebLogin ? "via web" : "via TTS app")}."); @@ -56,13 +69,14 @@ namespace TwitchChatTTS.Hermes.Socket.Handlers await client.FetchRedemptions(); await client.FetchPermissions(); - if (_user.NightbotConnection != null) { + if (_user.NightbotConnection != null) + { _nightbot.Initialize(_user.NightbotConnection.ClientId, _user.NightbotConnection.AccessToken); - var span = DateTime.Now - _user.NightbotConnection.ExpiresAt; - var timeLeft = span.TotalDays >= 2 ? Math.Floor(span.TotalDays) + " days" : (span.TotalHours >= 2 ? Math.Floor(span.TotalHours) + " hours" : Math.Floor(span.TotalMinutes) + " minutes"); - if (span.TotalDays >= 3) + var span = _user.NightbotConnection.ExpiresAt - DateTime.Now; + var timeLeft = span.Days >= 2 ? span.Days + " days" : (span.Hours >= 2 ? span.Hours + " hours" : span.Minutes + " minutes"); + if (span.Days >= 3) _logger.Information($"Nightbot connection has {timeLeft} before it is revoked."); - else if (span.TotalMinutes >= 0) + else if (span.Minutes >= 0) _logger.Warning($"Nightbot connection has {timeLeft} before it is revoked. Refreshing the token is soon required."); else _logger.Error("Nightbot connection has its permissions revoked. Refresh the token. Anything related to Nightbot from this application will not work."); diff --git a/Twitch/Socket/Handlers/NotificationHandler.cs b/Twitch/Socket/Handlers/NotificationHandler.cs index 6b8bac2..78e646c 100644 --- a/Twitch/Socket/Handlers/NotificationHandler.cs +++ b/Twitch/Socket/Handlers/NotificationHandler.cs @@ -46,25 +46,28 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers public async Task Execute(TwitchWebsocketClient sender, object data) { - if (sender == null) - return; - if (data is not NotificationMessage message) - return; - - if (!_messageTypes.TryGetValue(message.Subscription.Type, out var type) || type == null) + Task.Run(async () => { - _logger.Warning($"Could not find Twitch notification type [message type: {message.Subscription.Type}]"); - return; - } + if (sender == null) + return; + if (data is not NotificationMessage message) + return; - if (!_handlers.TryGetValue(message.Subscription.Type, out ITwitchSocketHandler? handler) || handler == null) - { - _logger.Warning($"Could not find Twitch notification handler [message type: {message.Subscription.Type}]"); - return; - } + if (!_messageTypes.TryGetValue(message.Subscription.Type, out var type) || type == null) + { + _logger.Warning($"Could not find Twitch notification type [message type: {message.Subscription.Type}]"); + return; + } - var d = JsonSerializer.Deserialize(message.Event.ToString()!, type, _options); - await handler.Execute(sender, d); + if (!_handlers.TryGetValue(message.Subscription.Type, out ITwitchSocketHandler? handler) || handler == null) + { + _logger.Warning($"Could not find Twitch notification handler [message type: {message.Subscription.Type}]"); + return; + } + + var d = JsonSerializer.Deserialize(message.Event.ToString()!, type, _options); + Task.Run(async () => await handler.Execute(sender, d)); + }); } } } \ No newline at end of file diff --git a/Twitch/Socket/Handlers/SessionWelcomeHandler.cs b/Twitch/Socket/Handlers/SessionWelcomeHandler.cs index 438ed22..8809137 100644 --- a/Twitch/Socket/Handlers/SessionWelcomeHandler.cs +++ b/Twitch/Socket/Handlers/SessionWelcomeHandler.cs @@ -40,11 +40,11 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers } _api.Initialize(_user.TwitchConnection.ClientId, _user.TwitchConnection.AccessToken); - var span = DateTime.Now - _user.TwitchConnection.ExpiresAt; - var timeLeft = span.TotalDays >= 2 ? Math.Floor(span.TotalDays) + " days" : (span.TotalHours >= 2 ? Math.Floor(span.TotalHours) + " hours" : Math.Floor(span.TotalMinutes) + " minutes"); - if (span.TotalDays >= 3) + var span = _user.TwitchConnection.ExpiresAt - DateTime.Now; + var timeLeft = span.Days >= 2 ? span.Days + " days" : (span.Hours >= 2 ? span.Hours + " hours" : span.Minutes + " minutes"); + if (span.Days >= 3) _logger.Information($"Twitch connection has {timeLeft} before it is revoked."); - else if (span.TotalMinutes >= 0) + else if (span.Minutes >= 0) _logger.Warning($"Twitch connection has {timeLeft} before it is revoked. Refreshing the token is soon required."); else { _logger.Error("Twitch connection has its permissions revoked. Refresh the token. Twith client will not be connecting."); diff --git a/Twitch/Socket/TwitchWebsocketClient.cs b/Twitch/Socket/TwitchWebsocketClient.cs index 8e6f90a..46e6c0a 100644 --- a/Twitch/Socket/TwitchWebsocketClient.cs +++ b/Twitch/Socket/TwitchWebsocketClient.cs @@ -55,7 +55,7 @@ namespace TwitchChatTTS.Twitch.Socket _messageTypes.Add("notification", typeof(NotificationMessage)); UID = Guid.NewGuid().ToString("D"); - + if (_configuration.Environment == "PROD" || string.IsNullOrWhiteSpace(_configuration.Twitch?.WebsocketUrl)) URL = "wss://eventsub.wss.twitch.tv/ws"; else @@ -144,39 +144,42 @@ namespace TwitchChatTTS.Twitch.Socket }; } - protected override async Task OnResponseReceived(TwitchWebsocketMessage? message) + protected override Task OnResponseReceived(TwitchWebsocketMessage? message) { - if (message == null || message.Metadata == null) + return Task.Run(async () => { - _logger.Information("Twitch message is null"); - return; - } + if (message == null || message.Metadata == null) + { + _logger.Information("Twitch message is null"); + return; + } - _lastReceivedMessageTimestamp = DateTime.UtcNow; + _lastReceivedMessageTimestamp = DateTime.UtcNow; - string content = message.Payload?.ToString() ?? string.Empty; - if (message.Metadata.MessageType != "session_keepalive") - _logger.Debug("Twitch RX #" + message.Metadata.MessageType + ": " + content); + string content = message.Payload?.ToString() ?? string.Empty; + if (message.Metadata.MessageType != "session_keepalive") + _logger.Debug("Twitch RX #" + message.Metadata.MessageType + ": " + content); - if (!_messageTypes.TryGetValue(message.Metadata.MessageType, out var type) || type == null) - { - _logger.Debug($"Could not find Twitch message type [message type: {message.Metadata.MessageType}]"); - return; - } + if (!_messageTypes.TryGetValue(message.Metadata.MessageType, out var type) || type == null) + { + _logger.Debug($"Could not find Twitch message type [message type: {message.Metadata.MessageType}]"); + return; + } - if (!_handlers.TryGetValue(message.Metadata.MessageType, out ITwitchSocketHandler? handler) || handler == null) - { - _logger.Debug($"Could not find Twitch handler [message type: {message.Metadata.MessageType}]"); - return; - } + if (!_handlers.TryGetValue(message.Metadata.MessageType, out ITwitchSocketHandler? handler) || handler == null) + { + _logger.Debug($"Could not find Twitch handler [message type: {message.Metadata.MessageType}]"); + return; + } - var data = JsonSerializer.Deserialize(content, type, _options); - if (data == null) - { - _logger.Warning("Twitch websocket message payload is null."); - return; - } - await Task.Run(async () => await handler.Execute(this, data)); + var data = JsonSerializer.Deserialize(content, type, _options); + if (data == null) + { + _logger.Warning("Twitch websocket message payload is null."); + return; + } + await handler.Execute(this, data); + }); } public async Task Send(string type, T data)