Fixed some crashes in response strings source generator, reorganized more submodules into their folders

This commit is contained in:
Kwoth
2022-01-02 03:49:54 +01:00
parent 9c590668df
commit 4b6af0e4ef
191 changed files with 120 additions and 80 deletions

View File

@@ -0,0 +1,24 @@
#nullable disable
using NadekoBot.Modules.Utility.Services;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class VerboseErrorCommands : NadekoSubmodule<VerboseErrorsService>
{
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
public async partial Task VerboseError(bool? newstate = null)
{
var state = _service.ToggleVerboseErrors(ctx.Guild.Id, newstate);
if (state)
await ReplyConfirmLocalizedAsync(strs.verbose_errors_enabled);
else
await ReplyConfirmLocalizedAsync(strs.verbose_errors_disabled);
}
}
}

View File

@@ -0,0 +1,69 @@
#nullable disable
using NadekoBot.Db;
using NadekoBot.Modules.Help.Services;
namespace NadekoBot.Modules.Utility.Services;
public class VerboseErrorsService : INService
{
private readonly ConcurrentHashSet<ulong> guildsEnabled;
private readonly DbService _db;
private readonly CommandHandler _ch;
private readonly HelpService _hs;
public VerboseErrorsService(
Bot bot,
DbService db,
CommandHandler ch,
HelpService hs)
{
_db = db;
_ch = ch;
_hs = hs;
_ch.CommandErrored += LogVerboseError;
guildsEnabled = 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 || !guildsEnabled.Contains(channel.GuildId))
return;
try
{
var embed = _hs.GetCommandHelp(cmd, channel.Guild)
.WithTitle("Command Error")
.WithDescription(reason)
.WithErrorColor();
await channel.EmbedAsync(embed);
}
catch
{
//ignore
}
}
public bool ToggleVerboseErrors(ulong guildId, bool? enabled = null)
{
using (var uow = _db.GetDbContext())
{
var gc = uow.GuildConfigsForId(guildId, set => set);
if (enabled == null)
enabled = gc.VerboseErrors = !gc.VerboseErrors; // Old behaviour, now behind a condition
else gc.VerboseErrors = (bool)enabled; // New behaviour, just set it.
uow.SaveChanges();
}
if ((bool)enabled) // This doesn't need to be duplicated inside the using block
guildsEnabled.Add(guildId);
else
guildsEnabled.TryRemove(guildId);
return (bool)enabled;
}
}