.ve toggle fixed

This commit is contained in:
Toastie 2025-03-20 15:24:18 +13:00
parent 5b9cd88848
commit d7ec044340
Signed by: toastie_t0ast
GPG key ID: 74226CF45EEE5AAF

View file

@ -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);