mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 17:58:26 -04:00
Restructured the project structure back to the way it was, there's no reasonable way to split the modules
This commit is contained in:
69
src/NadekoBot/Modules/Utility/VerboseErrorsService.cs
Normal file
69
src/NadekoBot/Modules/Utility/VerboseErrorsService.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Db;
|
||||
|
||||
namespace NadekoBot.Modules.Utility.Services;
|
||||
|
||||
public class VerboseErrorsService : INService
|
||||
{
|
||||
private readonly ConcurrentHashSet<ulong> _guildsDisabled;
|
||||
private readonly DbService _db;
|
||||
private readonly CommandHandler _ch;
|
||||
private readonly ICommandsUtilityService _hs;
|
||||
|
||||
public VerboseErrorsService(
|
||||
IBot bot,
|
||||
DbService db,
|
||||
CommandHandler ch,
|
||||
ICommandsUtilityService hs)
|
||||
{
|
||||
_db = db;
|
||||
_ch = ch;
|
||||
_hs = hs;
|
||||
|
||||
_ch.CommandErrored += LogVerboseError;
|
||||
|
||||
_guildsDisabled = new(bot.AllGuildConfigs.Where(x => !x.VerboseErrors).Select(x => x.GuildId));
|
||||
}
|
||||
|
||||
private async Task LogVerboseError(CommandInfo cmd, ITextChannel channel, string reason)
|
||||
{
|
||||
if (channel is null || _guildsDisabled.Contains(channel.GuildId))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var embed = _hs.GetCommandHelp(cmd, channel.Guild)
|
||||
.WithTitle("Command Error")
|
||||
.WithDescription(reason)
|
||||
.WithFooter("Admin may disable verbose errors via `.ve` command")
|
||||
.WithErrorColor();
|
||||
|
||||
await channel.EmbedAsync(embed);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Log.Information("Verbose error wasn't able to be sent to the server: {GuildId}",
|
||||
channel.GuildId);
|
||||
}
|
||||
}
|
||||
|
||||
public bool ToggleVerboseErrors(ulong guildId, bool? maybeEnabled = null)
|
||||
{
|
||||
using var uow = _db.GetDbContext();
|
||||
var gc = uow.GuildConfigsForId(guildId, set => set);
|
||||
|
||||
if (maybeEnabled is bool isEnabled) // set it
|
||||
gc.VerboseErrors = isEnabled;
|
||||
else // toggle it
|
||||
isEnabled = gc.VerboseErrors = !gc.VerboseErrors;
|
||||
|
||||
uow.SaveChanges();
|
||||
|
||||
if (isEnabled) // This doesn't need to be duplicated inside the using block
|
||||
_guildsDisabled.TryRemove(guildId);
|
||||
else
|
||||
_guildsDisabled.Add(guildId);
|
||||
|
||||
return isEnabled;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user