From 679f311e69e7ca330f4294d9d19edc23cf843658 Mon Sep 17 00:00:00 2001 From: FloatingMilkshake Date: Mon, 9 Dec 2024 08:44:49 -0500 Subject: [PATCH] Add 'debug overrides cleanup' --- Commands/Debug.cs | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Commands/Debug.cs b/Commands/Debug.cs index eb91b2e2..0b0bd03f 100644 --- a/Commands/Debug.cs +++ b/Commands/Debug.cs @@ -533,6 +533,56 @@ public async Task DumpFromDb(CommandContext ctx, await ctx.RespondAsync($"Dump from db:\n{await StringHelpers.CodeOrHasteBinAsync(output, "json")}"); } } + + [Command("cleanup")] + [Aliases("clean", "prune")] + [Description("Removes overrides from the db for channels that no longer exist.")] + [IsBotOwner] + public async Task CleanUpOverrides(CommandContext ctx) + { + var msg = await ctx.RespondAsync($"{Program.cfgjson.Emoji.Loading} Working on it..."); + var removedOverridesCount = 0; + + var dbOverwrites = await Program.db.HashGetAllAsync("overrides"); + foreach (var userOverwrites in dbOverwrites) + { + var overwriteDict = JsonConvert.DeserializeObject>(userOverwrites.Value); + foreach (var overwrite in overwriteDict) + { + bool channelExists = false; + try + { + await Program.discord.GetChannelAsync(overwrite.Key); + channelExists = true; + } + catch (DSharpPlus.Exceptions.NotFoundException) + { + // Channel doesn't exist, leave bool false + } + + if (!channelExists) + { + // Channel no longer exists, remove the override + overwriteDict.Remove(overwrite.Key); + removedOverridesCount++; + } + } + + // Write back to db + // If the user now has no overrides, remove them from the db entirely + if (overwriteDict.Count == 0) + { + await Program.db.HashDeleteAsync("overrides", userOverwrites.Name); + } + else + { + // Otherwise, update the user's overrides in the db + await Program.db.HashSetAsync("overrides", userOverwrites.Name, JsonConvert.SerializeObject(overwriteDict)); + } + } + + await msg.ModifyAsync($"{Program.cfgjson.Emoji.Success} Done! Cleaned up {removedOverridesCount} overrides."); + } } [Command("dmchannel")]