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

@@ -1,4 +1,7 @@
#nullable disable
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using NadekoBot.Modules.Utility.Services;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
@@ -32,6 +35,7 @@ public partial class Utility : NadekoModule
private readonly IBotCredentials _creds;
private readonly DownloadTracker _tracker;
private readonly IHttpClientFactory _httpFactory;
private readonly VerboseErrorsService _veService;
public Utility(
DiscordSocketClient client,
@@ -39,7 +43,8 @@ public partial class Utility : NadekoModule
IStatsService stats,
IBotCredentials creds,
DownloadTracker tracker,
IHttpClientFactory httpFactory)
IHttpClientFactory httpFactory,
VerboseErrorsService veService)
{
_client = client;
_coord = coord;
@@ -47,6 +52,7 @@ public partial class Utility : NadekoModule
_creds = creds;
_tracker = tracker;
_httpFactory = httpFactory;
_veService = veService;
}
[Cmd]
@@ -483,43 +489,16 @@ public partial class Utility : NadekoModule
}
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
public async partial Task VerboseError(bool? newstate = null)
{
var state = _veService.ToggleVerboseErrors(ctx.Guild.Id, newstate);
// [NadekoCommand, Usage, Description, Aliases]
// [RequireContext(ContextType.Guild)]
// public async Task CreateMyInvite(CreateInviteType type = CreateInviteType.Any)
// {
// if (type == CreateInviteType.Any)
// {
// if (_inviteService.TryGetInvite(type, out var code))
// {
// await ReplyErrorLocalizedAsync(strs.your_invite($"https://discord.gg/{code}"));
// return;
// }
// }
//
// var invite = await ((ITextChannel) ctx.Channel).CreateInviteAsync(isUnique: true);
// }
//
// [NadekoCommand, Usage, Description, Aliases]
// [RequireContext(ContextType.Guild)]
// public async partial Task InviteLb(int page = 1)
// {
// if (--page < 0)
// return;
//
// var inviteUsers = await _inviteService.GetInviteUsersAsync(ctx.Guild.Id);
//
// var embed = _eb.Create()
// .WithOkColor();
//
// await ctx.SendPaginatedConfirmAsync(page, (curPage) =>
// {
// var items = inviteUsers.Skip(curPage * 9).Take(9);
// var i = 0;
// foreach (var item in items)
// embed.AddField($"#{curPage * 9 + ++i} {item.UserName} [{item.User.Id}]", item.InvitedUsers);
//
// return embed;
// }, inviteUsers.Count, 9);
// }
if (state)
await ReplyConfirmLocalizedAsync(strs.verbose_errors_enabled);
else
await ReplyConfirmLocalizedAsync(strs.verbose_errors_disabled);
}
}

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

@@ -39,6 +39,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Html2Markdown" Version="5.0.2.561" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />

View File

@@ -1306,3 +1306,5 @@ patron:
patronmessage:
- patronmessage
- patronmsg
eval:
- eval

View File

@@ -2215,3 +2215,17 @@ patronmessage:
desc: "Sends a message to all patrons of the specified tier and higher. Supports embeds."
args:
- "x hello"
eval:
desc: |-
Execute arbitrary C# code and (optionally) return a result. Several namespaces are included by default.
Special variables available:
`self` - Instance of the command group executing the command (this)
`guild` - Server in which the command is executed
`channel` - Channel in which the command is executed
`user` - User executing the command
`ctx` - Discord.Net command context
`services` - Nadeko's IServiceProvider
args:
- "123 / 4.5f"
- "await ctx.OkAsync();"
- 'await ctx.SendConfirmAsync("uwu");'