From a5f099ab919f6bf50e8d74802bce399b07c40ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=E2=96=88=E2=96=88=E2=96=88=E2=96=88=E2=96=88?= Date: Tue, 22 Oct 2024 23:59:54 -0400 Subject: [PATCH] Abstracting TwitchClientProxy -> ITwitchClientProxy nullinside-development-group/nullinside-api-twitch-bot#39 --- .../Model/NullinsideContextExtensions.cs | 5 ----- src/Nullinside.Api.TwitchBot/Program.cs | 1 + .../Services/MainService.cs | 16 +++++++++++----- src/nullinside-api | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs b/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs index a87b11b..02571f7 100644 --- a/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs +++ b/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs @@ -46,11 +46,6 @@ public static void Configure(this ITwitchApiProxy api, User user) { } await db.UpdateOAuthInDatabase(user.Id, api.OAuth, stoppingToken); - if (Constants.BotId.Equals(user.TwitchId, StringComparison.InvariantCultureIgnoreCase)) { - TwitchClientProxy.Instance.TwitchUsername = Constants.BotUsername; - TwitchClientProxy.Instance.TwitchOAuthToken = api.OAuth.AccessToken; - } - return api; } diff --git a/src/Nullinside.Api.TwitchBot/Program.cs b/src/Nullinside.Api.TwitchBot/Program.cs index 6d7c312..997df75 100644 --- a/src/Nullinside.Api.TwitchBot/Program.cs +++ b/src/Nullinside.Api.TwitchBot/Program.cs @@ -30,6 +30,7 @@ }), ServiceLifetime.Transient); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddSingleton(_ => TwitchClientProxy.Instance); builder.Services.AddHostedService(); builder.Services.AddAuthentication() .AddScheme("Bearer", _ => { }); diff --git a/src/Nullinside.Api.TwitchBot/Services/MainService.cs b/src/Nullinside.Api.TwitchBot/Services/MainService.cs index 9d067c6..32ebfd7 100644 --- a/src/Nullinside.Api.TwitchBot/Services/MainService.cs +++ b/src/Nullinside.Api.TwitchBot/Services/MainService.cs @@ -84,6 +84,11 @@ public class MainService : BackgroundService { /// private readonly ITwitchApiProxy _api; + /// + /// The twitch client for sending/receiving chat messages. + /// + private readonly ITwitchClientProxy _client; + /// /// Initializes a new instance of the class. /// @@ -93,6 +98,7 @@ public MainService(IServiceScopeFactory serviceScopeFactory) { _scope = _serviceScopeFactory.CreateScope(); _db = _scope.ServiceProvider.GetRequiredService(); _api = _scope.ServiceProvider.GetRequiredService(); + _client = _scope.ServiceProvider.GetRequiredService(); _chatMessageConsumer = new TwitchChatMessageMonitorConsumer(_db, _api, _receivedMessageProcessingQueue); } @@ -109,8 +115,8 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) { throw new Exception("Unable to log in as bot user"); } - TwitchClientProxy.Instance.TwitchUsername = Constants.BotUsername; - TwitchClientProxy.Instance.TwitchOAuthToken = botApi.OAuth?.AccessToken; + this._client.TwitchUsername = Constants.BotUsername; + this._client.TwitchOAuthToken = botApi.OAuth?.AccessToken; _botRules = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) @@ -133,7 +139,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) { /// The starting point for running the bots' functionality. /// /// The stopping token. - private async Task Main(CancellationToken stoppingToken) { + private async Task Main(CancellationToken stoppingToken = new()) { try { while (!stoppingToken.IsCancellationRequested) { using (IServiceScope scope = _serviceScopeFactory.CreateAsyncScope()) { @@ -174,8 +180,8 @@ private async Task Main(CancellationToken stoppingToken) { // its chat limits so that "verified bots" like us don't get special treatment anymore. The only thing // that skips the chat limits is if it's a channel you're a mod in. foreach (TwitchModeratedChannel channel in moddedChannels) { - await TwitchClientProxy.Instance.AddMessageCallback(channel.broadcaster_login, OnTwitchMessageReceived); - await TwitchClientProxy.Instance.AddBannedCallback(channel.broadcaster_login, OnTwitchBanReceived); + await this._client.AddMessageCallback(channel.broadcaster_login, OnTwitchMessageReceived); + await this._client.AddBannedCallback(channel.broadcaster_login, OnTwitchBanReceived); } } diff --git a/src/nullinside-api b/src/nullinside-api index b53da06..7b3026f 160000 --- a/src/nullinside-api +++ b/src/nullinside-api @@ -1 +1 @@ -Subproject commit b53da06991eb23fc4d46cf228b2542a5d23d470d +Subproject commit 7b3026f66c810988adf866629de667ef8106adfd