diff --git a/src/NadekoBot/Common/NadekoRandom.cs b/src/NadekoBot/Common/NadekoRandom.cs index 7094171b2..7df59ed94 100644 --- a/src/NadekoBot/Common/NadekoRandom.cs +++ b/src/NadekoBot/Common/NadekoRandom.cs @@ -2,7 +2,7 @@ using System.Security.Cryptography; namespace NadekoBot.Common; - +// todo minby maxby public class NadekoRandom : Random { private readonly RandomNumberGenerator _rng; diff --git a/src/NadekoBot/Modules/Gambling/Gambling.cs b/src/NadekoBot/Modules/Gambling/Gambling.cs index 9550c4877..8b9ce9f65 100644 --- a/src/NadekoBot/Modules/Gambling/Gambling.cs +++ b/src/NadekoBot/Modules/Gambling/Gambling.cs @@ -64,7 +64,6 @@ public partial class Gambling : GamblingModule { var flowersCi = (CultureInfo)Culture.Clone(); flowersCi.NumberFormat.CurrencySymbol = CurrencySign; - Log.Information(string.Join(",", flowersCi.NumberFormat.NativeDigits)); return cur.ToString("C0", flowersCi); } @@ -109,10 +108,9 @@ public partial class Gambling : GamblingModule return; } - TimeSpan? rem; - if ((rem = _cache.AddTimelyClaim(ctx.User.Id, period)) is not null) + if (_cache.AddTimelyClaim(ctx.User.Id, period) is { } rem) { - await ReplyErrorLocalizedAsync(strs.timely_already_claimed(rem?.ToString(@"dd\d\ hh\h\ mm\m\ ss\s"))); + await ReplyErrorLocalizedAsync(strs.timely_already_claimed(rem.ToString(@"dd\d\ hh\h\ mm\m\ ss\s"))); return; } @@ -240,7 +238,7 @@ public partial class Gambling : GamblingModule [Cmd] [RequireContext(ContextType.Guild)] [Priority(0)] - public async partial Task Give(ShmartNumber amount, IGuildUser receiver, [Leftover] string msg = null) + public async partial Task Give(ShmartNumber amount, IGuildUser receiver, [Leftover] string msg) { if (amount <= 0 || ctx.User.Id == receiver.Id || receiver.IsBot) return; @@ -409,15 +407,15 @@ public partial class Gambling : GamblingModule if (_service.Duels.TryAdd((u.Id, ctx.User.Id), game)) { - game.OnGameTick += Game_OnGameTick; - game.OnEnded += Game_OnEnded; + game.OnGameTick += GameOnGameTick; + game.OnEnded += GameOnEnded; await ReplyConfirmLocalizedAsync(strs.roll_duel_challenge(Format.Bold(ctx.User.ToString()), Format.Bold(u.ToString()), Format.Bold(n(amount)))); } - async Task Game_OnGameTick(RollDuelGame arg) + async Task GameOnGameTick(RollDuelGame arg) { var rolls = arg.Rolls.Last(); description += $@"{Format.Bold(ctx.User.ToString())} rolled **{rolls.Item1}** @@ -435,7 +433,7 @@ public partial class Gambling : GamblingModule }); } - async Task Game_OnEnded(RollDuelGame rdGame, RollDuelGame.Reason reason) + async Task GameOnEnded(RollDuelGame rdGame, RollDuelGame.Reason reason) { try { @@ -515,14 +513,13 @@ public partial class Gambling : GamblingModule var (opts, _) = OptionsParser.ParseFrom(new LbOpts(), args); - var cleanRichest = new List(); + List cleanRichest; // it's pointless to have clean on dm context - if (ctx.Guild is null) opts.Clean = false; + if (ctx.Guild is null) + opts.Clean = false; if (opts.Clean) { - var now = DateTime.UtcNow; - await using (var uow = _db.GetDbContext()) { cleanRichest = uow.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 10_000); @@ -585,7 +582,7 @@ public partial class Gambling : GamblingModule if (!await CheckBetOptional(amount) || amount == 1) return; - string getRpsPick(RpsPick p) + string GetRpsPick(RpsPick p) { switch (p) { @@ -614,7 +611,7 @@ public partial class Gambling : GamblingModule { await _cs.AddAsync(ctx.User.Id, "Rps-draw", amount, true); embed.WithOkColor(); - msg = GetText(strs.rps_draw(getRpsPick(pick))); + msg = GetText(strs.rps_draw(GetRpsPick(pick))); } else if ((pick == RpsPick.Paper && nadekoPick == RpsPick.Rock) || (pick == RpsPick.Rock && nadekoPick == RpsPick.Scissors) @@ -624,13 +621,13 @@ public partial class Gambling : GamblingModule await _cs.AddAsync(ctx.User.Id, "Rps-win", amount, true); embed.WithOkColor(); embed.AddField(GetText(strs.won), n(amount)); - msg = GetText(strs.rps_win(ctx.User.Mention, getRpsPick(pick), getRpsPick(nadekoPick))); + msg = GetText(strs.rps_win(ctx.User.Mention, GetRpsPick(pick), GetRpsPick(nadekoPick))); } else { embed.WithErrorColor(); amount = 0; - msg = GetText(strs.rps_win(ctx.Client.CurrentUser.Mention, getRpsPick(nadekoPick), getRpsPick(pick))); + msg = GetText(strs.rps_win(ctx.Client.CurrentUser.Mention, GetRpsPick(nadekoPick), GetRpsPick(pick))); } embed.WithDescription(msg); diff --git a/src/NadekoBot/Modules/Utility/Remind/RemindService.cs b/src/NadekoBot/Modules/Utility/Remind/RemindService.cs index 7ba46abae..d9b70c798 100644 --- a/src/NadekoBot/Modules/Utility/Remind/RemindService.cs +++ b/src/NadekoBot/Modules/Utility/Remind/RemindService.cs @@ -1,5 +1,6 @@ #nullable disable -using Microsoft.EntityFrameworkCore; +using LinqToDB; +using LinqToDB.EntityFrameworkCore; using NadekoBot.Services.Database.Models; using System.Text.RegularExpressions; @@ -42,29 +43,30 @@ public class RemindService : INService if (reminders.Count == 0) continue; - Log.Information($"Executing {reminders.Count} reminders."); + Log.Information("Executing {ReminderCount} reminders", reminders.Count); // make groups of 5, with 1.5 second inbetween each one to ensure against ratelimits foreach (var group in reminders.Chunk(5)) { var executedReminders = group.ToList(); await executedReminders.Select(ReminderTimerAction).WhenAll(); - await RemoveReminders(executedReminders); + await RemoveReminders(executedReminders.Select(x => x.Id)); await Task.Delay(1500); } } catch (Exception ex) { - Log.Warning($"Error in reminder loop: {ex.Message}"); - Log.Warning(ex.ToString()); + Log.Warning(ex, "Error in reminder loop: {ErrorMessage}", ex.Message); } } } - private async Task RemoveReminders(List reminders) + private async Task RemoveReminders(IEnumerable reminders) { await using var uow = _db.GetDbContext(); - uow.Set().RemoveRange(reminders); + await uow.Reminders + .ToLinqToDBTable() + .DeleteAsync(x => reminders.Contains(x.Id)); await uow.SaveChangesAsync(); } @@ -73,9 +75,12 @@ public class RemindService : INService { using var uow = _db.GetDbContext(); return uow.Reminders - .FromSqlInterpolated( - $"select * from reminders where ((serverid >> 22) % {_creds.TotalShards}) == {_client.ShardId} and \"when\" < {now};") - .ToListAsync(); + .ToLinqToDBTable() + .Where(x => (x.ServerId / 4194304 % (ulong)_creds.TotalShards == (ulong)_client.ShardId) + && x.When < now) + // .FromSqlInterpolated( + // $"select * from reminders where ((serverid >> 22) % {_creds.TotalShards}) == {_client.ShardId} and \"when\" < {now};") + .ToListAsyncLinqToDB(); } public bool TryParseRemindMessage(string input, out RemindObject obj) @@ -91,7 +96,7 @@ public class RemindService : INService if (string.IsNullOrWhiteSpace(what)) { - Log.Warning("No message provided for the reminder."); + Log.Warning("No message provided for the reminder"); return false; } @@ -106,13 +111,13 @@ public class RemindService : INService if (!int.TryParse(m.Groups[groupName].Value, out var value)) { - Log.Warning($"Reminder regex group {groupName} has invalid value."); + Log.Warning("Reminder regex group {GroupName} has invalid value", groupName); return false; } if (value < 1) { - Log.Warning("Reminder time value has to be an integer greater than 0."); + Log.Warning("Reminder time value has to be an integer greater than 0"); return false; } @@ -155,7 +160,10 @@ public class RemindService : INService (await ch.GetUserAsync(r.UserId))?.ToString() ?? r.UserId.ToString()), r.Message); } - catch (Exception ex) { Log.Information(ex.Message + $"({r.Id})"); } + catch (Exception ex) + { + Log.Warning(ex, "Error executing reminder {ReminderId}: {ErrorMessage}", r.Id, ex.Message); + } } public struct RemindObject diff --git a/src/NadekoBot/NadekoBot.csproj b/src/NadekoBot/NadekoBot.csproj index a5a6ad6e2..a673fb374 100644 --- a/src/NadekoBot/NadekoBot.csproj +++ b/src/NadekoBot/NadekoBot.csproj @@ -50,6 +50,7 @@ +