From 7f935a72c1eca75b47dedb7d7b73bf1f3348b629 Mon Sep 17 00:00:00 2001
From: Toastie <toastie@toastiet0ast.com>
Date: Tue, 27 Aug 2024 21:31:39 +1200
Subject: [PATCH] fix for .leaveunkeptservers

---
 .../DangerousCommands/CleanupService.cs       | 24 +++++++++----------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs
index be01a0b..86d077d 100644
--- a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs
+++ b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs
@@ -59,14 +59,19 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, IEService
 
             var allGuildIds = _client.Guilds.Select(x => x.Id);
 
-            var table = await GetKeptGuildsTable();
+            HashSet<ulong> dontDelete;
+            await using (var db = _db.GetDbContext())
+            {
+                await using var ctx = db.CreateLinqToDBConnection();
+                var table = ctx.CreateTable<KeptGuilds>(tableOptions: TableOptions.CheckExistence);
 
-            var dontDeleteList = await table
+                var dontDeleteList = await table
                                        .Where(x => allGuildIds.Contains(x.GuildId))
                                        .Select(x => x.GuildId)
                                        .ToListAsyncLinqToDB();
 
-            var dontDelete = dontDeleteList.ToHashSet();
+                dontDelete = dontDeleteList.ToHashSet();
+            }
 
             guildIds = new();
             foreach (var guildId in allGuildIds)
@@ -209,8 +214,9 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, IEService
 
     public async Task<bool> KeepGuild(ulong guildId)
     {
-        var table = await GetKeptGuildsTable();
-
+        await using var db = _db.GetDbContext();
+        await using var ctx = db.CreateLinqToDbContext();
+        var table = ctx.CreateTable<KeptGuilds>(tableOptions: TableOptions.CheckExistence);
         if (await table.AnyAsyncLinqToDB(x => x.GuildId == guildId))
             return false;
 
@@ -223,17 +229,11 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, IEService
     }
 
     public async Task<int> GetKeptGuildCount()
-    {
-        var table = await GetKeptGuildsTable();
-        return await table.CountAsync();
-    }
-
-    private async Task<ITable<KeptGuilds>> GetKeptGuildsTable()
     {
         await using var db = _db.GetDbContext();
         await using var ctx = db.CreateLinqToDBContext();
         var table = ctx.CreateTable<KeptGuilds>(tableOptions: TableOptions.CheckExistence);
-        return table;
+        return await table.CountAsync();
     }
 
     public async Task LeaveUnkeptServers()