From d7ec0443405193dcf2c8205606a2f22b68e21169 Mon Sep 17 00:00:00 2001 From: Toastie <toastie@toastiet0ast.com> Date: Thu, 20 Mar 2025 15:24:18 +1300 Subject: [PATCH] .ve toggle fixed --- .../Modules/Utility/VerboseErrorsService.cs | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/EllieBot/Modules/Utility/VerboseErrorsService.cs b/src/EllieBot/Modules/Utility/VerboseErrorsService.cs index 748c7c2..7420500 100644 --- a/src/EllieBot/Modules/Utility/VerboseErrorsService.cs +++ b/src/EllieBot/Modules/Utility/VerboseErrorsService.cs @@ -1,4 +1,5 @@ #nullable disable +using LinqToDB; using LinqToDB.EntityFrameworkCore; using EllieBot.Common.ModuleBehaviors; using EllieBot.Db.Models; @@ -36,10 +37,10 @@ public class VerboseErrorsService : IReadyExecutor, IEService try { var embed = _hs.GetCommandHelp(cmd, channel.Guild) - .WithTitle("Command Error") - .WithDescription(reason) - .WithFooter("Admin may disable verbose errors via `.ve` command") - .WithErrorColor(); + .WithTitle("Command Error") + .WithDescription(reason) + .WithFooter("Admin may disable verbose errors via `.ve` command") + .WithErrorColor(); await _sender.Response(channel).Embed(embed).SendAsync(); } @@ -50,16 +51,29 @@ public class VerboseErrorsService : IReadyExecutor, IEService } } + /// <summary> + /// Toggles or sets verbose errors for the specified guild. + /// </summary> + /// <param name="guildId">The ID of the guild to toggle verbose errors for.</param> + /// <param name="maybeEnabled">If specified, sets to this value; otherwise toggles current value.</param> + /// <returns>Returns the new state of verbose errors (true = enabled, false = disabled).</returns> public async Task<bool> ToggleVerboseErrors(ulong guildId, bool? maybeEnabled = null) { await using var ctx = _db.GetDbContext(); - var isEnabled = ctx.GetTable<GuildConfig>() - .Where(x => x.GuildId == guildId) - .Select(x => x.VerboseErrors) - .FirstOrDefault(); - - if (isEnabled) // This doesn't need to be duplicated inside the using block + var current = await ctx.GetTable<GuildConfig>() + .Where(x => x.GuildId == guildId) + .Select(x => x.VerboseErrors) + .FirstOrDefaultAsync(); + + var newState = maybeEnabled ?? !current; + + await ctx.GetTable<GuildConfig>() + .Where(x => x.GuildId == guildId) + .Set(x => x.VerboseErrors, newState) + .UpdateAsync(); + + if (newState) { _guildsDisabled.TryRemove(guildId); } @@ -68,15 +82,15 @@ public class VerboseErrorsService : IReadyExecutor, IEService _guildsDisabled.Add(guildId); } - return isEnabled; + return newState; } public async Task OnReadyAsync() { await using var ctx = _db.GetDbContext(); var disabledOn = ctx.GetTable<GuildConfig>() - .Where(x => Queries.GuildOnShard(x.GuildId, _shardData.TotalShards, _shardData.ShardId) && !x.VerboseErrors) - .Select(x => x.GuildId); + .Where(x => Queries.GuildOnShard(x.GuildId, _shardData.TotalShards, _shardData.ShardId) && !x.VerboseErrors) + .Select(x => x.GuildId); foreach (var guildId in disabledOn) _guildsDisabled.Add(guildId);