From e6e335b5dc5b4e15743ccdff643450cea80bb22d Mon Sep 17 00:00:00 2001 From: Noi Date: Sun, 28 Apr 2024 18:15:12 -0700 Subject: [PATCH] Implement new suggestions from framework upgrade --- ApplicationCommands/BotModuleBase.cs | 12 +++++++----- ApplicationCommands/ExportModule.cs | 4 ++-- BackgroundServices/AutoUserDownload.cs | 2 +- BackgroundServices/BirthdayRoleUpdate.cs | 4 +--- Configuration.cs | 7 ++++--- ShardManager.cs | 2 +- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/ApplicationCommands/BotModuleBase.cs b/ApplicationCommands/BotModuleBase.cs index 2a6ddd1..7660576 100644 --- a/ApplicationCommands/BotModuleBase.cs +++ b/ApplicationCommands/BotModuleBase.cs @@ -10,7 +10,7 @@ namespace BirthdayBot.ApplicationCommands; /// /// Base class for our interaction module classes. Contains common data for use in implementing classes. /// -public abstract class BotModuleBase : InteractionModuleBase { +public abstract partial class BotModuleBase : InteractionModuleBase { protected const string MemberCacheEmptyError = ":warning: Please try the command again."; public const string AccessDeniedError = ":warning: You are not allowed to run this command."; @@ -65,8 +65,10 @@ public abstract class BotModuleBase : InteractionModuleBase\d{1,2})[ -](?[A-Za-z]+)$", RegexOptions.Compiled); - private static readonly Regex DateParse2 = new(@"^(?[A-Za-z]+)[ -](?\d{1,2})$", RegexOptions.Compiled); + [GeneratedRegex(@"^(?\d{1,2})[ -](?[A-Za-z]+)$")] + private static partial Regex DateParser1(); + [GeneratedRegex(@"^(?[A-Za-z]+)[ -](?\d{1,2})$")] + private static partial Regex DateParser2(); /// /// Parses a date input. @@ -76,10 +78,10 @@ public abstract class BotModuleBase : InteractionModuleBase protected static (int, int) ParseDate(string dateInput) { - var m = DateParse1.Match(dateInput); + var m = DateParser1().Match(dateInput); if (!m.Success) { // Flip the fields around, try again - m = DateParse2.Match(dateInput); + m = DateParser2().Match(dateInput); if (!m.Success) throw new FormatException(FormatError); } diff --git a/ApplicationCommands/ExportModule.cs b/ApplicationCommands/ExportModule.cs index 815ac4c..d2eefe1 100644 --- a/ApplicationCommands/ExportModule.cs +++ b/ApplicationCommands/ExportModule.cs @@ -28,7 +28,7 @@ public class ExportModule : BotModuleBase { await RespondWithFileAsync(fileoutput, filename, text: $"Exported {bdlist.Count} birthdays to file."); } - private static Stream ListExportNormal(SocketGuild guild, IEnumerable list) { + private static MemoryStream ListExportNormal(SocketGuild guild, IEnumerable list) { // Output: "● Mon-dd: (user ID) Username [ - Nickname: (nickname)]" var result = new MemoryStream(); var writer = new StreamWriter(result, Encoding.UTF8); @@ -52,7 +52,7 @@ public class ExportModule : BotModuleBase { return result; } - private static Stream ListExportCsv(SocketGuild guild, IEnumerable list) { + private static MemoryStream ListExportCsv(SocketGuild guild, IEnumerable list) { // Output: User ID, Username, Nickname, Month-Day, Month, Day var result = new MemoryStream(); var writer = new StreamWriter(result, Encoding.UTF8); diff --git a/BackgroundServices/AutoUserDownload.cs b/BackgroundServices/AutoUserDownload.cs index 67dd10b..cae178c 100644 --- a/BackgroundServices/AutoUserDownload.cs +++ b/BackgroundServices/AutoUserDownload.cs @@ -8,7 +8,7 @@ namespace BirthdayBot.BackgroundServices; class AutoUserDownload : BackgroundService { private static readonly TimeSpan RequestTimeout = ShardManager.DeadShardThreshold / 3; - private readonly HashSet _skippedGuilds = new(); + private readonly HashSet _skippedGuilds = []; public AutoUserDownload(ShardInstance instance) : base(instance) => Shard.DiscordClient.Disconnected += OnDisconnect; diff --git a/BackgroundServices/BirthdayRoleUpdate.cs b/BackgroundServices/BirthdayRoleUpdate.cs index d1008f8..02f01c6 100644 --- a/BackgroundServices/BirthdayRoleUpdate.cs +++ b/BackgroundServices/BirthdayRoleUpdate.cs @@ -7,9 +7,7 @@ namespace BirthdayBot.BackgroundServices; /// Core automatic functionality of the bot. Manages role memberships based on birthday information, /// and optionally sends the announcement message to appropriate guilds. /// -class BirthdayRoleUpdate : BackgroundService { - public BirthdayRoleUpdate(ShardInstance instance) : base(instance) { } - +class BirthdayRoleUpdate(ShardInstance instance) : BackgroundService(instance) { /// /// Processes birthday updates for all available guilds synchronously. /// diff --git a/Configuration.cs b/Configuration.cs index a633ccd..a8fdb63 100644 --- a/Configuration.cs +++ b/Configuration.cs @@ -9,7 +9,9 @@ namespace BirthdayBot; /// /// Loads and holds configuration values. /// -class Configuration { +partial class Configuration { + [GeneratedRegex(@"(?\d{1,2})[-,](?\d{1,2})")] + private static partial Regex ShardRangeParser(); const string KeyShardRange = "ShardRange"; public string BotToken { get; } @@ -70,8 +72,7 @@ class Configuration { var shardRangeInput = args.ShardRange ?? ReadConfKey(jc, KeyShardRange, false); if (!string.IsNullOrWhiteSpace(shardRangeInput)) { - Regex srPicker = new(@"(?\d{1,2})[-,]{1}(?\d{1,2})"); - var m = srPicker.Match(shardRangeInput); + var m = ShardRangeParser().Match(shardRangeInput); if (m.Success) { ShardStart = int.Parse(m.Groups["low"].Value); var high = int.Parse(m.Groups["high"].Value); diff --git a/ShardManager.cs b/ShardManager.cs index 8e08b10..f6211df 100644 --- a/ShardManager.cs +++ b/ShardManager.cs @@ -36,7 +36,7 @@ class ShardManager : IDisposable { Config = cfg; // Allocate shards based on configuration - _shards = new Dictionary(); + _shards = []; for (var i = Config.ShardStart; i < (Config.ShardStart + Config.ShardAmount); i++) { _shards.Add(i, null); }