From a6c2ec0511010f9ca50a015c10fe7263a3033884 Mon Sep 17 00:00:00 2001 From: Toastie Date: Tue, 6 Aug 2024 22:52:02 +1200 Subject: [PATCH] small cleanup --- src/EllieBot/Bot.cs | 2 +- .../Administration/Self/SelfCommands.cs | 2 +- .../Gambling/PlantPick/PlantPickService.cs | 2 -- .../Gambling/Waifus/WaifuClaimCommands.cs | 12 ++++--- .../Trivia/{Games.cs => TriviaCommands.cs} | 6 ++-- .../Modules/Games/Trivia/TriviaGame.cs | 23 +++++++++---- .../_common/Resolvers/RadioResolveStrategy.cs | 12 +++---- .../Modules/Patronage/PatronageService.cs | 33 ++++++++----------- .../Modules/Searches/Crypto/CryptoCommands.cs | 12 ------- .../Utility/Giveaway/GiveawayService.cs | 1 - ...ernelExtensions.cs => DryIocExtensions.cs} | 8 ----- .../Common/MarmaladeIoCKernelModule.cs | 2 +- 12 files changed, 49 insertions(+), 66 deletions(-) rename src/EllieBot/Modules/Games/Trivia/{Games.cs => TriviaCommands.cs} (98%) rename src/EllieBot/_common/{NinjectIKernelExtensions.cs => DryIocExtensions.cs} (80%) diff --git a/src/EllieBot/Bot.cs b/src/EllieBot/Bot.cs index 9ab0f92..a8c5fd3 100644 --- a/src/EllieBot/Bot.cs +++ b/src/EllieBot/Bot.cs @@ -114,7 +114,7 @@ public sealed class Bot : IBot // svcs.Components.Remove(); // svcs.Components.Add(); - svcs.AddSingleton(_ => _credsProvider.GetCreds()); + svcs.AddSingleton(_ => _credsProvider.GetCreds()); svcs.AddSingleton(_db); svcs.AddSingleton(_credsProvider); svcs.AddSingleton(Client); diff --git a/src/EllieBot/Modules/Administration/Self/SelfCommands.cs b/src/EllieBot/Modules/Administration/Self/SelfCommands.cs index d67ac93..b8842cd 100644 --- a/src/EllieBot/Modules/Administration/Self/SelfCommands.cs +++ b/src/EllieBot/Modules/Administration/Self/SelfCommands.cs @@ -546,7 +546,7 @@ public partial class Administration text = await repSvc.ReplaceAsync(text, repCtx); await Response().Channel(ch).Text(text).SendAsync(); - await ctx.OkAsync();; + await ctx.OkAsync(); } [Cmd] diff --git a/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs b/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs index 3fac5ba..bffd035 100644 --- a/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs +++ b/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs @@ -31,7 +31,6 @@ public class PlantPickService : IEService, IExecNoCommand public PlantPickService( DbService db, - CommandHandler cmd, IBotStrings strings, IImageCache images, FontProvider fonts, @@ -107,7 +106,6 @@ public class PlantPickService : IEService, IExecNoCommand /// Get a random currency image stream, with an optional password sticked onto it. /// /// Optional password to add to top left corner. - /// Extension of the file, defaults to png /// Stream of the currency image public async Task<(Stream, string)> GetRandomCurrencyImageAsync(string pass) { diff --git a/src/EllieBot/Modules/Gambling/Waifus/WaifuClaimCommands.cs b/src/EllieBot/Modules/Gambling/Waifus/WaifuClaimCommands.cs index 3730ded..d488e72 100644 --- a/src/EllieBot/Modules/Gambling/Waifus/WaifuClaimCommands.cs +++ b/src/EllieBot/Modules/Gambling/Waifus/WaifuClaimCommands.cs @@ -3,6 +3,7 @@ using EllieBot.Modules.Gambling.Common; using EllieBot.Modules.Gambling.Common.Waifu; using EllieBot.Modules.Gambling.Services; using EllieBot.Db.Models; +using System.Globalization; namespace EllieBot.Modules.Gambling; @@ -152,12 +153,12 @@ public partial class Gambling await Response().Confirm(strs.waifu_divorced_notlike(N(amount))).SendAsync(); else if (result == DivorceResult.NotYourWife) await Response().Error(strs.waifu_not_yours).SendAsync(); - else + else if (remaining is { } rem) { await Response() .Error(strs.waifu_recent_divorce( - Format.Bold(((int)remaining?.TotalHours).ToString()), - Format.Bold(remaining?.Minutes.ToString()))) + Format.Bold(((int)rem.TotalHours).ToString()), + Format.Bold(rem.Minutes.ToString()))) .SendAsync(); } } @@ -237,7 +238,7 @@ public partial class Gambling private string GetLbString(WaifuLbResult w) { var claimer = "no one"; - var status = string.Empty; + string status; var waifuUsername = w.Username.TrimTo(20); var claimerUsername = w.Claimer?.TrimTo(20); @@ -376,7 +377,8 @@ public partial class Gambling if (sucess) { await Response() - .Confirm(strs.waifu_gift(Format.Bold($"{GetCountString(items)}{items.Item} {items.Item.ItemEmoji}"), + .Confirm(strs.waifu_gift( + Format.Bold($"{GetCountString(items)}{items.Item} {items.Item.ItemEmoji}"), Format.Bold(waifu.ToString()))) .SendAsync(); } diff --git a/src/EllieBot/Modules/Games/Trivia/Games.cs b/src/EllieBot/Modules/Games/Trivia/TriviaCommands.cs similarity index 98% rename from src/EllieBot/Modules/Games/Trivia/Games.cs rename to src/EllieBot/Modules/Games/Trivia/TriviaCommands.cs index ff7ffbd..72b4eb4 100644 --- a/src/EllieBot/Modules/Games/Trivia/Games.cs +++ b/src/EllieBot/Modules/Games/Trivia/TriviaCommands.cs @@ -107,9 +107,9 @@ public partial class Games return sb.ToString(); } - private EmbedBuilder? questionEmbed = null; - private IUserMessage? questionMessage = null; - private bool showHowToQuit = false; + private EmbedBuilder? questionEmbed; + private IUserMessage? questionMessage; + private bool showHowToQuit; private void RegisterEvents(TriviaGame trivia) { diff --git a/src/EllieBot/Modules/Games/Trivia/TriviaGame.cs b/src/EllieBot/Modules/Games/Trivia/TriviaGame.cs index 4223104..07c4ab4 100644 --- a/src/EllieBot/Modules/Games/Trivia/TriviaGame.cs +++ b/src/EllieBot/Modules/Games/Trivia/TriviaGame.cs @@ -11,12 +11,19 @@ public sealed class TriviaGame private readonly IQuestionPool _questionPool; #region Events + public event Func OnQuestion = static delegate { return Task.CompletedTask; }; public event Func OnHint = static delegate { return Task.CompletedTask; }; public event Func OnStats = static delegate { return Task.CompletedTask; }; - public event Func OnGuess = static delegate { return Task.CompletedTask; }; + + public event Func OnGuess = static delegate + { + return Task.CompletedTask; + }; + public event Func OnTimeout = static delegate { return Task.CompletedTask; }; public event Func OnEnded = static delegate { return Task.CompletedTask; }; + #endregion private bool _isStopped; @@ -24,7 +31,7 @@ public sealed class TriviaGame public TriviaQuestion? CurrentQuestion { get; set; } - private readonly ConcurrentDictionary _users = new (); + private readonly ConcurrentDictionary _users = new(); private readonly Channel<(TriviaUser User, string Input)> _inputs = Channel.CreateUnbounded<(TriviaUser, string)>(new UnboundedChannelOptions @@ -41,8 +48,8 @@ public sealed class TriviaGame _questionPool = _opts.IsPokemon ? new PokemonQuestionPool(cache) : new DefaultQuestionPool(cache); - } + public async Task RunAsync() { await GameLoop(); @@ -50,7 +57,8 @@ public sealed class TriviaGame private async Task GameLoop() { - Task TimeOutFactory() => Task.Delay(_opts.QuestionTimer * 1000 / 2); + Task TimeOutFactory() + => Task.Delay(_opts.QuestionTimer * 1000 / 2); var errorCount = 0; var inactivity = 0; @@ -91,7 +99,8 @@ public sealed class TriviaGame { // clear out all of the past guesses while (_inputs.Reader.TryRead(out _)) - ; + { + } await OnQuestion(this, question); } @@ -121,7 +130,7 @@ public sealed class TriviaGame if (task == halfGuessTimerTask) { readCancel.Cancel(); - + // if hint is already sent, means time expired // break (end the round) if (hintSent) @@ -213,7 +222,7 @@ public sealed class TriviaGame public async Task TriggerQuestionAsync() { - if(CurrentQuestion is TriviaQuestion q) + if (CurrentQuestion is TriviaQuestion q) await OnQuestion(this, q); } } \ No newline at end of file diff --git a/src/EllieBot/Modules/Music/_common/Resolvers/RadioResolveStrategy.cs b/src/EllieBot/Modules/Music/_common/Resolvers/RadioResolveStrategy.cs index c3733a4..475b026 100644 --- a/src/EllieBot/Modules/Music/_common/Resolvers/RadioResolveStrategy.cs +++ b/src/EllieBot/Modules/Music/_common/Resolvers/RadioResolveStrategy.cs @@ -61,8 +61,8 @@ public class RadioResolver : IRadioResolver try { var m = _m3URegex.Match(file); - var res = m.Groups["url"]?.ToString(); - return res?.Trim(); + var res = m.Groups["url"].ToString(); + return res.Trim(); } catch { @@ -76,8 +76,8 @@ public class RadioResolver : IRadioResolver try { var m = _asxRegex.Match(file); - var res = m.Groups["url"]?.ToString(); - return res?.Trim(); + var res = m.Groups["url"].ToString(); + return res.Trim(); } catch { @@ -91,8 +91,8 @@ public class RadioResolver : IRadioResolver try { var m = _xspfRegex.Match(file); - var res = m.Groups["url"]?.ToString(); - return res?.Trim(); + var res = m.Groups["url"].ToString(); + return res.Trim(); } catch { diff --git a/src/EllieBot/Modules/Patronage/PatronageService.cs b/src/EllieBot/Modules/Patronage/PatronageService.cs index ff0ced6..0ea46e4 100644 --- a/src/EllieBot/Modules/Patronage/PatronageService.cs +++ b/src/EllieBot/Modules/Patronage/PatronageService.cs @@ -19,16 +19,11 @@ public sealed class PatronageService public int Priority => int.MinValue; - private static readonly PatronTier[] _tiers = Enum.GetValues(); - private readonly PatronageConfig _pConf; private readonly DbService _db; private readonly DiscordSocketClient _client; private readonly ISubscriptionHandler _subsHandler; - private static readonly TypedKey _quotaKey - = new($"quota:last_hourly_reset"); - private readonly IBotCache _cache; private readonly IBotCredsProvider _creds; private readonly IMessageSenderService _sender; @@ -133,19 +128,19 @@ public sealed class PatronageService // user is charged again for this month // if his sub would end in teh future, extend it by one month. // if it's not, just add 1 month to the last charge date - var count = await ctx.GetTable() - .Where(x => x.UniquePlatformUserId - == subscriber.UniquePlatformUserId) - .UpdateAsync(old => new() - { - UserId = subscriber.UserId, - AmountCents = subscriber.Cents, - LastCharge = lastChargeUtc, - ValidThru = old.ValidThru >= todayDate - // ? Sql.DateAdd(Sql.DateParts.Month, 1, old.ValidThru).Value - ? old.ValidThru.AddMonths(1) - : dateInOneMonth, - }); + await ctx.GetTable() + .Where(x => x.UniquePlatformUserId + == subscriber.UniquePlatformUserId) + .UpdateAsync(old => new() + { + UserId = subscriber.UserId, + AmountCents = subscriber.Cents, + LastCharge = lastChargeUtc, + ValidThru = old.ValidThru >= todayDate + // ? Sql.DateAdd(Sql.DateParts.Month, 1, old.ValidThru).Value + ? old.ValidThru.AddMonths(1) + : dateInOneMonth, + }); dbPatron.UserId = subscriber.UserId; @@ -330,7 +325,7 @@ public sealed class PatronageService { if (!_pConf.Data.IsEnabled) return _infiniteQuota; - + var maybePatron = await GetPatronAsync(userId); if (maybePatron is not { } patron) diff --git a/src/EllieBot/Modules/Searches/Crypto/CryptoCommands.cs b/src/EllieBot/Modules/Searches/Crypto/CryptoCommands.cs index 1bb8910..290685d 100644 --- a/src/EllieBot/Modules/Searches/Crypto/CryptoCommands.cs +++ b/src/EllieBot/Modules/Searches/Crypto/CryptoCommands.cs @@ -65,18 +65,6 @@ public partial class Searches var change = (stock.Price - stock.Close).ToString("N2", Culture); var changePercent = (1 - (stock.Close / stock.Price)).ToString("P1", Culture); - var sign50 = stock.Change50d >= 0 - ? "\\🔼" - : "\\🔻"; - - var change50 = (stock.Change50d).ToString("P1", Culture); - - var sign200 = stock.Change200d >= 0 - ? "\\🔼" - : "\\🔻"; - - var change200 = (stock.Change200d).ToString("P1", Culture); - var price = stock.Price.ToString("C2", localCulture); var eb = _sender.CreateEmbed() diff --git a/src/EllieBot/Modules/Utility/Giveaway/GiveawayService.cs b/src/EllieBot/Modules/Utility/Giveaway/GiveawayService.cs index f43e7bb..200c88a 100644 --- a/src/EllieBot/Modules/Utility/Giveaway/GiveawayService.cs +++ b/src/EllieBot/Modules/Utility/Giveaway/GiveawayService.cs @@ -19,7 +19,6 @@ public sealed class GiveawayService : IEService, IReadyExecutor private readonly IMemoryCache _cache; private SortedSet _giveawayCache = new SortedSet(); private readonly EllieRandom _rng; - private readonly ConcurrentDictionary _rerolls = new(); public GiveawayService(DbService db, IBotCredentials creds, DiscordSocketClient client, IMessageSenderService sender, IBotStrings strings, ILocalization localization, IMemoryCache cache) diff --git a/src/EllieBot/_common/NinjectIKernelExtensions.cs b/src/EllieBot/_common/DryIocExtensions.cs similarity index 80% rename from src/EllieBot/_common/NinjectIKernelExtensions.cs rename to src/EllieBot/_common/DryIocExtensions.cs index 5b5b909..c7d8c2a 100644 --- a/src/EllieBot/_common/NinjectIKernelExtensions.cs +++ b/src/EllieBot/_common/DryIocExtensions.cs @@ -20,14 +20,6 @@ public static class DryIocExtensions return container; } - public static IContainer AddSingleton(this IContainer container, Func factory) - where TImpl : TSvc - { - container.RegisterDelegate(factory, Reuse.Singleton); - - return container; - } - public static IContainer AddSingleton(this IContainer container) { container.Register(Reuse.Singleton); diff --git a/src/EllieBot/_common/Marmalade/Common/MarmaladeIoCKernelModule.cs b/src/EllieBot/_common/Marmalade/Common/MarmaladeIoCKernelModule.cs index e55ddff..3b9c450 100644 --- a/src/EllieBot/_common/Marmalade/Common/MarmaladeIoCKernelModule.cs +++ b/src/EllieBot/_common/Marmalade/Common/MarmaladeIoCKernelModule.cs @@ -34,7 +34,7 @@ public sealed class MarmaladeNinjectIocModule : IIocModule, IDisposable if (isLoaded) return; - foreach (var (type, data) in _types) + foreach (var (type, _) in _types) { var attribute = type.GetCustomAttribute()!;