@@ -31,11 +31,21 @@ public class MainService : BackgroundService {
31
31
/// </summary>
32
32
private static IBotRule [ ] ? _botRules ;
33
33
34
+ /// <summary>
35
+ /// The twitch api.
36
+ /// </summary>
37
+ private readonly ITwitchApiProxy _api ;
38
+
34
39
/// <summary>
35
40
/// Handles the enforcing rules on chat messages.
36
41
/// </summary>
37
42
private readonly TwitchChatMessageMonitorConsumer _chatMessageConsumer ;
38
43
44
+ /// <summary>
45
+ /// The twitch client for sending/receiving chat messages.
46
+ /// </summary>
47
+ private readonly ITwitchClientProxy _client ;
48
+
39
49
/// <summary>
40
50
/// The database.
41
51
/// </summary>
@@ -79,16 +89,6 @@ public class MainService : BackgroundService {
79
89
/// </summary>
80
90
private readonly IServiceScopeFactory _serviceScopeFactory ;
81
91
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
-
92
92
/// <summary>
93
93
/// Initializes a new instance of the <see cref="MainService" /> class.
94
94
/// </summary>
@@ -115,8 +115,8 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
115
115
throw new Exception ( "Unable to log in as bot user" ) ;
116
116
}
117
117
118
- this . _client . TwitchUsername = Constants . BotUsername ;
119
- this . _client . TwitchOAuthToken = botApi . OAuth ? . AccessToken ;
118
+ _client . TwitchUsername = Constants . BotUsername ;
119
+ _client . TwitchOAuthToken = botApi . OAuth ? . AccessToken ;
120
120
121
121
_botRules = AppDomain . CurrentDomain . GetAssemblies ( )
122
122
. SelectMany ( a => a . GetTypes ( ) )
@@ -152,13 +152,6 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
152
152
if ( null == usersWithBotEnabled ) {
153
153
continue ;
154
154
}
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 ( ) ;
162
155
163
156
// Get the bot user's information.
164
157
User ? botUser = await db . Users . AsNoTracking ( )
@@ -167,11 +160,11 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
167
160
throw new Exception ( "No bot user in database" ) ;
168
161
}
169
162
170
- ITwitchApiProxy ? botApi = await db . ConfigureApiAndRefreshToken ( botUser , this . _api , stoppingToken ) ;
163
+ ITwitchApiProxy ? botApi = await db . ConfigureApiAndRefreshToken ( botUser , _api , stoppingToken ) ;
171
164
if ( null != botApi ) {
172
165
// 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
+
175
168
// Trim channels that aren't live
176
169
IEnumerable < string > liveUsers = await botApi . GetChannelsLive ( usersWithBotEnabled
177
170
. Where ( u => null != u . TwitchId )
@@ -180,20 +173,20 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
180
173
181
174
// Trim channels we aren't a mod in
182
175
IEnumerable < TwitchModeratedChannel > moddedChannels = await botApi . GetUserModChannels ( Constants . BotId ) ;
183
- moddedChannels = moddedChannels . Where ( m => ! bannedUserIds . Contains ( m . broadcaster_id ) ) . ToList ( ) ;
184
-
185
176
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 ) )
189
178
. ToList ( ) ;
190
179
191
180
// Join all the channels we're a mod in. Why do we limit it to channels we are a mod in? Twitch changed
192
181
// its chat limits so that "verified bots" like us don't get special treatment anymore. The only thing
193
182
// 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 ) ;
197
190
}
198
191
}
199
192
@@ -290,7 +283,7 @@ orderby user.TwitchLastScanned
290
283
. AsNoTracking ( )
291
284
. ToListAsync ( stoppingToken ) ;
292
285
}
293
-
286
+
294
287
/// <summary>
295
288
/// Retrieve all users that are banned from using the bot.
296
289
/// </summary>
@@ -325,7 +318,7 @@ private async Task DoScan(User user, User botUser, CancellationToken stoppingTok
325
318
using ( IServiceScope scope = _serviceScopeFactory . CreateAsyncScope ( ) ) {
326
319
await using ( var db = scope . ServiceProvider . GetRequiredService < INullinsideContext > ( ) ) {
327
320
// Get the API
328
- this . _api . Configure ( botUser ) ;
321
+ _api . Configure ( botUser ) ;
329
322
if ( null == _botRules || null == user . TwitchConfig ) {
330
323
return ;
331
324
}
@@ -334,7 +327,7 @@ private async Task DoScan(User user, User botUser, CancellationToken stoppingTok
334
327
foreach ( IBotRule rule in _botRules ) {
335
328
try {
336
329
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 ) ;
338
331
}
339
332
}
340
333
catch ( Exception e ) {
0 commit comments