Skip to content

Commit 3b489ce

Browse files
committed
Began adding ChannelPoints notifications
1 parent 2ac593b commit 3b489ce

File tree

10 files changed

+459
-12
lines changed

10 files changed

+459
-12
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Fritz.StreamTools.Hubs;
2+
using Fritz.Twitch;
3+
using Fritz.Twitch.PubSub;
4+
using Microsoft.AspNetCore.SignalR;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Options;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Threading;
12+
using System.Threading.Tasks;
13+
14+
namespace Fritz.StreamTools.Services
15+
{
16+
public class TwitchPubSubService : IHostedService
17+
{
18+
19+
private readonly IHubContext<AttentionHub, IAttentionHubClient> _HubContext;
20+
private readonly Twitch.PubSub.Proxy _Proxy;
21+
private readonly ConfigurationSettings _Configuration;
22+
23+
public TwitchPubSubService(IHubContext<AttentionHub, IAttentionHubClient> hubContext, Twitch.PubSub.Proxy proxy, IOptions<ConfigurationSettings> settings)
24+
{
25+
_HubContext = hubContext;
26+
_Proxy = proxy;
27+
_Configuration = settings.Value;
28+
}
29+
30+
public Task StartAsync(CancellationToken cancellationToken)
31+
{
32+
_Proxy.OnChannelPointsRedeemed += _Proxy_OnChannelPointsRedeemed;
33+
return _Proxy.StartAsync(new TwitchTopic[] { TwitchTopic.ChannelPoints(_Configuration.UserId) }, cancellationToken);
34+
}
35+
36+
private void _Proxy_OnChannelPointsRedeemed(object sender, ChannelRedemption e)
37+
{
38+
_HubContext.Clients.All.PlaySoundEffect("pointsredeemed.mp3");
39+
}
40+
41+
public Task StopAsync(CancellationToken cancellationToken)
42+
{
43+
_Proxy.Dispose();
44+
return Task.CompletedTask;
45+
}
46+
}
47+
}

Fritz.StreamTools/Services/TwitchService.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class TwitchService : IHostedService, IStreamService, IChatService
2121
private IConfiguration Configuration { get; }
2222
public ILogger Logger { get; }
2323

24-
private readonly Proxy Proxy;
24+
private readonly Proxy _Proxy;
2525

2626
private ChatClient _ChatClient;
2727

@@ -37,7 +37,7 @@ public TwitchService(IConfiguration config, ILoggerFactory loggerFactory, Fritz.
3737
{
3838
this.Configuration = config;
3939
this.Logger = loggerFactory.CreateLogger("StreamServices");
40-
this.Proxy = proxy;
40+
this._Proxy = proxy;
4141
this._ChatClient = chatClient;
4242
}
4343

@@ -64,7 +64,7 @@ public int CurrentFollowerCount
6464

6565
public string Name { get { return "Twitch"; } }
6666

67-
public ValueTask<TimeSpan?> Uptime() => Proxy.Uptime();
67+
public ValueTask<TimeSpan?> Uptime() => _Proxy.Uptime();
6868

6969
public bool IsAuthenticated => true;
7070

@@ -79,13 +79,13 @@ private async Task StartTwitchMonitoring()
7979
_ChatClient.UserJoined += _ChatClient_UserJoined;
8080
_ChatClient.Init();
8181

82-
_CurrentFollowerCount = await Proxy.GetFollowerCountAsync();
83-
Proxy.NewFollowers += Proxy_NewFollowers;
84-
Proxy.WatchFollowers(10000);
82+
_CurrentFollowerCount = await _Proxy.GetFollowerCountAsync();
83+
_Proxy.NewFollowers += Proxy_NewFollowers;
84+
_Proxy.WatchFollowers(10000);
8585

86-
_CurrentViewerCount = await Proxy.GetViewerCountAsync();
87-
Proxy.NewViewers += Proxy_NewViewers;
88-
Proxy.WatchViewers();
86+
_CurrentViewerCount = await _Proxy.GetViewerCountAsync();
87+
_Proxy.NewViewers += Proxy_NewViewers;
88+
_Proxy.WatchViewers();
8989

9090
Logger.LogInformation($"Now monitoring Twitch with {_CurrentFollowerCount} followers and {_CurrentViewerCount} Viewers");
9191
}
@@ -216,7 +216,7 @@ private void Proxy_NewFollowers(object sender, NewFollowersEventArgs e)
216216
private Task StopTwitchMonitoring()
217217
{
218218

219-
Proxy.Dispose();
219+
_Proxy.Dispose();
220220
_ChatClient.Dispose();
221221

222222
return Task.CompletedTask;

Fritz.StreamTools/StartupServices/ConfigureServices.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Fritz.StreamTools.Services;
1111
using Fritz.StreamTools.TagHelpers;
1212
using Microsoft.AspNetCore.Mvc;
13+
using Microsoft.AspNetCore.SignalR;
1314
using Microsoft.Extensions.Configuration;
1415
using Microsoft.Extensions.DependencyInjection;
1516
using Microsoft.Extensions.Hosting;
@@ -35,10 +36,10 @@ public static void Execute(IServiceCollection services, IConfiguration configura
3536
services.Configure<FollowerGoalConfiguration>(configuration.GetSection("FollowerGoal"));
3637
services.Configure<FollowerCountConfiguration>(configuration.GetSection("FollowerCount"));
3738
services.Configure<Dictionary<string, SoundFxDefinition>>(configuration.GetSection("FritzBot:SoundFxCommands"));
38-
services.AddStreamingServices(configuration);
3939
services.Configure<GitHubConfiguration>(configuration.GetSection("GitHub"));
4040
services.AddSingleton<FollowerClient>();
4141
services.AddAspNetFeatures();
42+
services.AddStreamingServices(configuration);
4243

4344
services.AddSingleton<IConfigureOptions<SignalrTagHelperOptions>, ConfigureSignalrTagHelperOptions>();
4445
services.AddSingleton<SignalrTagHelperOptions>(cfg => cfg.GetService<IOptions<SignalrTagHelperOptions>>().Value);
@@ -137,6 +138,10 @@ private static void AddStreamService<TStreamService>(this IServiceCollection ser
137138
return;
138139
}
139140

141+
if (typeof(TStreamService) == typeof(TwitchService)) {
142+
services.AddSingleton<Twitch.PubSub.Proxy>();
143+
}
144+
140145
// Configure and grab a logger so that we can log information
141146
// about the creation of the services
142147
var provider = services.BuildServiceProvider(); // Build a 'temporary' instance of the DI container
@@ -148,10 +153,22 @@ private static void AddStreamService<TStreamService>(this IServiceCollection ser
148153
services.AddSingleton(service as IStreamService);
149154
services.AddSingleton(service);
150155

156+
if (typeof(TStreamService) == typeof(TwitchService))
157+
{
158+
var pubSub = new TwitchPubSubService(
159+
provider.GetRequiredService<IHubContext<AttentionHub, IAttentionHubClient>>(),
160+
provider.GetRequiredService<Twitch.PubSub.Proxy>(),
161+
provider.GetRequiredService<IOptions<Twitch.ConfigurationSettings>>());
162+
services.AddSingleton(pubSub as IHostedService);
163+
services.AddSingleton(pubSub);
164+
}
165+
166+
151167
if (service is IChatService chatService)
152168
{
153169
services.AddSingleton(chatService);
154170
}
171+
155172
}
156173

157174
/// <summary>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
3+
namespace Fritz.Twitch.PubSub
4+
{
5+
6+
public class ChannelRedemption : EventArgs, IPubSubData
7+
{
8+
public string timestamp { get; set; }
9+
public Redemption redemption { get; set; }
10+
}
11+
12+
public class Redemption
13+
{
14+
public string id { get; set; }
15+
public User user { get; set; }
16+
public string channel_id { get; set; }
17+
public string redeemed_at { get; set; }
18+
public Reward reward { get; set; }
19+
public string user_input { get; set; }
20+
public string status { get; set; }
21+
}
22+
23+
public class User
24+
{
25+
public string id { get; set; }
26+
public string login { get; set; }
27+
public string display_name { get; set; }
28+
}
29+
30+
public class Reward
31+
{
32+
public string id { get; set; }
33+
public string channel_id { get; set; }
34+
public string title { get; set; }
35+
public string prompt { get; set; }
36+
public int cost { get; set; }
37+
public bool is_user_input_required { get; set; }
38+
public bool is_sub_only { get; set; }
39+
public Image image { get; set; }
40+
public Default_Image default_image { get; set; }
41+
public string background_color { get; set; }
42+
public bool is_enabled { get; set; }
43+
public bool is_paused { get; set; }
44+
public bool is_in_stock { get; set; }
45+
public Max_Per_Stream max_per_stream { get; set; }
46+
public bool should_redemptions_skip_request_queue { get; set; }
47+
}
48+
49+
public class Image
50+
{
51+
public string url_1x { get; set; }
52+
public string url_2x { get; set; }
53+
public string url_4x { get; set; }
54+
}
55+
56+
public class Default_Image
57+
{
58+
public string url_1x { get; set; }
59+
public string url_2x { get; set; }
60+
public string url_4x { get; set; }
61+
}
62+
63+
public class Max_Per_Stream
64+
{
65+
public bool is_enabled { get; set; }
66+
public int max_per_stream { get; set; }
67+
}
68+
69+
70+
71+
}

0 commit comments

Comments
 (0)