Added .eval command. Very dangerous, don't use.

This commit is contained in:
Kwoth
2022-06-23 13:19:41 +00:00
parent f1d9db699f
commit df85b3b250
8 changed files with 126 additions and 65 deletions

View File

@@ -0,0 +1,76 @@
#nullable disable
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class EvalCommands : NadekoModule
{
private readonly IServiceProvider _services;
public EvalCommands(IServiceProvider services)
{
_services = services;
}
[Cmd]
[NoPublicBot]
[OwnerOnly]
public async partial Task Eval([Leftover] string scriptText)
{
_ = ctx.Channel.TriggerTypingAsync();
if (scriptText.StartsWith("```cs"))
scriptText = scriptText[5..];
else if (scriptText.StartsWith("```"))
scriptText = scriptText[3..];
if (scriptText.EndsWith("```"))
scriptText = scriptText[..^3];
var script = CSharpScript.Create(scriptText,
ScriptOptions.Default
.WithReferences(this.GetType().Assembly)
.WithImports(
"System",
"NadekoBot",
"NadekoBot.Extensions",
"Microsoft.Extensions.DependencyInjection",
"NadekoBot.Common",
"System.Text",
"System.Text.Json"),
globalsType: typeof(EvalGlobals));
try
{
var result = await script.RunAsync(new EvalGlobals()
{
ctx = this.ctx,
guild = this.ctx.Guild,
channel = this.ctx.Channel,
user = this.ctx.User,
self = this,
services = _services
});
var output = result.ReturnValue?.ToString();
if (!string.IsNullOrWhiteSpace(output))
{
var eb = _eb.Create(ctx)
.WithOkColor()
.AddField("Code", scriptText)
.AddField("Output", output.TrimTo(512)!);
_ = ctx.Channel.EmbedAsync(eb);
}
}
catch (Exception ex)
{
await SendErrorAsync(ex.Message);
}
}
}
}

View File

@@ -0,0 +1,13 @@
// ReSharper disable InconsistentNaming
#nullable disable
namespace NadekoBot.Modules.Utility;
public class EvalGlobals
{
public ICommandContext ctx;
public Utility.EvalCommands self;
public IUser user;
public IMessageChannel channel;
public IGuild guild;
public IServiceProvider services;
}

View File

@@ -1,24 +0,0 @@
#nullable disable
using NadekoBot.Modules.Utility.Services;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class VerboseErrorCommands : NadekoModule<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

@@ -1,70 +0,0 @@
#nullable disable
using NadekoBot.Db;
using NadekoBot.Modules.Help.Services;
namespace NadekoBot.Modules.Utility.Services;
public class VerboseErrorsService : INService
{
private readonly ConcurrentHashSet<ulong> _guildsDisabled;
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;
_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;
}
}