Skip to content

Commit 2f6be32

Browse files
Merge pull request #42 from nullinside-development-group/feature/Poc2Prod
Abstracting TwitchClientProxy -> ITwitchClientProxy
2 parents 0b8c24d + a5f099a commit 2f6be32

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ public static void Configure(this ITwitchApiProxy api, User user) {
4646
}
4747

4848
await db.UpdateOAuthInDatabase(user.Id, api.OAuth, stoppingToken);
49-
if (Constants.BotId.Equals(user.TwitchId, StringComparison.InvariantCultureIgnoreCase)) {
50-
TwitchClientProxy.Instance.TwitchUsername = Constants.BotUsername;
51-
TwitchClientProxy.Instance.TwitchOAuthToken = api.OAuth.AccessToken;
52-
}
53-
5449
return api;
5550
}
5651

src/Nullinside.Api.TwitchBot/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
}), ServiceLifetime.Transient);
3131
builder.Services.AddScoped<IAuthorizationHandler, BasicAuthorizationHandler>();
3232
builder.Services.AddScoped<ITwitchApiProxy, TwitchApiProxy>();
33+
builder.Services.AddSingleton<ITwitchClientProxy>(_ => TwitchClientProxy.Instance);
3334
builder.Services.AddHostedService<MainService>();
3435
builder.Services.AddAuthentication()
3536
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("Bearer", _ => { });

src/Nullinside.Api.TwitchBot/Services/MainService.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public class MainService : BackgroundService {
8484
/// </summary>
8585
private readonly ITwitchApiProxy _api;
8686

87+
/// <summary>
88+
/// The twitch client for sending/receiving chat messages.
89+
/// </summary>
90+
private readonly ITwitchClientProxy _client;
91+
8792
/// <summary>
8893
/// Initializes a new instance of the <see cref="MainService" /> class.
8994
/// </summary>
@@ -93,6 +98,7 @@ public MainService(IServiceScopeFactory serviceScopeFactory) {
9398
_scope = _serviceScopeFactory.CreateScope();
9499
_db = _scope.ServiceProvider.GetRequiredService<INullinsideContext>();
95100
_api = _scope.ServiceProvider.GetRequiredService<ITwitchApiProxy>();
101+
_client = _scope.ServiceProvider.GetRequiredService<ITwitchClientProxy>();
96102
_chatMessageConsumer = new TwitchChatMessageMonitorConsumer(_db, _api, _receivedMessageProcessingQueue);
97103
}
98104

@@ -109,8 +115,8 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
109115
throw new Exception("Unable to log in as bot user");
110116
}
111117

112-
TwitchClientProxy.Instance.TwitchUsername = Constants.BotUsername;
113-
TwitchClientProxy.Instance.TwitchOAuthToken = botApi.OAuth?.AccessToken;
118+
this._client.TwitchUsername = Constants.BotUsername;
119+
this._client.TwitchOAuthToken = botApi.OAuth?.AccessToken;
114120

115121
_botRules = AppDomain.CurrentDomain.GetAssemblies()
116122
.SelectMany(a => a.GetTypes())
@@ -133,7 +139,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
133139
/// The starting point for running the bots' functionality.
134140
/// </summary>
135141
/// <param name="stoppingToken">The stopping token.</param>
136-
private async Task Main(CancellationToken stoppingToken) {
142+
private async Task Main(CancellationToken stoppingToken = new()) {
137143
try {
138144
while (!stoppingToken.IsCancellationRequested) {
139145
using (IServiceScope scope = _serviceScopeFactory.CreateAsyncScope()) {
@@ -174,8 +180,8 @@ private async Task Main(CancellationToken stoppingToken) {
174180
// its chat limits so that "verified bots" like us don't get special treatment anymore. The only thing
175181
// that skips the chat limits is if it's a channel you're a mod in.
176182
foreach (TwitchModeratedChannel channel in moddedChannels) {
177-
await TwitchClientProxy.Instance.AddMessageCallback(channel.broadcaster_login, OnTwitchMessageReceived);
178-
await TwitchClientProxy.Instance.AddBannedCallback(channel.broadcaster_login, OnTwitchBanReceived);
183+
await this._client.AddMessageCallback(channel.broadcaster_login, OnTwitchMessageReceived);
184+
await this._client.AddBannedCallback(channel.broadcaster_login, OnTwitchBanReceived);
179185
}
180186
}
181187

0 commit comments

Comments
 (0)