Skip to content

Commit 3864955

Browse files
authored
Merge pull request #62 from AoshiW/HypeTrainV2
Add HypeTrainV2 events
2 parents cd91499 + 9abc281 commit 3864955

File tree

9 files changed

+124
-1
lines changed

9 files changed

+124
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
2+
using TwitchLib.EventSub.Websockets.Core.Models;
3+
4+
namespace TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;
5+
6+
public class ChannelHypeTrainBeginV2Args : TwitchLibEventSubEventArgs<EventSubNotification<HypeTrainBeginV2>>
7+
{ }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
2+
using TwitchLib.EventSub.Websockets.Core.Models;
3+
4+
namespace TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;
5+
6+
public class ChannelHypeTrainEndV2Args : TwitchLibEventSubEventArgs<EventSubNotification<HypeTrainEndV2>>
7+
{ }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
2+
using TwitchLib.EventSub.Websockets.Core.Models;
3+
4+
namespace TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;
5+
6+
public class ChannelHypeTrainProgressV2Args : TwitchLibEventSubEventArgs<EventSubNotification<HypeTrainProgressV2>>
7+
{ }

TwitchLib.EventSub.Websockets/EventSubWebsocketClient.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,30 @@ public class EventSubWebsocketClient
140140
/// <summary>
141141
/// Event that triggers on "channel.hype_train.begin" notifications
142142
/// </summary>
143+
[Obsolete("This event is deprecated, please use: ChannelHypeTrainBeginV2")]
143144
public event AsyncEventHandler<ChannelHypeTrainBeginArgs>? ChannelHypeTrainBegin;
144145
/// <summary>
146+
/// Event that triggers on "channel.hype_train.begin" notifications
147+
/// </summary>
148+
public event AsyncEventHandler<ChannelHypeTrainBeginV2Args>? ChannelHypeTrainBeginV2;
149+
/// <summary>
145150
/// Event that triggers on "channel.hype_train.end" notifications
146151
/// </summary>
152+
[Obsolete("This event is deprecated, please use: ChannelHypeTrainEndV2")]
147153
public event AsyncEventHandler<ChannelHypeTrainEndArgs>? ChannelHypeTrainEnd;
148154
/// <summary>
155+
/// Event that triggers on "channel.hype_train.end" notifications
156+
/// </summary>
157+
public event AsyncEventHandler<ChannelHypeTrainEndV2Args>? ChannelHypeTrainEndV2;
158+
/// <summary>
149159
/// Event that triggers on "channel.hype_train.progress" notifications
150160
/// </summary>
161+
[Obsolete("This event is deprecated, please use: ChannelHypeTrainProgressV2")]
151162
public event AsyncEventHandler<ChannelHypeTrainProgressArgs>? ChannelHypeTrainProgress;
163+
/// <summary>
164+
/// Event that triggers on "channel.hype_train.progress" notifications
165+
/// </summary>
166+
public event AsyncEventHandler<ChannelHypeTrainProgressV2Args>? ChannelHypeTrainProgressV2;
152167

153168
/// <summary>
154169
/// Event that triggers on "channel.moderator.add" notifications
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
2+
using TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;
3+
using TwitchLib.EventSub.Websockets.Core.Models;
4+
5+
namespace TwitchLib.EventSub.Websockets.Handler.Channel.HypeTrains;
6+
7+
internal class ChannelHypeTrainBeginHandlerV2 : NotificationHandler<ChannelHypeTrainBeginV2Args, EventSubNotification<HypeTrainBeginV2>>
8+
{
9+
public override string SubscriptionType => "channel.hype_train.begin";
10+
11+
public override string SubscriptionVersion => "2";
12+
13+
public override string EventName => nameof(EventSubWebsocketClient.ChannelHypeTrainBeginV2);
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
2+
using TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;
3+
using TwitchLib.EventSub.Websockets.Core.Models;
4+
5+
namespace TwitchLib.EventSub.Websockets.Handler.Channel.HypeTrains;
6+
7+
internal class ChannelHypeTrainEndHandlerV2 : NotificationHandler<ChannelHypeTrainEndV2Args, EventSubNotification<HypeTrainEndV2>>
8+
{
9+
public override string SubscriptionType => "channel.hype_train.end";
10+
11+
public override string SubscriptionVersion => "2";
12+
13+
public override string EventName => nameof(EventSubWebsocketClient.ChannelHypeTrainEndV2);
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using TwitchLib.EventSub.Core.SubscriptionTypes.Channel;
2+
using TwitchLib.EventSub.Websockets.Core.EventArgs.Channel;
3+
using TwitchLib.EventSub.Websockets.Core.Models;
4+
5+
namespace TwitchLib.EventSub.Websockets.Handler.Channel.HypeTrains;
6+
7+
internal class ChannelHypeTrainProgressHandlerV2 : NotificationHandler<ChannelHypeTrainProgressV2Args, EventSubNotification<HypeTrainProgressV2>>
8+
{
9+
public override string SubscriptionType => "channel.hype_train.progress";
10+
11+
public override string SubscriptionVersion => "2";
12+
13+
public override string EventName => nameof(EventSubWebsocketClient.ChannelHypeTrainProgressV2);
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Text.Json;
3+
using TwitchLib.EventSub.Websockets.Core.EventArgs;
4+
using TwitchLib.EventSub.Websockets.Core.Handler;
5+
6+
namespace TwitchLib.EventSub.Websockets.Handler;
7+
8+
9+
// internal so we can change it without breaking changes in the future
10+
internal abstract class NotificationHandler<TEvent, TModel> : INotificationHandler
11+
where TEvent : TwitchLibEventSubEventArgs<TModel>, new()
12+
where TModel : new()
13+
{
14+
/// <inheritdoc />
15+
public abstract string SubscriptionType { get; }
16+
17+
/// <inheritdoc />
18+
public abstract string SubscriptionVersion { get; }
19+
20+
/// <inheritdoc />
21+
public abstract string EventName { get; }
22+
23+
/// <inheritdoc />
24+
public void Handle(EventSubWebsocketClient client, string jsonString, JsonSerializerOptions serializerOptions)
25+
{
26+
#pragma warning disable CA1031 // Do not catch general exception types
27+
try
28+
{
29+
var data = JsonSerializer.Deserialize<TModel>(jsonString, serializerOptions);
30+
31+
if (data is null)
32+
throw new InvalidOperationException("Parsed JSON cannot be null!");
33+
34+
client.RaiseEvent("UserUpdate", new TEvent { Notification = data });
35+
}
36+
catch (Exception ex)
37+
{
38+
// TODO events are async, so we NEVER get here
39+
// possible solutions:
40+
// rewriting RaiseEvent to return: 'Task?' and await if not null
41+
client.RaiseEvent("ErrorOccurred", new ErrorOccuredArgs { Exception = ex, Message = $"Error encountered while trying to handle {SubscriptionType}/{SubscriptionVersion} notification! Raw Json: {jsonString}" });
42+
}
43+
#pragma warning restore CA1031 // Do not catch general exception types
44+
}
45+
}

TwitchLib.EventSub.Websockets/TwitchLib.EventSub.Websockets.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<ItemGroup>
3333
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
34-
<PackageReference Include="TwitchLib.EventSub.Core" Version="2.6.0" />
34+
<PackageReference Include="TwitchLib.EventSub.Core" Version="3.0.0-preview.58.b20c9f1" />
3535
<PackageReference Include="System.Text.Json" Version="8.0.5" />
3636
</ItemGroup>
3737

0 commit comments

Comments
 (0)