Implement new suggestions from framework upgrade

This commit is contained in:
Noi 2024-04-28 18:15:12 -07:00
parent 284e71724a
commit e6e335b5dc
6 changed files with 16 additions and 15 deletions

View file

@ -10,7 +10,7 @@ namespace BirthdayBot.ApplicationCommands;
/// <summary>
/// Base class for our interaction module classes. Contains common data for use in implementing classes.
/// </summary>
public abstract class BotModuleBase : InteractionModuleBase<SocketInteractionContext> {
public abstract partial class BotModuleBase : InteractionModuleBase<SocketInteractionContext> {
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<SocketInteractionCon
const string FormatError = ":x: Unrecognized date format. The following formats are accepted, as examples: "
+ "`15-jan`, `jan-15`, `15 jan`, `jan 15`, `15 January`, `January 15`.";
private static readonly Regex DateParse1 = new(@"^(?<day>\d{1,2})[ -](?<month>[A-Za-z]+)$", RegexOptions.Compiled);
private static readonly Regex DateParse2 = new(@"^(?<month>[A-Za-z]+)[ -](?<day>\d{1,2})$", RegexOptions.Compiled);
[GeneratedRegex(@"^(?<day>\d{1,2})[ -](?<month>[A-Za-z]+)$")]
private static partial Regex DateParser1();
[GeneratedRegex(@"^(?<month>[A-Za-z]+)[ -](?<day>\d{1,2})$")]
private static partial Regex DateParser2();
/// <summary>
/// Parses a date input.
@ -76,10 +78,10 @@ public abstract class BotModuleBase : InteractionModuleBase<SocketInteractionCon
/// Thrown for any parsing issue. Reason is expected to be sent to Discord as-is.
/// </exception>
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);
}

View file

@ -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<ListItem> list) {
private static MemoryStream ListExportNormal(SocketGuild guild, IEnumerable<ListItem> 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<ListItem> list) {
private static MemoryStream ListExportCsv(SocketGuild guild, IEnumerable<ListItem> list) {
// Output: User ID, Username, Nickname, Month-Day, Month, Day
var result = new MemoryStream();
var writer = new StreamWriter(result, Encoding.UTF8);

View file

@ -8,7 +8,7 @@ namespace BirthdayBot.BackgroundServices;
class AutoUserDownload : BackgroundService {
private static readonly TimeSpan RequestTimeout = ShardManager.DeadShardThreshold / 3;
private readonly HashSet<ulong> _skippedGuilds = new();
private readonly HashSet<ulong> _skippedGuilds = [];
public AutoUserDownload(ShardInstance instance) : base(instance)
=> Shard.DiscordClient.Disconnected += OnDisconnect;

View file

@ -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.
/// </summary>
class BirthdayRoleUpdate : BackgroundService {
public BirthdayRoleUpdate(ShardInstance instance) : base(instance) { }
class BirthdayRoleUpdate(ShardInstance instance) : BackgroundService(instance) {
/// <summary>
/// Processes birthday updates for all available guilds synchronously.
/// </summary>

View file

@ -9,7 +9,9 @@ namespace BirthdayBot;
/// <summary>
/// Loads and holds configuration values.
/// </summary>
class Configuration {
partial class Configuration {
[GeneratedRegex(@"(?<low>\d{1,2})[-,](?<high>\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<string>(jc, KeyShardRange, false);
if (!string.IsNullOrWhiteSpace(shardRangeInput)) {
Regex srPicker = new(@"(?<low>\d{1,2})[-,]{1}(?<high>\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);

View file

@ -36,7 +36,7 @@ class ShardManager : IDisposable {
Config = cfg;
// Allocate shards based on configuration
_shards = new Dictionary<int, ShardInstance?>();
_shards = [];
for (var i = Config.ShardStart; i < (Config.ShardStart + Config.ShardAmount); i++) {
_shards.Add(i, null);
}