diff --git a/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs b/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs index f71c29d..e9a6dc5 100644 --- a/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs +++ b/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs @@ -30,7 +30,7 @@ public abstract class ABotRule : IBotRule { /// The database. /// The cancellation token. /// An asynchronous task. - public abstract Task Handle(User user, TwitchUserConfig config, TwitchApiProxy botProxy, + public abstract Task Handle(User user, TwitchUserConfig config, ITwitchApiProxy botProxy, INullinsideContext db, CancellationToken stoppingToken = new()); /// @@ -44,7 +44,7 @@ public abstract Task Handle(User user, TwitchUserConfig config, TwitchApiProxy b /// The reason for the ban. /// The cancellation token. /// A collection of confirmed banned users. - protected virtual async Task?> BanOnce(TwitchApiProxy botProxy, INullinsideContext db, + protected virtual async Task?> BanOnce(ITwitchApiProxy botProxy, INullinsideContext db, string channelId, IEnumerable<(string Id, string Username)> users, string reason, CancellationToken stoppingToken = new()) { // Get the list of everyone to ban @@ -68,7 +68,7 @@ where string.Equals(bannedUsers.ChannelId, channelId) && // Perform the ban and get the list of people actually banned IEnumerable confirmedBans = - await botProxy.BanUsers(channelId, Constants.BotId, bansToTry, reason, stoppingToken); + await botProxy.BanChannelUsers(channelId, Constants.BotId, bansToTry, reason, stoppingToken); await db.SaveTwitchBans(channelId, users, reason, stoppingToken); return confirmedBans; diff --git a/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs b/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs index eaab64b..c1dfb4b 100644 --- a/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs +++ b/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs @@ -69,7 +69,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// The twitch api authenticated as the bot user. /// The database. /// The cancellation token. - public override async Task Handle(User user, TwitchUserConfig config, TwitchApiProxy botProxy, + public override async Task Handle(User user, TwitchUserConfig config, ITwitchApiProxy botProxy, INullinsideContext db, CancellationToken stoppingToken = new()) { if (null == user.TwitchId) { return; @@ -79,7 +79,7 @@ public override async Task Handle(User user, TwitchUserConfig config, TwitchApiP // Get the list of people in the chat. List? chatters = - (await botProxy.GetChattersInChannel(user.TwitchId, Constants.BotId, stoppingToken))?.ToList(); + (await botProxy.GetChannelUsers(user.TwitchId, Constants.BotId, stoppingToken))?.ToList(); if (null == chatters || chatters.Count == 0) { return; } diff --git a/src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs b/src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs index a6b5716..1a5d7bd 100644 --- a/src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs +++ b/src/Nullinside.Api.TwitchBot/Bots/IBotRule.cs @@ -25,6 +25,6 @@ public interface IBotRule { /// The database. /// The cancellation token. /// An asynchronous task. - public Task Handle(User user, TwitchUserConfig config, TwitchApiProxy botProxy, + public Task Handle(User user, TwitchUserConfig config, ITwitchApiProxy botProxy, INullinsideContext db, CancellationToken stoppingToken = new()); } \ No newline at end of file diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs b/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs index f4222b7..534a151 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs @@ -16,7 +16,7 @@ public abstract class AChatRule : IChatRule { public abstract bool ShouldRun(TwitchUserConfig config); /// - public abstract Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, + public abstract Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()); /// @@ -28,10 +28,10 @@ public abstract Task Handle(string channelId, TwitchApiProxy botProxy, Cha /// The ban reason. /// The database. /// The cancellation token. - public async Task BanAndLog(string channelId, TwitchApiProxy botProxy, + public async Task BanAndLog(string channelId, ITwitchApiProxy botProxy, IEnumerable<(string Id, string Username)> users, string reason, INullinsideContext db, CancellationToken stoppingToken = new()) { - await botProxy.BanUsers(channelId, Constants.BotId, users, reason, stoppingToken); + await botProxy.BanChannelUsers(channelId, Constants.BotId, users, reason, stoppingToken); await db.SaveTwitchBans(channelId, users, reason, stoppingToken); } } \ No newline at end of file diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs index c3eef79..a6e10f4 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs @@ -26,7 +26,7 @@ public override bool ShouldRun(TwitchUserConfig config) { } /// - public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, + public override async Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { if (!message.IsFirstMessage) { return true; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs index 5da9979..5904020 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs @@ -22,7 +22,7 @@ public override bool ShouldRun(TwitchUserConfig config) { } /// - public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, + public override async Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { if (!message.IsFirstMessage) { return true; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs index 5ba2bff..2cb813f 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs @@ -16,7 +16,7 @@ public override bool ShouldRun(TwitchUserConfig config) { } /// - public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, + public override async Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { // The number of spaces per message may chance, so normalize that and lowercase it for comparison. string normalized = string.Concat(message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s))) diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs b/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs index 0068949..2ebb1a2 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs @@ -16,7 +16,7 @@ public override bool ShouldRun(TwitchUserConfig config) { } /// - public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, + public override async Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { // The number of spaces per message may change, so normalize that and lowercase it for comparison. string normalized = string.Concat(message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s))) diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs b/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs index d3cfda6..013dfb5 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs @@ -27,6 +27,6 @@ public interface IChatRule { /// The database. /// The cancellation token. /// An asynchronous task. - public Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, + public Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()); } \ No newline at end of file diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs index ea965fa..6bd8c26 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs @@ -23,7 +23,7 @@ public override bool ShouldRun(TwitchUserConfig config) { } /// - public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, + public override async Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { if (!message.IsFirstMessage) { return true; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs index 18165f4..add6111 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs @@ -19,7 +19,7 @@ public override bool ShouldRun(TwitchUserConfig config) { } /// - public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, + public override async Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { if (message.IsFirstMessage && (message.Message.TrimStart().StartsWith(_spam, StringComparison.InvariantCultureIgnoreCase) || diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs b/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs index 661e78f..5a8e111 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs @@ -22,7 +22,7 @@ public override bool ShouldRun(TwitchUserConfig config) { } /// - public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, + public override async Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { if (message.IsFirstMessage && _spam.Equals(message.Message, StringComparison.InvariantCultureIgnoreCase)) { await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) }, diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs index 2035608..4bc0522 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs @@ -21,7 +21,7 @@ public override bool ShouldRun(TwitchUserConfig config) { } /// - public override async Task Handle(string channelId, TwitchApiProxy botProxy, ChatMessage message, + public override async Task Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { if (!message.IsFirstMessage) { return true; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs b/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs index daab287..0991215 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs @@ -53,14 +53,21 @@ public class TwitchChatMessageMonitorConsumer : IDisposable { /// private bool _poisonPill; + /// + /// The twitch api. + /// + private readonly ITwitchApiProxy _api; + /// /// Initializes a new instance of the class. /// /// The database. + /// The twitch api. /// The non-priority queue to scan messages from. - public TwitchChatMessageMonitorConsumer(INullinsideContext db, BlockingCollection queue) { + public TwitchChatMessageMonitorConsumer(INullinsideContext db, ITwitchApiProxy api, BlockingCollection queue) { _db = db; _queue = queue; + _api = api; _thread = new Thread(MainLoop) { IsBackground = true, @@ -138,7 +145,7 @@ private async void MainLoop() { } // Get the bot proxy - TwitchApiProxy? botProxy = await _db.GetBotApiAndRefreshToken(); + ITwitchApiProxy? botProxy = await _db.ConfigureBotApiAndRefreshToken(this._api); if (null == botProxy) { continue; } diff --git a/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs b/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs index b35acc1..927d1e1 100644 --- a/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs +++ b/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs @@ -8,6 +8,7 @@ using Nullinside.Api.Common.Twitch; using Nullinside.Api.Model; using Nullinside.Api.Model.Ddl; +using Nullinside.Api.TwitchBot.Model; using TwitchLib.Api.Helix.Models.Moderation.GetModerators; @@ -49,11 +50,12 @@ public BotController(INullinsideContext dbContext, IConfiguration configuration) /// /// Checks if the bot account is a moderator. /// + /// The twitch api. /// The cancellation token. /// True if they are a mod, false otherwise. [HttpGet] [Route("mod")] - public async Task IsMod(CancellationToken token) { + public async Task IsMod([FromServices] ITwitchApiProxy api, CancellationToken token = new()) { Claim? userId = HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.UserData); if (null == userId) { return Unauthorized(); @@ -64,9 +66,9 @@ public async Task IsMod(CancellationToken token) { null == user.TwitchTokenExpiration || null == user.TwitchId) { return Unauthorized(); } - - var api = new TwitchApiProxy(user.TwitchToken, user.TwitchRefreshToken, user.TwitchTokenExpiration.Value); - IEnumerable mods = await api.GetMods(user.TwitchId, token); + + api.Configure(user); + IEnumerable mods = await api.GetChannelMods(user.TwitchId, token); return Ok(new { isMod = null != mods.FirstOrDefault(m => string.Equals(m.UserId, Constants.BotId, StringComparison.InvariantCultureIgnoreCase)) @@ -76,11 +78,12 @@ public async Task IsMod(CancellationToken token) { /// /// Mods the bot account. /// + /// The twitch api. /// The cancellation token. /// True if they are a mod, false otherwise. [HttpPost] [Route("mod")] - public async Task ModBotAccount(CancellationToken token) { + public async Task ModBotAccount([FromServices] ITwitchApiProxy api, CancellationToken token) { Claim? userId = HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.UserData); if (null == userId) { return Unauthorized(); @@ -91,9 +94,9 @@ public async Task ModBotAccount(CancellationToken token) { null == user.TwitchTokenExpiration || null == user.TwitchId) { return Unauthorized(); } - - var api = new TwitchApiProxy(user.TwitchToken, user.TwitchRefreshToken, user.TwitchTokenExpiration.Value); - bool success = await api.ModAccount(user.TwitchId, Constants.BotId, token); + + api.Configure(user); + bool success = await api.AddChannelMod(user.TwitchId, Constants.BotId, token); return Ok(success); } diff --git a/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs b/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs index 8157813..55d48e9 100644 --- a/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs +++ b/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs @@ -45,6 +45,7 @@ public LoginController(INullinsideContext dbContext, IConfiguration configuratio /// redirects users back to the nullinside website. /// /// The credentials provided by twitch. + /// The twitch api. /// The cancellation token. /// /// A redirect to the nullinside website. @@ -56,10 +57,9 @@ public LoginController(INullinsideContext dbContext, IConfiguration configuratio [AllowAnonymous] [HttpGet] [Route("twitch-login")] - public async Task TwitchLogin([FromQuery] string code, CancellationToken token) { + public async Task TwitchLogin([FromQuery] string code, [FromServices] ITwitchApiProxy api, CancellationToken token) { string? siteUrl = _configuration.GetValue("Api:SiteUrl"); - var api = new TwitchApiProxy(); - if (!await api.GetAccessToken(code, token)) { + if (null == await api.CreateAccessToken(code, token)) { return Redirect($"{siteUrl}/twitch-bot/login?error=3"); } diff --git a/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs b/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs index f976bc0..a87b11b 100644 --- a/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs +++ b/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs @@ -14,9 +14,10 @@ public static class NullinsideContextExtensions { /// Gets a twitch api proxy. /// /// The user to configure the proxy as. + /// The twitch api object currently in use. /// The twitch api. - public static TwitchApiProxy GetApi(this User user) { - return new TwitchApiProxy { + public static void Configure(this ITwitchApiProxy api, User user) { + api.OAuth = new() { AccessToken = user.TwitchToken, RefreshToken = user.TwitchRefreshToken, ExpiresUtc = user.TwitchTokenExpiration @@ -28,48 +29,61 @@ public static TwitchApiProxy GetApi(this User user) { /// /// The database. /// The user to configure the twitch api as. + /// The twitch api. /// The stopping token. /// The twitch api. - public static async Task GetApiAndRefreshToken(this INullinsideContext db, User user, - CancellationToken stoppingToken = new()) { - // Get the API - TwitchApiProxy api = GetApi(user); + public static async Task ConfigureApiAndRefreshToken(this INullinsideContext db, User user, + ITwitchApiProxy api, CancellationToken stoppingToken = new()) { + api.Configure(user); // Refresh its token if necessary. if (!(DateTime.UtcNow + TimeSpan.FromHours(1) > user.TwitchTokenExpiration)) { return api; } - if (!await api.RefreshTokenAsync(stoppingToken)) { + if (null == await api.RefreshAccessToken(stoppingToken) || null == api.OAuth) { return api; } - User? row = await db.Users.FirstOrDefaultAsync(u => u.Id == user.Id, stoppingToken); - if (null == row) { - return null; - } - - row.TwitchToken = api.AccessToken; - row.TwitchRefreshToken = api.RefreshToken; - row.TwitchTokenExpiration = api.ExpiresUtc; - await db.SaveChangesAsync(stoppingToken); - + 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.AccessToken; + TwitchClientProxy.Instance.TwitchOAuthToken = api.OAuth.AccessToken; } return api; } + /// + /// Updates the OAuth of a user in the database. + /// + /// The database. + /// The user whose OAuth should be updated. + /// The OAuth information. + /// The stopping token. + /// The number of state entries written to the database. + public static async Task UpdateOAuthInDatabase(this INullinsideContext db, int userId, + TwitchAccessToken oAuth, CancellationToken stoppingToken = new()) { + User? row = await db.Users.FirstOrDefaultAsync(u => u.Id == userId, stoppingToken); + if (null == row) { + return -1; + } + + row.TwitchToken = oAuth.AccessToken; + row.TwitchRefreshToken = oAuth.RefreshToken; + row.TwitchTokenExpiration = oAuth.ExpiresUtc; + return await db.SaveChangesAsync(stoppingToken); + } + /// /// Gets a twitch api proxy for the bot user and refreshes its token if necessary. /// /// The database. + /// The twitch api. /// The stopping token. /// The twitch api. - public static async Task GetBotApiAndRefreshToken(this INullinsideContext db, - CancellationToken stoppingToken = new()) { + public static async Task ConfigureBotApiAndRefreshToken(this INullinsideContext db, + ITwitchApiProxy api, CancellationToken stoppingToken = new()) { // Get the bot user's information. User? botUser = await db.Users.AsNoTracking() .FirstOrDefaultAsync(u => u.TwitchId == Constants.BotId, stoppingToken); @@ -77,7 +91,7 @@ public static TwitchApiProxy GetApi(this User user) { throw new Exception("No bot user in database"); } - return await GetApiAndRefreshToken(db, botUser, stoppingToken); + return await ConfigureApiAndRefreshToken(db, botUser, api, stoppingToken); } /// diff --git a/src/Nullinside.Api.TwitchBot/Program.cs b/src/Nullinside.Api.TwitchBot/Program.cs index ca8ef0d..6d7c312 100644 --- a/src/Nullinside.Api.TwitchBot/Program.cs +++ b/src/Nullinside.Api.TwitchBot/Program.cs @@ -7,6 +7,7 @@ using Nullinside.Api.Common; using Nullinside.Api.Common.AspNetCore.Middleware; +using Nullinside.Api.Common.Twitch; using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Services; @@ -28,6 +29,7 @@ builder.EnableRetryOnFailure(3); }), ServiceLifetime.Transient); builder.Services.AddScoped(); +builder.Services.AddScoped(); 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 7d632c4..9d067c6 100644 --- a/src/Nullinside.Api.TwitchBot/Services/MainService.cs +++ b/src/Nullinside.Api.TwitchBot/Services/MainService.cs @@ -79,6 +79,11 @@ public class MainService : BackgroundService { /// private readonly IServiceScopeFactory _serviceScopeFactory; + /// + /// The twitch api. + /// + private readonly ITwitchApiProxy _api; + /// /// Initializes a new instance of the class. /// @@ -87,7 +92,8 @@ public MainService(IServiceScopeFactory serviceScopeFactory) { _serviceScopeFactory = serviceScopeFactory; _scope = _serviceScopeFactory.CreateScope(); _db = _scope.ServiceProvider.GetRequiredService(); - _chatMessageConsumer = new TwitchChatMessageMonitorConsumer(_db, _receivedMessageProcessingQueue); + _api = _scope.ServiceProvider.GetRequiredService(); + _chatMessageConsumer = new TwitchChatMessageMonitorConsumer(_db, _api, _receivedMessageProcessingQueue); } /// @@ -98,13 +104,13 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) { return Task.Run(async () => { while (!stoppingToken.IsCancellationRequested) { try { - TwitchApiProxy? botApi = await _db.GetBotApiAndRefreshToken(stoppingToken); - if (null == botApi || !await botApi.IsValid(stoppingToken)) { + ITwitchApiProxy? botApi = await _db.ConfigureBotApiAndRefreshToken(_api, stoppingToken); + if (null == botApi || !await botApi.GetAccessTokenIsValid(stoppingToken)) { throw new Exception("Unable to log in as bot user"); } TwitchClientProxy.Instance.TwitchUsername = Constants.BotUsername; - TwitchClientProxy.Instance.TwitchOAuthToken = botApi.AccessToken; + TwitchClientProxy.Instance.TwitchOAuthToken = botApi.OAuth?.AccessToken; _botRules = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) @@ -148,16 +154,16 @@ private async Task Main(CancellationToken stoppingToken) { throw new Exception("No bot user in database"); } - TwitchApiProxy? botApi = await db.GetApiAndRefreshToken(botUser, stoppingToken); + ITwitchApiProxy? botApi = await db.ConfigureApiAndRefreshToken(botUser, this._api, stoppingToken); if (null != botApi) { // Trim channels that aren't live - IEnumerable liveUsers = await botApi.GetLiveChannels(usersWithBotEnabled + IEnumerable liveUsers = await botApi.GetChannelsLive(usersWithBotEnabled .Where(u => null != u.TwitchId) .Select(u => u.TwitchId)!); usersWithBotEnabled = usersWithBotEnabled.Where(u => liveUsers.Contains(u.TwitchId)).ToList(); // Trim channels we aren't a mod in - IEnumerable moddedChannels = await botApi.GetChannelsWeMod(Constants.BotId); + IEnumerable moddedChannels = await botApi.GetUserModChannels(Constants.BotId); usersWithBotEnabled = usersWithBotEnabled .Where(u => moddedChannels .Select(m => m.broadcaster_id) @@ -284,7 +290,7 @@ private async Task DoScan(User user, User botUser, CancellationToken stoppingTok using (IServiceScope scope = _serviceScopeFactory.CreateAsyncScope()) { await using (var db = scope.ServiceProvider.GetRequiredService()) { // Get the API - TwitchApiProxy? botApi = await db.GetApiAndRefreshToken(botUser, stoppingToken); + ITwitchApiProxy? botApi = await db.ConfigureApiAndRefreshToken(botUser, this._api, stoppingToken); if (null == _botRules || null == user.TwitchConfig || null == botApi) { return; } diff --git a/src/nullinside-api b/src/nullinside-api index 7ce25f8..b53da06 160000 --- a/src/nullinside-api +++ b/src/nullinside-api @@ -1 +1 @@ -Subproject commit 7ce25f8099a8d04769f0bdfe49555dc5b0692bdd +Subproject commit b53da06991eb23fc4d46cf228b2542a5d23d470d