Skip to content

Commit

Permalink
Merge branch 'dev' into chore/net8
Browse files Browse the repository at this point in the history
  • Loading branch information
Syzuna authored Apr 11, 2024
2 parents 6c7f23f + c59f15d commit 0f370ad
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
using TwitchLib.EventSub.Websockets.Core.Models;
namespace TwitchLib.EventSub.Websockets.Core.EventArgs.Channel
{
public class ChannelAdBreakBeginArgs : TwitchLibEventSubEventArgs<EventSubNotification<ChannelAdBreakBegin>>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
using TwitchLib.EventSub.Websockets.Core.Models;

namespace TwitchLib.EventSub.Websockets.Core.EventArgs.Channel
{
public class ChannelChatMessageArgs : TwitchLibEventSubEventArgs<EventSubNotification<ChannelChatMessage>>
{
}
}
40 changes: 24 additions & 16 deletions TwitchLib.EventSub.Websockets/EventSubWebsocketClient.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using TwitchLib.EventSub.Core;
using TwitchLib.EventSub.Core.Extensions;
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
using TwitchLib.EventSub.Websockets.Client;
using TwitchLib.EventSub.Websockets.Core.EventArgs;
using TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;
using TwitchLib.EventSub.Websockets.Core.EventArgs.Stream;
using TwitchLib.EventSub.Websockets.Core.EventArgs.User;
using TwitchLib.EventSub.Core;
using TwitchLib.EventSub.Core.Extensions;
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
using TwitchLib.EventSub.Websockets.Core.Handler;
using TwitchLib.EventSub.Websockets.Core.Models;
using TwitchLib.EventSub.Websockets.Extensions;
Expand Down Expand Up @@ -46,6 +46,11 @@ public class EventSubWebsocketClient
/// </summary>
public event AsyncEventHandler WebsocketReconnected;

/// <summary>
/// Event that triggers on "channel.ad_break.begin" notifications
/// </summary>
public event AsyncEventHandler<ChannelAdBreakBeginArgs> ChannelAdBreakBegin;

Check warning on line 52 in TwitchLib.EventSub.Websockets/EventSubWebsocketClient.cs

View workflow job for this annotation

GitHub Actions / check-buildstatus

The event 'EventSubWebsocketClient.ChannelAdBreakBegin' is never used

/// <summary>
/// Event that triggers on "channel.ban" notifications
/// </summary>
Expand All @@ -67,7 +72,10 @@ public class EventSubWebsocketClient
/// Event that triggers on "channel.charity_campaign.stop" notifications
/// </summary>
public event AsyncEventHandler<ChannelCharityCampaignStopArgs> ChannelCharityCampaignStop;

/// <summary>
/// Event that triggers on channel.chat.message notifications
/// </summary>
public event AsyncEventHandler<ChannelChatMessageArgs> ChannelChatMessage;
/// <summary>
/// Event that triggers on "channel.cheer" notifications
/// </summary>
Expand All @@ -89,7 +97,7 @@ public class EventSubWebsocketClient
/// Event that triggers on "channel.goal.progress" notifications
/// </summary>
public event AsyncEventHandler<ChannelGoalProgressArgs> ChannelGoalProgress;

/// <summary>
/// Event that triggers on "channel.guest_star_guest.update" notifications
/// </summary>
Expand Down Expand Up @@ -189,7 +197,7 @@ public class EventSubWebsocketClient
/// Event that triggers on "channel.raid" notifications
/// </summary>
public event AsyncEventHandler<ChannelRaidArgs> ChannelRaid;

Check warning on line 199 in TwitchLib.EventSub.Websockets/EventSubWebsocketClient.cs

View workflow job for this annotation

GitHub Actions / check-buildstatus

The event 'EventSubWebsocketClient.ChannelRaid' is never used

/// <summary>
/// Event that triggers on "channel.shield_mode.begin" notifications
/// </summary>
Expand All @@ -198,7 +206,7 @@ public class EventSubWebsocketClient
/// Event that triggers on "channel.shield_mode.end" notifications
/// </summary>
public event AsyncEventHandler<ChannelShieldModeEndArgs> ChannelShieldModeEnd;

/// <summary>
/// Event that triggers on "channel.shoutout.create" notifications
/// </summary>
Expand Down Expand Up @@ -310,30 +318,30 @@ public EventSubWebsocketClient(ILogger<EventSubWebsocketClient> logger, IEnumera
public EventSubWebsocketClient(ILoggerFactory loggerFactory = null)
{
_loggerFactory = loggerFactory;
_logger = _loggerFactory != null
? _loggerFactory.CreateLogger<EventSubWebsocketClient>()

_logger = _loggerFactory != null
? _loggerFactory.CreateLogger<EventSubWebsocketClient>()
: NullLogger<EventSubWebsocketClient>.Instance;

_websocketClient = _loggerFactory != null
_websocketClient = _loggerFactory != null
? new WebsocketClient(_loggerFactory.CreateLogger<WebsocketClient>())
: new WebsocketClient();

_websocketClient.OnDataReceived += OnDataReceived;
_websocketClient.OnErrorOccurred += OnErrorOccurred;

var handlers = typeof(INotificationHandler)
.Assembly.ExportedTypes
.Where(x => typeof(INotificationHandler).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
.Select(Activator.CreateInstance).Cast<INotificationHandler>()
.ToList();

PrepareHandlers(handlers);

_reconnectComplete = false;
_reconnectRequested = false;
}

/// <summary>
/// Connect to Twitch EventSub Websockets
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Text.Json;
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
using TwitchLib.EventSub.Websockets.Core.EventArgs;
using TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;
using TwitchLib.EventSub.Websockets.Core.Handler;
using TwitchLib.EventSub.Websockets.Core.Models;

namespace TwitchLib.EventSub.Websockets.Handler.Channel.Ads
{
/// <summary>
/// Handler for 'channel.ad_break.begin' notifications
/// </summary>
public class AdBreakBeginHandler : INotificationHandler
{
/// <inheritdoc />
public string SubscriptionType => "channel.ad_break.begin";

/// <inheritdoc />
public void Handle(EventSubWebsocketClient client, string jsonString, JsonSerializerOptions serializerOptions)
{
try
{
var data = JsonSerializer.Deserialize<EventSubNotification<ChannelAdBreakBegin>>(jsonString.AsSpan(), serializerOptions);
if (data is null)
throw new InvalidOperationException("Parsed JSON cannot be null!");
client.RaiseEvent("ChannelAdBreakBegin", new ChannelAdBreakBeginArgs { Notification = data });
}
catch (Exception ex)
{
client.RaiseEvent("ErrorOccurred", new ErrorOccuredArgs { Exception = ex, Message = $"Error encountered while trying to handle {SubscriptionType} notification! Raw Json: {jsonString}" });
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Text.Json;
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
using TwitchLib.EventSub.Websockets.Core.EventArgs;
using TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;
using TwitchLib.EventSub.Websockets.Core.Handler;
using TwitchLib.EventSub.Websockets.Core.Models;

namespace TwitchLib.EventSub.Websockets.Handler.Channel
{
public class ChatMessageHandler : INotificationHandler
{
public string SubscriptionType => "channel.chat.message";

public void Handle(EventSubWebsocketClient client, string jsonString, JsonSerializerOptions serializerOptions)
{
try
{
var data = JsonSerializer.Deserialize<EventSubNotification<ChannelChatMessage>>(jsonString.AsSpan(), serializerOptions);
if (data is null)
throw new InvalidOperationException("Parsed JSON cannot be null!");
client.RaiseEvent("ChannelChatMessage", new ChannelChatMessageArgs { Notification = data });
}
catch (Exception ex)
{
client.RaiseEvent("ErrorOccurred", new ErrorOccuredArgs { Exception = ex, Message = $"Error encountered while trying to handle {SubscriptionType} notification! Raw Json: {jsonString}" });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="TwitchLib.EventSub.Core" Version="2.5.2-preview-7658429" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
Expand Down

0 comments on commit 0f370ad

Please sign in to comment.