@@ -31,11 +31,21 @@ public class MainService : BackgroundService {
3131 /// </summary>
3232 private static IBotRule [ ] ? _botRules ;
3333
34+ /// <summary>
35+ /// The twitch api.
36+ /// </summary>
37+ private readonly ITwitchApiProxy _api ;
38+
3439 /// <summary>
3540 /// Handles the enforcing rules on chat messages.
3641 /// </summary>
3742 private readonly TwitchChatMessageMonitorConsumer _chatMessageConsumer ;
3843
44+ /// <summary>
45+ /// The twitch client for sending/receiving chat messages.
46+ /// </summary>
47+ private readonly ITwitchClientProxy _client ;
48+
3949 /// <summary>
4050 /// The database.
4151 /// </summary>
@@ -79,16 +89,6 @@ public class MainService : BackgroundService {
7989 /// </summary>
8090 private readonly IServiceScopeFactory _serviceScopeFactory ;
8191
82- /// <summary>
83- /// The twitch api.
84- /// </summary>
85- private readonly ITwitchApiProxy _api ;
86-
87- /// <summary>
88- /// The twitch client for sending/receiving chat messages.
89- /// </summary>
90- private readonly ITwitchClientProxy _client ;
91-
9292 /// <summary>
9393 /// Initializes a new instance of the <see cref="MainService" /> class.
9494 /// </summary>
@@ -115,8 +115,8 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
115115 throw new Exception ( "Unable to log in as bot user" ) ;
116116 }
117117
118- this . _client . TwitchUsername = Constants . BotUsername ;
119- this . _client . TwitchOAuthToken = botApi . OAuth ? . AccessToken ;
118+ _client . TwitchUsername = Constants . BotUsername ;
119+ _client . TwitchOAuthToken = botApi . OAuth ? . AccessToken ;
120120
121121 _botRules = AppDomain . CurrentDomain . GetAssemblies ( )
122122 . SelectMany ( a => a . GetTypes ( ) )
@@ -152,13 +152,6 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
152152 if ( null == usersWithBotEnabled ) {
153153 continue ;
154154 }
155-
156- // Get the list of users that are banned.
157- List < User > ? bannedUsers = await GetBannedUsers ( db , stoppingToken ) ;
158- if ( null == bannedUsers ) {
159- bannedUsers = Enumerable . Empty < User > ( ) . ToList ( ) ;
160- }
161- var bannedUserIds = bannedUsers . Select ( u => u . TwitchId ) . ToList ( ) ;
162155
163156 // Get the bot user's information.
164157 User ? botUser = await db . Users . AsNoTracking ( )
@@ -167,11 +160,11 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
167160 throw new Exception ( "No bot user in database" ) ;
168161 }
169162
170- ITwitchApiProxy ? botApi = await db . ConfigureApiAndRefreshToken ( botUser , this . _api , stoppingToken ) ;
163+ ITwitchApiProxy ? botApi = await db . ConfigureApiAndRefreshToken ( botUser , _api , stoppingToken ) ;
171164 if ( null != botApi ) {
172165 // Ensure the twitch client has the most up-to-date password
173- this . _client . TwitchOAuthToken = botApi . OAuth ? . AccessToken ;
174-
166+ _client . TwitchOAuthToken = botApi . OAuth ? . AccessToken ;
167+
175168 // Trim channels that aren't live
176169 IEnumerable < string > liveUsers = await botApi . GetChannelsLive ( usersWithBotEnabled
177170 . Where ( u => null != u . TwitchId )
@@ -180,20 +173,20 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
180173
181174 // Trim channels we aren't a mod in
182175 IEnumerable < TwitchModeratedChannel > moddedChannels = await botApi . GetUserModChannels ( Constants . BotId ) ;
183- moddedChannels = moddedChannels . Where ( m => ! bannedUserIds . Contains ( m . broadcaster_id ) ) . ToList ( ) ;
184-
185176 usersWithBotEnabled = usersWithBotEnabled
186- . Where ( u => moddedChannels
187- . Select ( m => m . broadcaster_id )
188- . Contains ( u . TwitchId ) )
177+ . Where ( u => moddedChannels . Select ( m => m . broadcaster_id ) . Contains ( u . TwitchId ) )
189178 . ToList ( ) ;
190179
191180 // Join all the channels we're a mod in. Why do we limit it to channels we are a mod in? Twitch changed
192181 // its chat limits so that "verified bots" like us don't get special treatment anymore. The only thing
193182 // that skips the chat limits is if it's a channel you're a mod in.
194- foreach ( TwitchModeratedChannel channel in moddedChannels ) {
195- await this . _client . AddMessageCallback ( channel . broadcaster_login , OnTwitchMessageReceived ) ;
196- await this . _client . AddBannedCallback ( channel . broadcaster_login , OnTwitchBanReceived ) ;
183+ foreach ( User channel in usersWithBotEnabled ) {
184+ if ( string . IsNullOrWhiteSpace ( channel . TwitchUsername ) ) {
185+ continue ;
186+ }
187+
188+ await _client . AddMessageCallback ( channel . TwitchUsername , OnTwitchMessageReceived ) ;
189+ await _client . AddBannedCallback ( channel . TwitchUsername , OnTwitchBanReceived ) ;
197190 }
198191 }
199192
@@ -290,7 +283,7 @@ orderby user.TwitchLastScanned
290283 . AsNoTracking ( )
291284 . ToListAsync ( stoppingToken ) ;
292285 }
293-
286+
294287 /// <summary>
295288 /// Retrieve all users that are banned from using the bot.
296289 /// </summary>
@@ -325,7 +318,7 @@ private async Task DoScan(User user, User botUser, CancellationToken stoppingTok
325318 using ( IServiceScope scope = _serviceScopeFactory . CreateAsyncScope ( ) ) {
326319 await using ( var db = scope . ServiceProvider . GetRequiredService < INullinsideContext > ( ) ) {
327320 // Get the API
328- this . _api . Configure ( botUser ) ;
321+ _api . Configure ( botUser ) ;
329322 if ( null == _botRules || null == user . TwitchConfig ) {
330323 return ;
331324 }
@@ -334,7 +327,7 @@ private async Task DoScan(User user, User botUser, CancellationToken stoppingTok
334327 foreach ( IBotRule rule in _botRules ) {
335328 try {
336329 if ( rule . ShouldRun ( user . TwitchConfig ) ) {
337- await rule . Handle ( user , user . TwitchConfig , this . _api , db , stoppingToken ) ;
330+ await rule . Handle ( user , user . TwitchConfig , _api , db , stoppingToken ) ;
338331 }
339332 }
340333 catch ( Exception e ) {
0 commit comments