From b1a0091e71c0f9f1c2b58116ce03e4cb5b5a0cce 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: Thu, 12 Jun 2025 10:56:16 -0400 Subject: [PATCH 1/2] chore: format and clean up closes #98 closes #99 --- .../.editorconfig | 20 ++++++------ .../ChatRules/BestCheapViewersTests.cs | 2 +- .../Nullinside.Api.TwitchBot.Tests.csproj | 10 +++--- src/Nullinside.Api.TwitchBot/.editorconfig | 15 +++++---- .../Bots/BanKnownBots.cs | 3 +- .../ChatRules/AChatRule.cs | 2 -- .../ChatRules/BestCheapViewers.cs | 14 ++++---- .../ChatRules/Botsister.cs | 2 -- .../ChatRules/Discord.cs | 6 ++-- .../ChatRules/Dogehype.cs | 2 -- .../ChatRules/HostHub.cs | 2 -- .../ChatRules/IChatRule.cs | 2 -- .../ChatRules/IfYouWantViewers.cs | 12 +++---- .../ChatRules/Naked.cs | 2 -- .../ChatRules/Nezhna.cs | 4 +-- .../ChatRules/StreamRise.cs | 2 -- .../ChatRules/StreamViewers.cs | 4 +-- .../ChatRules/Streamboo.cs | 2 -- .../TwitchChatMessageMonitorConsumer.cs | 32 +++++++++---------- .../Controllers/BotController.cs | 8 ++--- .../Controllers/LoginController.cs | 8 ++--- .../Nullinside.Api.TwitchBot.csproj | 12 +++---- src/Nullinside.Api.TwitchBot/Program.cs | 2 +- src/nullinside-api | 2 +- 24 files changed, 72 insertions(+), 98 deletions(-) diff --git a/src/Nullinside.Api.TwitchBot.Tests/.editorconfig b/src/Nullinside.Api.TwitchBot.Tests/.editorconfig index e4fa89d..c50c605 100644 --- a/src/Nullinside.Api.TwitchBot.Tests/.editorconfig +++ b/src/Nullinside.Api.TwitchBot.Tests/.editorconfig @@ -25,15 +25,13 @@ end_of_line = lf insert_final_newline = false #### .NET Coding Conventions #### -[*.{cs,vb}] # Organize usings dotnet_separate_import_directive_groups = true dotnet_sort_system_directives_first = true -file_header_template = # this. and Me. preferences - - +file_header_template = unset +# this. and Me. preferences dotnet_style_qualification_for_event = false:silent dotnet_style_qualification_for_field = false:silent dotnet_style_qualification_for_method = false:silent @@ -79,7 +77,6 @@ dotnet_code_quality_unused_parameters = all:suggestion dotnet_remove_unnecessary_suppression_exclusions = none #### C# Coding Conventions #### -[*.cs] # var preferences csharp_style_var_elsewhere = false:silent @@ -176,7 +173,6 @@ csharp_preserve_single_line_blocks = true csharp_preserve_single_line_statements = true #### Naming styles #### -[*.{cs,vb}] # Naming rules @@ -230,19 +226,19 @@ dotnet_naming_rule.private_static_fields_should_be_s_camelcase.style = s_camelca dotnet_naming_rule.public_constant_fields_should_be_pascalcase.severity = suggestion dotnet_naming_rule.public_constant_fields_should_be_pascalcase.symbols = public_constant_fields -dotnet_naming_rule.public_constant_fields_should_be_pascalcase.style = pascalcase +dotnet_naming_rule.public_constant_fields_should_be_pascalcase.style = all_upper dotnet_naming_rule.private_constant_fields_should_be_pascalcase.severity = suggestion dotnet_naming_rule.private_constant_fields_should_be_pascalcase.symbols = private_constant_fields -dotnet_naming_rule.private_constant_fields_should_be_pascalcase.style = pascalcase +dotnet_naming_rule.private_constant_fields_should_be_pascalcase.style = all_upper dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.severity = suggestion dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.symbols = public_static_readonly_fields -dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.style = pascalcase +dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.style = all_upper dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.severity = suggestion dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.symbols = private_static_readonly_fields -dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.style = pascalcase +dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.style = all_upper dotnet_naming_rule.enums_should_be_pascalcase.severity = suggestion dotnet_naming_rule.enums_should_be_pascalcase.symbols = enums @@ -366,3 +362,7 @@ dotnet_naming_style.s_camelcase.required_suffix = dotnet_naming_style.s_camelcase.word_separator = dotnet_naming_style.s_camelcase.capitalization = camel_case +dotnet_naming_style.all_upper.required_prefix = +dotnet_naming_style.all_upper.required_suffix = +dotnet_naming_style.all_upper.word_separator = _ +dotnet_naming_style.all_upper.capitalization = all_upper diff --git a/src/Nullinside.Api.TwitchBot.Tests/ChatRules/BestCheapViewersTests.cs b/src/Nullinside.Api.TwitchBot.Tests/ChatRules/BestCheapViewersTests.cs index 31af63b..2b3fa89 100644 --- a/src/Nullinside.Api.TwitchBot.Tests/ChatRules/BestCheapViewersTests.cs +++ b/src/Nullinside.Api.TwitchBot.Tests/ChatRules/BestCheapViewersTests.cs @@ -41,7 +41,7 @@ public class BestCheapViewersTests : AChatRuleUnitTestBase { [TestCase("Cheaͧp v̫iewers on *** ")] [TestCase("Cheap viewers ͚on followersboosters.ru @U8y0GrKS")] [TestCase("Chea̓p viewers on streamboo. com (remove the space) @ND80DOGe")] - [TestCase("Bestͮ vie͐wers ̷on cutt.ly/EeK6Anda")] + [TestCase("Bestͮ vie͐wers ̷on cutt.ly/EeK6Anda")] [TestCase("Bestͮ vie͐wers ̷on viewerszone.online")] [TestCase("Best viěw\u0310e\u0329rs \u0333o\u032bno\u034en streamboo .com ( remove the space ) @WSZ7tPNI")] public async Task TestKnownStrings(string badString) { diff --git a/src/Nullinside.Api.TwitchBot.Tests/Nullinside.Api.TwitchBot.Tests.csproj b/src/Nullinside.Api.TwitchBot.Tests/Nullinside.Api.TwitchBot.Tests.csproj index 688523e..36662ce 100644 --- a/src/Nullinside.Api.TwitchBot.Tests/Nullinside.Api.TwitchBot.Tests.csproj +++ b/src/Nullinside.Api.TwitchBot.Tests/Nullinside.Api.TwitchBot.Tests.csproj @@ -15,16 +15,16 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/src/Nullinside.Api.TwitchBot/.editorconfig b/src/Nullinside.Api.TwitchBot/.editorconfig index 6850b93..c50c605 100644 --- a/src/Nullinside.Api.TwitchBot/.editorconfig +++ b/src/Nullinside.Api.TwitchBot/.editorconfig @@ -25,7 +25,6 @@ end_of_line = lf insert_final_newline = false #### .NET Coding Conventions #### -[*.{cs,vb}] # Organize usings dotnet_separate_import_directive_groups = true @@ -78,7 +77,6 @@ dotnet_code_quality_unused_parameters = all:suggestion dotnet_remove_unnecessary_suppression_exclusions = none #### C# Coding Conventions #### -[*.cs] # var preferences csharp_style_var_elsewhere = false:silent @@ -175,7 +173,6 @@ csharp_preserve_single_line_blocks = true csharp_preserve_single_line_statements = true #### Naming styles #### -[*.{cs,vb}] # Naming rules @@ -229,19 +226,19 @@ dotnet_naming_rule.private_static_fields_should_be_s_camelcase.style = s_camelca dotnet_naming_rule.public_constant_fields_should_be_pascalcase.severity = suggestion dotnet_naming_rule.public_constant_fields_should_be_pascalcase.symbols = public_constant_fields -dotnet_naming_rule.public_constant_fields_should_be_pascalcase.style = pascalcase +dotnet_naming_rule.public_constant_fields_should_be_pascalcase.style = all_upper dotnet_naming_rule.private_constant_fields_should_be_pascalcase.severity = suggestion dotnet_naming_rule.private_constant_fields_should_be_pascalcase.symbols = private_constant_fields -dotnet_naming_rule.private_constant_fields_should_be_pascalcase.style = pascalcase +dotnet_naming_rule.private_constant_fields_should_be_pascalcase.style = all_upper dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.severity = suggestion dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.symbols = public_static_readonly_fields -dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.style = pascalcase +dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.style = all_upper dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.severity = suggestion dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.symbols = private_static_readonly_fields -dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.style = pascalcase +dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.style = all_upper dotnet_naming_rule.enums_should_be_pascalcase.severity = suggestion dotnet_naming_rule.enums_should_be_pascalcase.symbols = enums @@ -365,3 +362,7 @@ dotnet_naming_style.s_camelcase.required_suffix = dotnet_naming_style.s_camelcase.word_separator = dotnet_naming_style.s_camelcase.capitalization = camel_case +dotnet_naming_style.all_upper.required_prefix = +dotnet_naming_style.all_upper.required_suffix = +dotnet_naming_style.all_upper.word_separator = _ +dotnet_naming_style.all_upper.capitalization = all_upper diff --git a/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs b/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs index 65035e8..2ec7701 100644 --- a/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs +++ b/src/Nullinside.Api.TwitchBot/Bots/BanKnownBots.cs @@ -195,4 +195,5 @@ // jsonString = null; // return allBots; // } -// } \ 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 e7186db..0e6aa9c 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs index a3c8c27..f1e2c42 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; @@ -13,15 +11,15 @@ namespace Nullinside.Api.TwitchBot.ChatRules; /// public class BestCheapViewers : AChatRule { /// - /// The strings that we expect to receive if this is a bot. + /// The strings that we expect to receive if this is a bot. /// public readonly string[] EXPECTED = [ - "best viewers on", + "best viewers on", "cheap viewers on", "cheap folloewrs on", "do you want more viewers and to rank higher on the twitch list? you can visit the website" ]; - + /// public override bool ShouldRun(TwitchUserConfig config) { return config is { Enabled: true, BanKnownBots: true }; @@ -33,7 +31,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro if (!message.IsFirstMessage) { return true; } - + // The number of spaces per message may chance, so normalize that and lowercase it for comparison. string normalized = string.Join(' ', message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s))) .ToLowerInvariant(); @@ -41,7 +39,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro // Messages will be one of two variations with random special characters mixed in. Some of those special characters // will be accent marks. When we receive an accent mark it'll take the position of a real character, hence why we // need an offset applied only to the incoming string. - foreach (var expected in EXPECTED) { + foreach (string expected in EXPECTED) { if (normalized.Length > expected.Length) { int matches = 0; int offset = 0; @@ -49,7 +47,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro // If this is a normal character it should be in the correct position. if (i + offset < normalized.Length && normalized[i + offset] == expected[i]) { ++matches; - } + } // If this is an accent mark then the next character should match and the whole string we're evalutating // will be off by 1 more position. else if (i + offset + 1 < normalized.Length && normalized[i + offset + 1] == expected[i]) { diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Botsister.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Botsister.cs index ff6cd31..4825059 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Botsister.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Botsister.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs index 1349d86..115166e 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; @@ -38,7 +36,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro if (!message.IsFirstMessage) { return true; } - + // The number of spaces per message may chance, so normalize that and lowercase it for comparison. string normalized = string.Join(' ', message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s))) .ToLowerInvariant(); @@ -47,7 +45,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro return true; } - foreach (var phrase in KNOWN_PHRASES) { + foreach (string phrase in KNOWN_PHRASES) { if (normalized.Contains(phrase, StringComparison.InvariantCultureIgnoreCase)) { await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) }, "[Bot] Spam (Discord Scammers)", db, stoppingToken); diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs index 782bad3..dfe9e66 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs b/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs index febb136..04d6b6e 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs b/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs index e639a70..f7038f3 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs index bc46a65..d10fb00 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs @@ -2,23 +2,21 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; /// -/// "if you want viewers, go to [link]" scam. +/// "if you want viewers, go to [link]" scam. /// public class IfYouWantViewers : AChatRule { /// - /// The strings that we expect to receive if this is a bot. + /// The strings that we expect to receive if this is a bot. /// public readonly string[] EXPECTED = [ "if you want more viewers for your stream, go to" ]; - + /// public override bool ShouldRun(TwitchUserConfig config) { return config is { Enabled: true, BanKnownBots: true }; @@ -30,12 +28,12 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro if (!message.IsFirstMessage) { return true; } - + // The number of spaces per message may chance, so normalize that and lowercase it for comparison. string normalized = string.Join(' ', message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s))) .ToLowerInvariant(); - foreach (var expected in EXPECTED) { + foreach (string expected in EXPECTED) { if (normalized.Contains(expected, StringComparison.InvariantCultureIgnoreCase)) { await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) }, "[Bot] Spam (If You Want Viewers)", db, stoppingToken); diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs index 321a683..da0dc5d 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Nezhna.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Nezhna.cs index d15cb42..ffb3a46 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Nezhna.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Nezhna.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; @@ -31,7 +29,7 @@ await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) "[Bot] Spam (Nezhna)", db, stoppingToken); return false; } - + return true; } } \ No newline at end of file diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs b/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs index ac96045..4acce0c 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs index 1919d92..9f7b217 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; @@ -28,7 +26,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro if (!message.IsFirstMessage) { return true; } - + List parts = message.Message .Split(" ") .Where(s => !string.IsNullOrWhiteSpace(s)) diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Streamboo.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Streamboo.cs index 9a5e13c..159c370 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Streamboo.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Streamboo.cs @@ -2,8 +2,6 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Model; -using TwitchLib.Client.Models; - using TwitchUserConfig = Nullinside.Api.Model.Ddl.TwitchUserConfig; namespace Nullinside.Api.TwitchBot.ChatRules; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs b/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs index e1cc618..2676dfd 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/TwitchChatMessageMonitorConsumer.cs @@ -4,8 +4,6 @@ using Microsoft.EntityFrameworkCore; -using Mysqlx.Expr; - using Nullinside.Api.Common.Twitch; using Nullinside.Api.Model; using Nullinside.Api.Model.Ddl; @@ -35,6 +33,11 @@ public class TwitchChatMessageMonitorConsumer : IDisposable { /// private static IChatRule[]? s_chatRules; + /// + /// The twitch api. + /// + private readonly ITwitchApiProxy _api; + /// /// The nullinside database. /// @@ -55,11 +58,6 @@ public class TwitchChatMessageMonitorConsumer : IDisposable { /// private bool _poisonPill; - /// - /// The twitch api. - /// - private readonly ITwitchApiProxy _api; - /// /// Initializes a new instance of the class. /// @@ -127,15 +125,15 @@ private async void MainLoop() { } // We need the user's configuration to check which rules to run. - User? user = _db.Users - .Include(u => u.TwitchConfig) - .FirstOrDefault(u => - !u.IsBanned && - null != u.TwitchConfig && - u.TwitchConfig.Enabled && - u.TwitchUsername == message.Channel - ); - + User? user = _db.Users + .Include(u => u.TwitchConfig) + .FirstOrDefault(u => + !u.IsBanned && + null != u.TwitchConfig && + u.TwitchConfig.Enabled && + u.TwitchUsername == message.Channel + ); + if (null == user?.TwitchConfig || null == user.TwitchId) { continue; } @@ -147,7 +145,7 @@ private async void MainLoop() { } // Get the bot proxy - ITwitchApiProxy? botProxy = await _db.ConfigureBotApiAndRefreshToken(this._api); + ITwitchApiProxy? botProxy = await _db.ConfigureBotApiAndRefreshToken(_api); if (null == botProxy) { continue; } diff --git a/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs b/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs index 71db3f7..8d986e2 100644 --- a/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs +++ b/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs @@ -67,7 +67,7 @@ public BotController(INullinsideContext dbContext, IConfiguration configuration) null == user.TwitchTokenExpiration || null == user.TwitchId) { return Unauthorized(); } - + api.Configure(user); IEnumerable mods = await api.GetChannelMods(user.TwitchId, token); return Ok(new { @@ -95,7 +95,7 @@ public async Task ModBotAccount([FromServices] ITwitchApiProxy ap null == user.TwitchTokenExpiration || null == user.TwitchId) { return Unauthorized(); } - + api.Configure(user); bool success = await api.AddChannelMod(user.TwitchId, Constants.BotId, token); return Ok(success); @@ -133,7 +133,7 @@ public async Task GetConfig(CancellationToken token) { BanKnownBots = config.BanKnownBots }); } - + /// /// Gets the timestamp of the last time a chat message was received. /// @@ -143,7 +143,7 @@ public async Task GetConfig(CancellationToken token) { [HttpGet] [Route("chat/timestamp")] public async Task GetLastChatTimestamp(CancellationToken token) { - var message = + TwitchUserChatLogs? message = await _dbContext.TwitchUserChatLogs.OrderByDescending(c => c.Timestamp).FirstOrDefaultAsync(token); if (null == message) { return StatusCode(500); diff --git a/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs b/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs index e2b39ff..080a6c0 100644 --- a/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs +++ b/src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs @@ -60,23 +60,23 @@ public LoginController(INullinsideContext dbContext, IConfiguration configuratio public async Task TwitchLogin([FromQuery] string code, [FromServices] ITwitchApiProxy api, CancellationToken token) { string? siteUrl = _configuration.GetValue("Api:SiteUrl"); if (null == await api.CreateAccessToken(code, token)) { - return Redirect($"{siteUrl}/twitch-bot/config?error={TwitchBotLoginErrors.TwitchErrorWithToken}"); + return Redirect($"{siteUrl}/twitch-bot/config?error={TwitchBotLoginErrors.TWITCH_ERROR_WITH_TOKEN}"); } string? email = await api.GetUserEmail(token); if (string.IsNullOrWhiteSpace(email)) { - return Redirect($"{siteUrl}/twitch-bot/config?error={TwitchBotLoginErrors.TwitchAccountHasNoEmail}"); + return Redirect($"{siteUrl}/twitch-bot/config?error={TwitchBotLoginErrors.TWITCH_ACCOUNT_HAS_NO_EMAIL}"); } (string? id, string? username) user = await api.GetUser(token); if (string.IsNullOrWhiteSpace(user.username) || string.IsNullOrWhiteSpace(user.id)) { - return Redirect($"{siteUrl}/twitch-bot/config?error={TwitchBotLoginErrors.InternalError}"); + return Redirect($"{siteUrl}/twitch-bot/config?error={TwitchBotLoginErrors.INTERNAL_ERROR}"); } string? bearerToken = await UserHelpers.GenerateTokenAndSaveToDatabase(_dbContext, email, token, api.OAuth?.AccessToken, api.OAuth?.RefreshToken, api.OAuth?.ExpiresUtc, user.username, user.id); if (string.IsNullOrWhiteSpace(bearerToken)) { - return Redirect($"{siteUrl}/twitch-bot/config?error={TwitchBotLoginErrors.InternalError}"); + return Redirect($"{siteUrl}/twitch-bot/config?error={TwitchBotLoginErrors.INTERNAL_ERROR}"); } return Redirect($"{siteUrl}/twitch-bot/config?token={bearerToken}"); diff --git a/src/Nullinside.Api.TwitchBot/Nullinside.Api.TwitchBot.csproj b/src/Nullinside.Api.TwitchBot/Nullinside.Api.TwitchBot.csproj index 6c12652..b77d215 100644 --- a/src/Nullinside.Api.TwitchBot/Nullinside.Api.TwitchBot.csproj +++ b/src/Nullinside.Api.TwitchBot/Nullinside.Api.TwitchBot.csproj @@ -22,17 +22,17 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + diff --git a/src/Nullinside.Api.TwitchBot/Program.cs b/src/Nullinside.Api.TwitchBot/Program.cs index 997df75..419cd61 100644 --- a/src/Nullinside.Api.TwitchBot/Program.cs +++ b/src/Nullinside.Api.TwitchBot/Program.cs @@ -47,7 +47,7 @@ } options.FallbackPolicy = new AuthorizationPolicyBuilder() - .RequireRole(nameof(UserRoles.User)) + .RequireRole(nameof(UserRoles.USER)) .RequireAuthenticatedUser() .Build(); }); diff --git a/src/nullinside-api b/src/nullinside-api index b4247a8..b5e34a7 160000 --- a/src/nullinside-api +++ b/src/nullinside-api @@ -1 +1 @@ -Subproject commit b4247a81c19c97be191dc2f19e677a042f54e8c1 +Subproject commit b5e34a7cc2eb2407beb16fb5099db36408bc3936 From e2206faccc28d5f25c8f4f0e05765e27a03ec315 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: Thu, 12 Jun 2025 10:58:51 -0400 Subject: [PATCH 2/2] chore: variable renames --- src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs | 2 +- .../ChatRules/AChatRule.cs | 2 +- .../ChatRules/BestCheapViewers.cs | 4 ++-- .../ChatRules/Discord.cs | 4 ++-- .../ChatRules/IfYouWantViewers.cs | 4 ++-- .../ChatRules/Naked.cs | 8 +++---- .../ChatRules/StreamRise.cs | 4 ++-- .../ChatRules/StreamViewers.cs | 12 +++++----- src/Nullinside.Api.TwitchBot/Constants.cs | 12 +++++----- .../Controllers/BotController.cs | 4 ++-- .../Model/NullinsideContextExtensions.cs | 6 ++--- src/Nullinside.Api.TwitchBot/Program.cs | 6 ++--- .../Services/MainService.cs | 24 +++++++++---------- 13 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs b/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs index e9a6dc5..88f9314 100644 --- a/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs +++ b/src/Nullinside.Api.TwitchBot/Bots/ABotRule.cs @@ -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.BanChannelUsers(channelId, Constants.BotId, bansToTry, reason, stoppingToken); + await botProxy.BanChannelUsers(channelId, Constants.BOT_ID, bansToTry, reason, stoppingToken); await db.SaveTwitchBans(channelId, users, reason, stoppingToken); return confirmedBans; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs b/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs index 0e6aa9c..fcb080d 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs @@ -29,7 +29,7 @@ public abstract Task Handle(string channelId, ITwitchApiProxy botProxy, Tw public async Task BanAndLog(string channelId, ITwitchApiProxy botProxy, IEnumerable<(string Id, string Username)> users, string reason, INullinsideContext db, CancellationToken stoppingToken = new()) { - await botProxy.BanChannelUsers(channelId, Constants.BotId, users, reason, stoppingToken); + await botProxy.BanChannelUsers(channelId, Constants.BOT_ID, 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 f1e2c42..934c555 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/BestCheapViewers.cs @@ -13,7 +13,7 @@ public class BestCheapViewers : AChatRule { /// /// The strings that we expect to receive if this is a bot. /// - public readonly string[] EXPECTED = [ + public readonly string[] Expected = [ "best viewers on", "cheap viewers on", "cheap folloewrs on", @@ -39,7 +39,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro // Messages will be one of two variations with random special characters mixed in. Some of those special characters // will be accent marks. When we receive an accent mark it'll take the position of a real character, hence why we // need an offset applied only to the incoming string. - foreach (string expected in EXPECTED) { + foreach (string expected in Expected) { if (normalized.Length > expected.Length) { int matches = 0; int offset = 0; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs index 115166e..9e89876 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs @@ -10,7 +10,7 @@ namespace Nullinside.Api.TwitchBot.ChatRules; /// Handles the add me on discord bots. /// public class Discord : AChatRule { - private readonly string[] KNOWN_PHRASES = [ + private readonly string[] _knownPhrases = [ "add me", "add my", "add up", @@ -45,7 +45,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro return true; } - foreach (string phrase in KNOWN_PHRASES) { + foreach (string phrase in _knownPhrases) { if (normalized.Contains(phrase, StringComparison.InvariantCultureIgnoreCase)) { await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) }, "[Bot] Spam (Discord Scammers)", db, stoppingToken); diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs index d10fb00..396d81e 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/IfYouWantViewers.cs @@ -13,7 +13,7 @@ public class IfYouWantViewers : AChatRule { /// /// The strings that we expect to receive if this is a bot. /// - public readonly string[] EXPECTED = [ + public readonly string[] Expected = [ "if you want more viewers for your stream, go to" ]; @@ -33,7 +33,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro string normalized = string.Join(' ', message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s))) .ToLowerInvariant(); - foreach (string expected in EXPECTED) { + foreach (string expected in Expected) { if (normalized.Contains(expected, StringComparison.InvariantCultureIgnoreCase)) { await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) }, "[Bot] Spam (If You Want Viewers)", db, stoppingToken); diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs b/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs index da0dc5d..e33d357 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs @@ -10,8 +10,8 @@ namespace Nullinside.Api.TwitchBot.ChatRules; /// Handles the want to see her naked porn bots. /// public class Naked : AChatRule { - private const string _spam = "Want to see her naked?"; - private const string _spam2 = "Want to see me naked?"; + private const string SPAM = "Want to see her naked?"; + private const string SPAM2 = "Want to see me naked?"; /// public override bool ShouldRun(TwitchUserConfig config) { @@ -22,8 +22,8 @@ public override bool ShouldRun(TwitchUserConfig config) { public override async Task Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { if (message.IsFirstMessage && - (message.Message.TrimStart().StartsWith(_spam, StringComparison.InvariantCultureIgnoreCase) || - message.Message.TrimStart().StartsWith(_spam2, StringComparison.InvariantCultureIgnoreCase))) { + (message.Message.TrimStart().StartsWith(SPAM, StringComparison.InvariantCultureIgnoreCase) || + message.Message.TrimStart().StartsWith(SPAM2, StringComparison.InvariantCultureIgnoreCase))) { await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) }, "[Bot] Spam (Naked)", db, stoppingToken); return false; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs b/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs index 4acce0c..58b2119 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs @@ -10,7 +10,7 @@ namespace Nullinside.Api.TwitchBot.ChatRules; /// Handles "streamrise" spam. /// public class StreamRise : AChatRule { - private const string _spam = "Hello, sorry for bothering you. I want to offer promotion of your channel, " + + private const string SPAM = "Hello, sorry for bothering you. I want to offer promotion of your channel, " + "viewers, followers, views, chat bots, etc...The price is lower than any competitor, " + "the quality is guaranteed to be the best. Flexible and convenient order management " + "panel, chat panel, everything is in your hands, a huge number of custom settings. Go " + @@ -24,7 +24,7 @@ public override bool ShouldRun(TwitchUserConfig config) { /// public override async Task Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message, INullinsideContext db, CancellationToken stoppingToken = new()) { - if (message.IsFirstMessage && _spam.Equals(message.Message, StringComparison.InvariantCultureIgnoreCase)) { + if (message.IsFirstMessage && SPAM.Equals(message.Message, StringComparison.InvariantCultureIgnoreCase)) { await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) }, "[Bot] Spam (StreamRise)", db, stoppingToken); return false; diff --git a/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs b/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs index 9f7b217..c4bd2a6 100644 --- a/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs +++ b/src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs @@ -10,9 +10,9 @@ namespace Nullinside.Api.TwitchBot.ChatRules; /// Handles "streamviewers org" spam. /// public class StreamViewers : AChatRule { - private const int MinThreshold = 70; + private const int MIN_THRESHOLD = 70; - private const string ExpectedSpamMessage = + private const string EXPECTED_SPAM_MESSAGE = "doyoualreadytriedstreamviewersorg?realviewers,fireworks!theyarenowgivingoutafreepackageforstreamersoo"; /// @@ -47,13 +47,13 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro // With no spaces the message will be exactly the length of our spam message. string noSpaces = string.Concat(parts); - if (noSpaces.Length != ExpectedSpamMessage.Length) { + if (noSpaces.Length != EXPECTED_SPAM_MESSAGE.Length) { return true; } // Determine how similar the message is to the spam message. int matches = 0; - for (int i = 0; i < ExpectedSpamMessage.Length; i++) { + for (int i = 0; i < EXPECTED_SPAM_MESSAGE.Length; i++) { // If it's not an ascii character it might mean they're trying to obfuscate by swapping out letters that look // like ascii characters. Like an e with an accent mark instead of an ascii e. They'll look almost the same to // the reader but the character will be different. We can skip these and assume it doesn't count as a match. @@ -65,7 +65,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro // strings must match. There shouldn't be any letters in order that we aren't expecting provided they're actually // ascii characters. If, for some reason, there is a letter in this position that we aren't expecting it means it // is not our spam message. - if (noSpaces[i] != ExpectedSpamMessage[i]) { + if (noSpaces[i] != EXPECTED_SPAM_MESSAGE[i]) { return true; } @@ -74,7 +74,7 @@ public override async Task Handle(string channelId, ITwitchApiProxy botPro } // If we had less character matches than our threshold then this wasn't a spam message. - if (matches < MinThreshold) { + if (matches < MIN_THRESHOLD) { return true; } diff --git a/src/Nullinside.Api.TwitchBot/Constants.cs b/src/Nullinside.Api.TwitchBot/Constants.cs index f7a3aaa..1fc7d36 100644 --- a/src/Nullinside.Api.TwitchBot/Constants.cs +++ b/src/Nullinside.Api.TwitchBot/Constants.cs @@ -7,23 +7,23 @@ public static class Constants { /// /// The email address associated with the twitch account for the bot. /// - public const string BotEmail = "dev.nullinside@gmail.com"; + public const string BOT_EMAIL = "dev.nullinside@gmail.com"; /// /// The twitch username for the bot account. /// - public const string BotUsername = "nullinside"; + public const string BOT_USERNAME = "nullinside"; /// /// The twitch id for the bot account. /// - public const string BotId = "640082552"; + public const string BOT_ID = "640082552"; // TODO: This should be dynamic but I need to find a source of "good bots" lists. Might have to cheap out and just do a database table with data entry. Let users of the bot submit suggestions that we approve manually. /// /// The whitelist of bots to not ban. /// - public static readonly string[] WhitelistedBots = { + public static readonly string[] WHITELISTED_BOTS = { "soundalerts", "nightbot", "streamlabs", "pokemoncommunitygame", "streamelements", "moobot", "wizebot", "bad_elbereth", "dixperbro", @@ -37,10 +37,10 @@ public static class Constants { /// /// The minimum time that must elapse between user's getting scanned. /// - public static readonly TimeSpan MinimumTimeBetweenScans = TimeSpan.FromSeconds(30); + public static readonly TimeSpan MINIMUM_TIME_BETWEEN_SCANS = TimeSpan.FromSeconds(30); /// /// The minimum time that must elapse between user's getting scanned if they are currently live. /// - public static readonly TimeSpan MinimumTimeBetweenScansLive = TimeSpan.FromSeconds(1); + public static readonly TimeSpan MINIMUM_TIME_BETWEEN_SCANS_LIVE = TimeSpan.FromSeconds(1); } \ No newline at end of file diff --git a/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs b/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs index 8d986e2..6383755 100644 --- a/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs +++ b/src/Nullinside.Api.TwitchBot/Controllers/BotController.cs @@ -72,7 +72,7 @@ public BotController(INullinsideContext dbContext, IConfiguration configuration) IEnumerable mods = await api.GetChannelMods(user.TwitchId, token); return Ok(new { isMod = null != mods.FirstOrDefault(m => - string.Equals(m.UserId, Constants.BotId, StringComparison.InvariantCultureIgnoreCase)) + string.Equals(m.UserId, Constants.BOT_ID, StringComparison.InvariantCultureIgnoreCase)) }); } @@ -97,7 +97,7 @@ public async Task ModBotAccount([FromServices] ITwitchApiProxy ap } api.Configure(user); - bool success = await api.AddChannelMod(user.TwitchId, Constants.BotId, token); + bool success = await api.AddChannelMod(user.TwitchId, Constants.BOT_ID, token); return Ok(success); } diff --git a/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs b/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs index 3a628f2..f516b36 100644 --- a/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs +++ b/src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs @@ -24,7 +24,7 @@ public static class NullinsideContextExtensions { /// /// The logger. /// - private static readonly ILog _log = LogManager.GetLogger(typeof(NullinsideContextExtensions)); + private static readonly ILog LOG = LogManager.GetLogger(typeof(NullinsideContextExtensions)); /// /// Gets a twitch api proxy. @@ -70,7 +70,7 @@ public static void Configure(this ITwitchApiProxy api, User user) { var sw = new Stopwatch(); sw.Start(); await dbLock.GetLock(BOT_REFRESH_TOKEN_LOCK_NAME, stoppingToken); - _log.Info($"bot_refresh_token: {sw.Elapsed}"); + LOG.Info($"bot_refresh_token: {sw.Elapsed}"); sw.Stop(); try { @@ -143,7 +143,7 @@ private static async Task UpdateOAuthInDatabase(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); + .FirstOrDefaultAsync(u => u.TwitchId == Constants.BOT_ID, stoppingToken); if (null == botUser) { throw new Exception("No bot user in database"); } diff --git a/src/Nullinside.Api.TwitchBot/Program.cs b/src/Nullinside.Api.TwitchBot/Program.cs index 419cd61..e836e57 100644 --- a/src/Nullinside.Api.TwitchBot/Program.cs +++ b/src/Nullinside.Api.TwitchBot/Program.cs @@ -11,7 +11,7 @@ using Nullinside.Api.Model; using Nullinside.Api.TwitchBot.Services; -const string CORS_KEY = "_customAllowedSpecificOrigins"; +const string corsKey = "_customAllowedSpecificOrigins"; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Logging.ClearProviders(); @@ -88,7 +88,7 @@ // Add services to the container. builder.Services.AddCors(options => { - options.AddPolicy(CORS_KEY, + options.AddPolicy(corsKey, policyBuilder => { policyBuilder.WithOrigins("https://www.nullinside.com", "https://nullinside.com", "http://localhost:4200", "http://127.0.0.1:4200") @@ -114,7 +114,7 @@ } app.UseHttpsRedirection(); -app.UseCors(CORS_KEY); +app.UseCors(corsKey); app.UseAuthorization(); app.MapControllers(); diff --git a/src/Nullinside.Api.TwitchBot/Services/MainService.cs b/src/Nullinside.Api.TwitchBot/Services/MainService.cs index 5859b05..c5eecc6 100644 --- a/src/Nullinside.Api.TwitchBot/Services/MainService.cs +++ b/src/Nullinside.Api.TwitchBot/Services/MainService.cs @@ -24,12 +24,12 @@ public class MainService : BackgroundService { /// /// The amount of time to wait between each scan of the live users, in milliseconds. /// - private const int ScanLoopDelayMilliseconds = 10000; + private const int SCAN_LOOP_DELAY_MILLISECONDS = 10000; /// /// The bot rules to scan with. /// - private static IBotRule[]? _botRules; + private static IBotRule[]? s_botRules; /// /// The twitch api. @@ -115,10 +115,10 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) { throw new Exception("Unable to log in as bot user"); } - _client.TwitchUsername = Constants.BotUsername; + _client.TwitchUsername = Constants.BOT_USERNAME; _client.TwitchOAuthToken = botApi.OAuth?.AccessToken; - _botRules = AppDomain.CurrentDomain.GetAssemblies() + s_botRules = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(t => typeof(IBotRule).IsAssignableFrom(t) && t is { IsAbstract: false, IsInterface: false }) .Select(t => Activator.CreateInstance(t) as IBotRule) @@ -155,7 +155,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) { // Get the bot user's information. User? botUser = await db.Users.AsNoTracking() - .FirstOrDefaultAsync(u => u.TwitchId == Constants.BotId, stoppingToken); + .FirstOrDefaultAsync(u => u.TwitchId == Constants.BOT_ID, stoppingToken); if (null == botUser) { throw new Exception("No bot user in database"); } @@ -172,7 +172,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) { usersWithBotEnabled = usersWithBotEnabled.Where(u => liveUsers.Contains(u.TwitchId)).ToList(); // Trim channels we aren't a mod in - IEnumerable moddedChannels = await botApi.GetUserModChannels(Constants.BotId); + IEnumerable moddedChannels = await botApi.GetUserModChannels(Constants.BOT_ID); usersWithBotEnabled = usersWithBotEnabled .Where(u => moddedChannels.Select(m => m.broadcaster_id).Contains(u.TwitchId)) .ToList(); @@ -203,7 +203,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) { } // Wait between scans. We don't want to spam the API too much. - await Task.Delay(ScanLoopDelayMilliseconds, stoppingToken); + await Task.Delay(SCAN_LOOP_DELAY_MILLISECONDS, stoppingToken); } } catch (Exception ex) { @@ -275,7 +275,7 @@ private void OnTwitchMessageReceived(OnMessageReceivedArgs e) { return await (from user in db.Users orderby user.TwitchLastScanned - where user.TwitchId != Constants.BotId && + where user.TwitchId != Constants.BOT_ID && !user.IsBanned select user) .Include(u => u.TwitchConfig) @@ -294,7 +294,7 @@ orderby user.TwitchLastScanned return await (from user in db.Users orderby user.TwitchLastScanned - where user.TwitchId != Constants.BotId && + where user.TwitchId != Constants.BOT_ID && user.IsBanned select user) .AsNoTracking() @@ -309,7 +309,7 @@ orderby user.TwitchLastScanned /// The stopping token. private async Task DoScan(User user, User botUser, CancellationToken stoppingToken) { // Determine if it's too early for a scan. - if (DateTime.UtcNow < user.TwitchLastScanned + Constants.MinimumTimeBetweenScansLive) { + if (DateTime.UtcNow < user.TwitchLastScanned + Constants.MINIMUM_TIME_BETWEEN_SCANS_LIVE) { return; } @@ -319,12 +319,12 @@ private async Task DoScan(User user, User botUser, CancellationToken stoppingTok await using (var db = scope.ServiceProvider.GetRequiredService()) { // Get the API _api.Configure(botUser); - if (null == _botRules || null == user.TwitchConfig) { + if (null == s_botRules || null == user.TwitchConfig) { return; } // Run the rules that scan the chats and the accounts. - foreach (IBotRule rule in _botRules) { + foreach (IBotRule rule in s_botRules) { try { if (rule.ShouldRun(user.TwitchConfig)) { await rule.Handle(user, user.TwitchConfig, _api, db, stoppingToken);