Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed / Implemented Twitch Channel Online Detection #279

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/FloppyBot.Chat.Agent/floppybot.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"ClientId": "",
"AccessToken": "",
"DisableWhenChannelIsOffline": true,
"MonitorInterface": 30
"MonitorInterface": 30,
"AnnounceChannelOnlineStatus": true
}
}
3 changes: 2 additions & 1 deletion src/FloppyBot.Chat.Twitch.Tests/TwitchChatInterfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public TwitchChatInterfaceTests()
"aclientid",
"anaccesstoken",
false,
0
0,
false
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/FloppyBot.Chat.Twitch/Config/TwitchConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/FloppyBot.Chat.Twitch/FloppyBot.Chat.Twitch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,17 @@
<ProjectReference Include="..\FloppyBot.Chat.Twitch.Events\FloppyBot.Chat.Twitch.Events.csproj" />
<ProjectReference Include="..\FloppyBot.Chat\FloppyBot.Chat.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Compile Update="Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ public interface ITwitchChannelOnlineMonitor
{
TwitchStream? Stream { get; }
bool IsChannelOnline();

event TwitchChannelOnlineStatusChangedDelegate OnlineStatusChanged;
event TwitchChannelStatusChangedDelegate StatusChanged;
}
39 changes: 32 additions & 7 deletions src/FloppyBot.Chat.Twitch/Monitor/TwitchChannelOnlineMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ TwitchConfiguration twitchConfiguration
_liveStreamMonitorService.OnStreamOnline += OnStreamOnline;
_liveStreamMonitorService.OnStreamUpdate += OnStreamUpdate;

_liveStreamMonitorService.SetChannelsByName(
new List<string> { twitchConfiguration.Channel }
);
_liveStreamMonitorService.SetChannelsByName([twitchConfiguration.Channel]);

_logger.LogInformation("Starting Live Stream Monitor ...");
_liveStreamMonitorService.Start();
Expand All @@ -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 ...");
Expand All @@ -61,20 +62,44 @@ public void Dispose()

private void OnStreamUpdate(object? sender, OnStreamUpdateArgs e)
{
_logger.LogInformation("Channel {ChannelName} has updated", e.Channel);
var stream = e.Stream.ToTwitchStream(false);
_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;

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);
_logger.LogTrace("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);
_logger.LogTrace("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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
);
Original file line number Diff line number Diff line change
@@ -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;
}
60 changes: 60 additions & 0 deletions src/FloppyBot.Chat.Twitch/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/FloppyBot.Chat.Twitch/Resources.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">

</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ChannelOnlineMessage" xml:space="preserve">
<value>MrDestructoid Hey, I'm online and ready to take your requests</value>
</data>
<data name="ChannelOfflineMessage" xml:space="preserve">
<value>MrDestructoid Going offline! See you next stream HeyGuys</value>
</data>
</root>
24 changes: 24 additions & 0 deletions src/FloppyBot.Chat.Twitch/TwitchChatInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ITwitchChannelOnlineMonitor onlineMonitor
_clientLogger = clientLogger;
_configuration = configuration;
_onlineMonitor = onlineMonitor;
_onlineMonitor.OnlineStatusChanged += OnlineMonitor_OnlineStatusChanged;

_channelIdentifier = new ChannelIdentifier(IF_NAME, _configuration.Channel);

Expand Down Expand Up @@ -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
);
}
}
Loading