66using Microsoft . Extensions . Configuration ;
77using Microsoft . Extensions . DependencyInjection ;
88using Microsoft . Extensions . Hosting ;
9+ using Microsoft . Extensions . Logging ;
910using Microsoft . Extensions . Options ;
1011using System ;
1112using System . Collections . Generic ;
@@ -27,15 +28,18 @@ public class TwitchPubSubService : IHostedService
2728 private readonly Twitch . PubSub . Proxy _Proxy ;
2829 private readonly ConfigurationSettings _Configuration ;
2930 private readonly IHostEnvironment _HostEnvironment ;
30- private readonly Dictionary < string , Action < IHubContext < AttentionHub , IAttentionHubClient > > > _ChannelPointActions = new Dictionary < string , Action < IHubContext < AttentionHub , IAttentionHubClient > > > ( ) ;
31+ private readonly ILogger _Logger ;
32+ private readonly Dictionary < string , Action < IHubContext < AttentionHub , IAttentionHubClient > > > _ChannelPointActions = new Dictionary < string , Action < IHubContext < AttentionHub , IAttentionHubClient > > > ( ) ;
3133
32- public TwitchPubSubService ( IServiceProvider serviceProvider , Twitch . PubSub . Proxy proxy , IHostEnvironment hostEnvironment , IOptions < ConfigurationSettings > settings )
34+ public TwitchPubSubService ( IServiceProvider serviceProvider , Twitch . PubSub . Proxy proxy , IHostEnvironment hostEnvironment , IOptions < ConfigurationSettings > settings , ILoggerFactory loggerFactory )
3335 {
3436 _ServiceProvider = serviceProvider ;
3537 _Proxy = proxy ;
3638 _Configuration = settings . Value ;
3739 _HostEnvironment = hostEnvironment ;
3840
41+ _Logger = loggerFactory . CreateLogger ( "TwitchPubSub" ) ;
42+
3943 InitializeChannelPointActions ( ) ;
4044
4145 }
@@ -47,6 +51,12 @@ private void InitializeChannelPointActions()
4751
4852 public Task StartAsync ( CancellationToken cancellationToken )
4953 {
54+
55+ if ( string . IsNullOrEmpty ( _Configuration . PubSubAuthToken ) ) {
56+ _Logger . LogError ( "Twitch PubSub token was not provided, unable to start this service" ) ;
57+ return Task . CompletedTask ;
58+ }
59+
5060 _Proxy . OnChannelPointsRedeemed += _Proxy_OnChannelPointsRedeemed ;
5161 _Token = cancellationToken ;
5262 _BackgroundTask = _Proxy . StartAsync ( new TwitchTopic [ ] { TwitchTopic . ChannelPoints ( _Configuration . UserId ) } , _Token ) ;
@@ -59,7 +69,10 @@ private void IdentifyOldManAudio()
5969
6070 var oldManPath = Path . Combine ( _HostEnvironment . ContentRootPath , "wwwroot" , "contents" , "oldman" ) ;
6171 var di = new DirectoryInfo ( oldManPath ) ;
62- _OldManSoundFx = di . GetFiles ( ) . Select ( f => f . Name ) . OrderBy ( x => Guid . NewGuid ( ) ) . ToArray ( ) ;
72+
73+ if ( di . Exists && di . GetFiles ( ) . Any ( ) ) {
74+ _OldManSoundFx = di . GetFiles ( ) . Select ( f => f . Name ) . OrderBy ( x => Guid . NewGuid ( ) ) . ToArray ( ) ;
75+ }
6376
6477 }
6578
0 commit comments