From 13873abff946e63581bb4c1a912ed9b3ecc891b3 Mon Sep 17 00:00:00 2001 From: Raphael Guntersweiler Date: Mon, 6 May 2024 20:49:50 +0200 Subject: [PATCH 1/2] implemented awake message for Twitch chat --- src/FloppyBot.Chat.Agent/floppybot.json | 3 +- .../TwitchChatInterfaceTests.cs | 3 +- .../Config/TwitchConfiguration.cs | 6 +- .../FloppyBot.Chat.Twitch.csproj | 13 ++++ .../Monitor/ITwitchChannelOnlineMonitor.cs | 3 + .../Monitor/TwitchChannelOnlineMonitor.cs | 33 ++++++++-- ...witchChannelOnlineStatusChangedDelegate.cs | 11 ++++ ...itchChannelOnlineStatusChangedEventArgs.cs | 12 ++++ .../Resources.Designer.cs | 60 +++++++++++++++++++ src/FloppyBot.Chat.Twitch/Resources.resx | 27 +++++++++ .../TwitchChatInterface.cs | 24 ++++++++ 11 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineStatusChangedDelegate.cs create mode 100644 src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineStatusChangedEventArgs.cs create mode 100644 src/FloppyBot.Chat.Twitch/Resources.Designer.cs create mode 100644 src/FloppyBot.Chat.Twitch/Resources.resx diff --git a/src/FloppyBot.Chat.Agent/floppybot.json b/src/FloppyBot.Chat.Agent/floppybot.json index f913de97..0d90f7fd 100644 --- a/src/FloppyBot.Chat.Agent/floppybot.json +++ b/src/FloppyBot.Chat.Agent/floppybot.json @@ -35,6 +35,7 @@ "ClientId": "", "AccessToken": "", "DisableWhenChannelIsOffline": true, - "MonitorInterface": 30 + "MonitorInterface": 30, + "AnnounceChannelOnlineStatus": true } } diff --git a/src/FloppyBot.Chat.Twitch.Tests/TwitchChatInterfaceTests.cs b/src/FloppyBot.Chat.Twitch.Tests/TwitchChatInterfaceTests.cs index 25df6fff..248a8671 100644 --- a/src/FloppyBot.Chat.Twitch.Tests/TwitchChatInterfaceTests.cs +++ b/src/FloppyBot.Chat.Twitch.Tests/TwitchChatInterfaceTests.cs @@ -37,7 +37,8 @@ public TwitchChatInterfaceTests() "aclientid", "anaccesstoken", false, - 0 + 0, + false ); } diff --git a/src/FloppyBot.Chat.Twitch/Config/TwitchConfiguration.cs b/src/FloppyBot.Chat.Twitch/Config/TwitchConfiguration.cs index 6fac9bb7..41ca3f21 100644 --- a/src/FloppyBot.Chat.Twitch/Config/TwitchConfiguration.cs +++ b/src/FloppyBot.Chat.Twitch/Config/TwitchConfiguration.cs @@ -7,13 +7,15 @@ public record TwitchConfiguration( string ClientId, string AccessToken, bool DisableWhenChannelIsOffline, - int MonitorInterval + int MonitorInterval, + bool AnnounceChannelOnlineStatus ) { [Obsolete("This constructor is only present for configuration purposes and should not be used")] // ReSharper disable once UnusedMember.Global public TwitchConfiguration() - : this(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, true, 30) { } + : this(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, true, 30, true) + { } public bool HasTwitchApiCredentials => !string.IsNullOrWhiteSpace(ClientId) && !string.IsNullOrWhiteSpace(AccessToken); diff --git a/src/FloppyBot.Chat.Twitch/FloppyBot.Chat.Twitch.csproj b/src/FloppyBot.Chat.Twitch/FloppyBot.Chat.Twitch.csproj index e1ff0075..5ba219af 100644 --- a/src/FloppyBot.Chat.Twitch/FloppyBot.Chat.Twitch.csproj +++ b/src/FloppyBot.Chat.Twitch/FloppyBot.Chat.Twitch.csproj @@ -14,4 +14,17 @@ + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + + True + True + Resources.resx + + \ No newline at end of file diff --git a/src/FloppyBot.Chat.Twitch/Monitor/ITwitchChannelOnlineMonitor.cs b/src/FloppyBot.Chat.Twitch/Monitor/ITwitchChannelOnlineMonitor.cs index c4ffaf44..dd836667 100644 --- a/src/FloppyBot.Chat.Twitch/Monitor/ITwitchChannelOnlineMonitor.cs +++ b/src/FloppyBot.Chat.Twitch/Monitor/ITwitchChannelOnlineMonitor.cs @@ -4,4 +4,7 @@ public interface ITwitchChannelOnlineMonitor { TwitchStream? Stream { get; } bool IsChannelOnline(); + + event TwitchChannelOnlineStatusChangedDelegate OnlineStatusChanged; + event TwitchChannelStatusChangedDelegate StatusChanged; } diff --git a/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineMonitor.cs b/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineMonitor.cs index 54df47c9..040b0685 100644 --- a/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineMonitor.cs +++ b/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineMonitor.cs @@ -27,9 +27,7 @@ TwitchConfiguration twitchConfiguration _liveStreamMonitorService.OnStreamOnline += OnStreamOnline; _liveStreamMonitorService.OnStreamUpdate += OnStreamUpdate; - _liveStreamMonitorService.SetChannelsByName( - new List { twitchConfiguration.Channel } - ); + _liveStreamMonitorService.SetChannelsByName([twitchConfiguration.Channel]); _logger.LogInformation("Starting Live Stream Monitor ..."); _liveStreamMonitorService.Start(); @@ -49,6 +47,9 @@ public bool IsChannelOnline() return _stream?.IsOnline ?? false; } + public event TwitchChannelOnlineStatusChangedDelegate? OnlineStatusChanged; + public event TwitchChannelStatusChangedDelegate? StatusChanged; + public void Dispose() { _logger.LogInformation("Stopping and disposing Live Stream Monitor ..."); @@ -62,19 +63,43 @@ public void Dispose() private void OnStreamUpdate(object? sender, OnStreamUpdateArgs e) { _logger.LogInformation("Channel {ChannelName} has updated", e.Channel); - var stream = e.Stream.ToTwitchStream(false); + var stream = e.Stream.ToTwitchStream(_stream?.IsOnline ?? false); + var onlineChanged = _stream?.IsOnline != stream.IsOnline; _stream = _stream != null ? stream with { IsOnline = _stream.IsOnline } : stream; + + var eventArgs = new TwitchChannelOnlineStatusChangedEventArgs(_stream); + StatusChanged?.Invoke(this, eventArgs); + if (onlineChanged) + { + OnlineStatusChanged?.Invoke(this, eventArgs); + } } private void OnStreamOnline(object? sender, OnStreamOnlineArgs e) { _logger.LogInformation("Channel {ChannelName} has come online", e.Channel); + var changed = _stream?.IsOnline != true; _stream = e.Stream.ToTwitchStream(true); + + var eventArgs = new TwitchChannelOnlineStatusChangedEventArgs(_stream); + StatusChanged?.Invoke(this, eventArgs); + if (changed) + { + OnlineStatusChanged?.Invoke(this, eventArgs); + } } private void OnStreamOffline(object? sender, OnStreamOfflineArgs e) { _logger.LogInformation("Channel {ChannelName} has come offline", e.Channel); + var changed = _stream?.IsOnline != false; _stream = e.Stream.ToTwitchStream(false); + + var eventArgs = new TwitchChannelOnlineStatusChangedEventArgs(_stream); + StatusChanged?.Invoke(this, eventArgs); + if (changed) + { + OnlineStatusChanged?.Invoke(this, eventArgs); + } } } diff --git a/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineStatusChangedDelegate.cs b/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineStatusChangedDelegate.cs new file mode 100644 index 00000000..760edfc8 --- /dev/null +++ b/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineStatusChangedDelegate.cs @@ -0,0 +1,11 @@ +namespace FloppyBot.Chat.Twitch.Monitor; + +public delegate void TwitchChannelOnlineStatusChangedDelegate( + ITwitchChannelOnlineMonitor sender, + TwitchChannelOnlineStatusChangedEventArgs eventArgs +); + +public delegate void TwitchChannelStatusChangedDelegate( + ITwitchChannelOnlineMonitor sender, + TwitchChannelOnlineStatusChangedEventArgs eventArgs +); diff --git a/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineStatusChangedEventArgs.cs b/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineStatusChangedEventArgs.cs new file mode 100644 index 00000000..0d1cf957 --- /dev/null +++ b/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineStatusChangedEventArgs.cs @@ -0,0 +1,12 @@ +namespace FloppyBot.Chat.Twitch.Monitor; + +public class TwitchChannelOnlineStatusChangedEventArgs : EventArgs +{ + public TwitchChannelOnlineStatusChangedEventArgs(TwitchStream? stream) + { + Stream = stream; + } + + public TwitchStream? Stream { get; } + public bool IsOnline => Stream?.IsOnline ?? false; +} diff --git a/src/FloppyBot.Chat.Twitch/Resources.Designer.cs b/src/FloppyBot.Chat.Twitch/Resources.Designer.cs new file mode 100644 index 00000000..3833c129 --- /dev/null +++ b/src/FloppyBot.Chat.Twitch/Resources.Designer.cs @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace FloppyBot.Chat.Twitch { + using System; + + + [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [System.Diagnostics.DebuggerNonUserCodeAttribute()] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static System.Resources.ResourceManager resourceMan; + + private static System.Globalization.CultureInfo resourceCulture; + + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Resources.ResourceManager ResourceManager { + get { + if (object.Equals(null, resourceMan)) { + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("FloppyBot.Chat.Twitch.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + internal static string ChannelOnlineMessage { + get { + return ResourceManager.GetString("ChannelOnlineMessage", resourceCulture); + } + } + + internal static string ChannelOfflineMessage { + get { + return ResourceManager.GetString("ChannelOfflineMessage", resourceCulture); + } + } + } +} diff --git a/src/FloppyBot.Chat.Twitch/Resources.resx b/src/FloppyBot.Chat.Twitch/Resources.resx new file mode 100644 index 00000000..27f4a401 --- /dev/null +++ b/src/FloppyBot.Chat.Twitch/Resources.resx @@ -0,0 +1,27 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + MrDestructoid Hey, I'm online and ready to take your requests + + + MrDestructoid Going offline! See you next stream HeyGuys + + \ No newline at end of file diff --git a/src/FloppyBot.Chat.Twitch/TwitchChatInterface.cs b/src/FloppyBot.Chat.Twitch/TwitchChatInterface.cs index 200fe51e..ac61be94 100644 --- a/src/FloppyBot.Chat.Twitch/TwitchChatInterface.cs +++ b/src/FloppyBot.Chat.Twitch/TwitchChatInterface.cs @@ -37,6 +37,7 @@ ITwitchChannelOnlineMonitor onlineMonitor _clientLogger = clientLogger; _configuration = configuration; _onlineMonitor = onlineMonitor; + _onlineMonitor.OnlineStatusChanged += OnlineMonitor_OnlineStatusChanged; _channelIdentifier = new ChannelIdentifier(IF_NAME, _configuration.Channel); @@ -361,4 +362,27 @@ private void Client_OnReconnected(object? sender, OnReconnectedEventArgs e) { _logger.LogInformation("Reconnected"); } + + private void OnlineMonitor_OnlineStatusChanged( + ITwitchChannelOnlineMonitor sender, + TwitchChannelOnlineStatusChangedEventArgs e + ) + { + _logger.LogInformation( + "Channel online status changed to {IsChannelOnline}: {TwitchStream}", + e.IsOnline, + e.Stream + ); + + if (e.Stream is null || !_configuration.AnnounceChannelOnlineStatus) + { + return; + } + + var channelId = new ChannelIdentifier(IF_NAME, e.Stream.ChannelName); + SendMessage( + channelId, + e.IsOnline ? Resources.ChannelOnlineMessage : Resources.ChannelOfflineMessage + ); + } } From 292e96ab6ca7b15d86ea1430df5d81c1738a333a Mon Sep 17 00:00:00 2001 From: Raphael Guntersweiler Date: Mon, 6 May 2024 21:06:24 +0200 Subject: [PATCH 2/2] made logging verbose --- .../Monitor/TwitchChannelOnlineMonitor.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineMonitor.cs b/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineMonitor.cs index 040b0685..ab96c7f0 100644 --- a/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineMonitor.cs +++ b/src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineMonitor.cs @@ -62,7 +62,7 @@ public void Dispose() private void OnStreamUpdate(object? sender, OnStreamUpdateArgs e) { - _logger.LogInformation("Channel {ChannelName} has updated", e.Channel); + _logger.LogTrace("Channel {ChannelName} has updated", e.Channel); var stream = e.Stream.ToTwitchStream(_stream?.IsOnline ?? false); var onlineChanged = _stream?.IsOnline != stream.IsOnline; _stream = _stream != null ? stream with { IsOnline = _stream.IsOnline } : stream; @@ -77,7 +77,7 @@ private void OnStreamUpdate(object? sender, OnStreamUpdateArgs e) private void OnStreamOnline(object? sender, OnStreamOnlineArgs e) { - _logger.LogInformation("Channel {ChannelName} has come online", e.Channel); + _logger.LogTrace("Channel {ChannelName} has come online", e.Channel); var changed = _stream?.IsOnline != true; _stream = e.Stream.ToTwitchStream(true); @@ -91,7 +91,7 @@ private void OnStreamOnline(object? sender, OnStreamOnlineArgs e) private void OnStreamOffline(object? sender, OnStreamOfflineArgs e) { - _logger.LogInformation("Channel {ChannelName} has come offline", e.Channel); + _logger.LogTrace("Channel {ChannelName} has come offline", e.Channel); var changed = _stream?.IsOnline != false; _stream = e.Stream.ToTwitchStream(false);