diff --git a/src/EllieBot/Db/EllieContext.cs b/src/EllieBot/Db/EllieContext.cs index 11a3d65..50de935 100644 --- a/src/EllieBot/Db/EllieContext.cs +++ b/src/EllieBot/Db/EllieContext.cs @@ -688,7 +688,7 @@ public abstract class EllieContext : DbContext x.GuildId, x.GreetType }) - .IsUnique()); + .IsUnique()); modelBuilder.Entity(gs => { diff --git a/src/EllieBot/Modules/Administration/UserPunish/UserPunishService.cs b/src/EllieBot/Modules/Administration/UserPunish/UserPunishService.cs index 2b9e00d..85dcb10 100644 --- a/src/EllieBot/Modules/Administration/UserPunish/UserPunishService.cs +++ b/src/EllieBot/Modules/Administration/UserPunish/UserPunishService.cs @@ -244,7 +244,7 @@ public class UserPunishService : IEService, IReadyExecutor { await using var uow = _db.GetDbContext(); - var cleared = await uow.GetTable() + var toClear = await uow.GetTable() .Where(x => uow.GetTable() .Count(y => y.GuildId == x.GuildId && y.WarnExpireHours > 0 @@ -256,30 +256,40 @@ public class UserPunishService : IEService, IReadyExecutor .Where(y => x.GuildId == y.GuildId) .Select(y => y.WarnExpireHours) .First())) - .UpdateAsync(_ => new() - { - Forgiven = true, - ForgivenBy = "expiry" - }); + .Select(x => x.Id) + .ToListAsyncLinqToDB(); + + var cleared = await uow.GetTable() + .Where(x => toClear.Contains(x.Id)) + .UpdateAsync(_ => new() + { + Forgiven = true, + ForgivenBy = "expiry" + }); + + var toDelete = await uow.GetTable() + .Where(x => uow.GetTable() + .Count(y => y.GuildId == x.GuildId + && y.WarnExpireHours > 0 + && y.WarnExpireAction == WarnExpireAction.Delete) + > 0 + && x.DateAdded + < DateTime.UtcNow.AddHours(-uow.GetTable() + .Where(y => x.GuildId == y.GuildId) + .Select(y => y.WarnExpireHours) + .First())) + .Select(x => x.Id) + .ToListAsyncLinqToDB(); var deleted = await uow.GetTable() - .Where(x => uow.GetTable() - .Count(y => y.GuildId == x.GuildId - && y.WarnExpireHours > 0 - && y.WarnExpireAction == WarnExpireAction.Delete) - > 0 - && x.DateAdded - < DateTime.UtcNow.AddHours(-uow.GetTable() - .Where(y => x.GuildId == y.GuildId) - .Select(y => y.WarnExpireHours) - .First())) - .DeleteAsync(); + .Where(x => toDelete.Contains(x.Id)) + .DeleteAsync(); if (cleared > 0 || deleted > 0) { Log.Information("Cleared {ClearedWarnings} warnings and deleted {DeletedWarnings} warnings due to expiry", cleared, - deleted); + toDelete.Count); } }