diff --git a/src/TrackerCouncil.Smz3.Chat.Integration/IChatClient.cs b/src/TrackerCouncil.Smz3.Chat.Integration/IChatClient.cs index d78da8190..158dd1a75 100644 --- a/src/TrackerCouncil.Smz3.Chat.Integration/IChatClient.cs +++ b/src/TrackerCouncil.Smz3.Chat.Integration/IChatClient.cs @@ -9,6 +9,8 @@ public interface IChatClient : IDisposable { event EventHandler? Connected; + event EventHandler? Reconnected; + event EventHandler? Disconnected; event EventHandler? SendMessageFailure; diff --git a/src/TrackerCouncil.Smz3.Chat.Twitch/TwitchChatClient.cs b/src/TrackerCouncil.Smz3.Chat.Twitch/TwitchChatClient.cs index 358eff0ed..cdd0c529b 100644 --- a/src/TrackerCouncil.Smz3.Chat.Twitch/TwitchChatClient.cs +++ b/src/TrackerCouncil.Smz3.Chat.Twitch/TwitchChatClient.cs @@ -8,6 +8,7 @@ using TrackerCouncil.Smz3.Chat.Integration.Models; using TrackerCouncil.Smz3.Chat.Twitch.Models; using TwitchLib.Client; +using TwitchLib.Client.Events; using TwitchLib.Client.Exceptions; using TwitchLib.Client.Models; @@ -17,6 +18,7 @@ public class TwitchChatClient : IChatClient { private readonly TwitchClient _twitchClient; private readonly IChatApi _chatApi; + private bool _firstConnection = true; public TwitchChatClient(ILogger logger, ILoggerFactory loggerFactory, IChatApi chatApi) { @@ -25,6 +27,8 @@ public TwitchChatClient(ILogger logger, ILoggerFactory loggerF _twitchClient.OnConnected += _twitchClient_OnConnected; _twitchClient.OnDisconnected += _twitchClient_OnDisconnected; _twitchClient.OnMessageReceived += _twitchClient_OnMessageReceived; + _twitchClient.OnReconnected += _twitchClient_OnReconnected; + _twitchClient.OnJoinedChannel += _twitchClient_OnJoinedChannel; _chatApi = chatApi; } @@ -32,6 +36,8 @@ public TwitchChatClient(ILogger logger, ILoggerFactory loggerF public event EventHandler? Disconnected; + public event EventHandler? Reconnected; + public event EventHandler? SendMessageFailure; public event MessageReceivedEventHandler? MessageReceived; @@ -109,6 +115,11 @@ protected virtual void OnConnected() Connected?.Invoke(this, new()); } + protected virtual void OnReconnected() + { + Reconnected?.Invoke(this, new()); + } + protected virtual void OnDisconnected() { Logger.LogWarning("Connection to chat lost"); @@ -131,7 +142,6 @@ protected virtual void Dispose(bool disposing) private void _twitchClient_OnConnected(object? sender, TwitchLib.Client.Events.OnConnectedArgs e) { IsConnected = true; - OnConnected(); } private void _twitchClient_OnDisconnected(object? sender, TwitchLib.Communication.Events.OnDisconnectedEventArgs e) @@ -148,6 +158,30 @@ private void _twitchClient_OnMessageReceived(object? sender, TwitchLib.Client.Ev OnMessageReceived(new MessageReceivedEventArgs(new TwitchChatMessage(e.ChatMessage))); } + private void _twitchClient_OnReconnected(object? sender, TwitchLib.Communication.Events.OnReconnectedEventArgs e) + { + // Unfortunately this fires before reconnecting is genuinely finished, so we have to wait before + // rejoining the original channel + Task.Run(async () => + { + await Task.Delay(TimeSpan.FromSeconds(3)); + _twitchClient.JoinChannel(Channel); + }); + } + + private void _twitchClient_OnJoinedChannel(object? sender, OnJoinedChannelArgs e) + { + if (_firstConnection) + { + OnConnected(); + _firstConnection = false; + } + else + { + OnReconnected(); + } + } + public async Task CreatePollAsync(string title, ICollection options, int duration) { // Create the poll object diff --git a/src/TrackerCouncil.Smz3.Data/Configuration/ConfigTypes/ChatConfig.cs b/src/TrackerCouncil.Smz3.Data/Configuration/ConfigTypes/ChatConfig.cs index 320a9394c..51ff0050a 100644 --- a/src/TrackerCouncil.Smz3.Data/Configuration/ConfigTypes/ChatConfig.cs +++ b/src/TrackerCouncil.Smz3.Data/Configuration/ConfigTypes/ChatConfig.cs @@ -18,6 +18,11 @@ public class ChatConfig : IMergeable /// public SchrodingersString? WhenConnected { get; init; } + /// + /// Gets the phrases to respond with when reconnected to chat. + /// + public SchrodingersString? WhenReconnected { get; init; } + /// /// Gets the phrases to respond with when disconnected to chat. /// diff --git a/src/TrackerCouncil.Smz3.Tracking/VoiceCommands/ChatIntegrationModule.cs b/src/TrackerCouncil.Smz3.Tracking/VoiceCommands/ChatIntegrationModule.cs index 5a0c89a02..5a8f3f410 100644 --- a/src/TrackerCouncil.Smz3.Tracking/VoiceCommands/ChatIntegrationModule.cs +++ b/src/TrackerCouncil.Smz3.Tracking/VoiceCommands/ChatIntegrationModule.cs @@ -53,6 +53,7 @@ public ChatIntegrationModule(TrackerBase tracker, IChatClient chatClient, IItemS _itemService = itemService; _timerService = timerService; ChatClient.Connected += ChatClient_Connected; + ChatClient.Reconnected += ChatClient_Reconnected; ChatClient.MessageReceived += ChatClient_MessageReceived; ChatClient.Disconnected += ChatClient_Disconnected; ChatClient.SendMessageFailure += ChatClient_SendMessageFailure; @@ -500,6 +501,11 @@ private void ChatClient_Disconnected(object? sender, EventArgs e) TrackerBase.Say(x => x.Chat.WhenDisconnected); } + private void ChatClient_Reconnected(object? sender, EventArgs e) + { + TrackerBase.Say(x => x.Chat.WhenReconnected); + } + private void ChatClient_SendMessageFailure(object? sender, EventArgs e) { TrackerBase.Error();