mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-03 16:24:27 -05:00 
			
		
		
		
	Added .eval command. Very dangerous, don't use.
This commit is contained in:
		@@ -1,4 +1,7 @@
 | 
				
			|||||||
#nullable disable
 | 
					#nullable disable
 | 
				
			||||||
 | 
					using Microsoft.CodeAnalysis.CSharp.Scripting;
 | 
				
			||||||
 | 
					using Microsoft.CodeAnalysis.Scripting;
 | 
				
			||||||
 | 
					using NadekoBot.Modules.Utility.Services;
 | 
				
			||||||
using Newtonsoft.Json;
 | 
					using Newtonsoft.Json;
 | 
				
			||||||
using System.Diagnostics;
 | 
					using System.Diagnostics;
 | 
				
			||||||
using System.Text;
 | 
					using System.Text;
 | 
				
			||||||
@@ -32,6 +35,7 @@ public partial class Utility : NadekoModule
 | 
				
			|||||||
    private readonly IBotCredentials _creds;
 | 
					    private readonly IBotCredentials _creds;
 | 
				
			||||||
    private readonly DownloadTracker _tracker;
 | 
					    private readonly DownloadTracker _tracker;
 | 
				
			||||||
    private readonly IHttpClientFactory _httpFactory;
 | 
					    private readonly IHttpClientFactory _httpFactory;
 | 
				
			||||||
 | 
					    private readonly VerboseErrorsService _veService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Utility(
 | 
					    public Utility(
 | 
				
			||||||
        DiscordSocketClient client,
 | 
					        DiscordSocketClient client,
 | 
				
			||||||
@@ -39,7 +43,8 @@ public partial class Utility : NadekoModule
 | 
				
			|||||||
        IStatsService stats,
 | 
					        IStatsService stats,
 | 
				
			||||||
        IBotCredentials creds,
 | 
					        IBotCredentials creds,
 | 
				
			||||||
        DownloadTracker tracker,
 | 
					        DownloadTracker tracker,
 | 
				
			||||||
        IHttpClientFactory httpFactory)
 | 
					        IHttpClientFactory httpFactory,
 | 
				
			||||||
 | 
					        VerboseErrorsService veService)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        _client = client;
 | 
					        _client = client;
 | 
				
			||||||
        _coord = coord;
 | 
					        _coord = coord;
 | 
				
			||||||
@@ -47,6 +52,7 @@ public partial class Utility : NadekoModule
 | 
				
			|||||||
        _creds = creds;
 | 
					        _creds = creds;
 | 
				
			||||||
        _tracker = tracker;
 | 
					        _tracker = tracker;
 | 
				
			||||||
        _httpFactory = httpFactory;
 | 
					        _httpFactory = httpFactory;
 | 
				
			||||||
 | 
					        _veService = veService;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    [Cmd]
 | 
					    [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]
 | 
					        if (state)
 | 
				
			||||||
    // [RequireContext(ContextType.Guild)]
 | 
					            await ReplyConfirmLocalizedAsync(strs.verbose_errors_enabled);
 | 
				
			||||||
    // public async Task CreateMyInvite(CreateInviteType type = CreateInviteType.Any)
 | 
					        else
 | 
				
			||||||
    // {
 | 
					            await ReplyConfirmLocalizedAsync(strs.verbose_errors_disabled);
 | 
				
			||||||
    //     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);
 | 
					 | 
				
			||||||
    // }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										76
									
								
								src/NadekoBot/Modules/Utility/VerboseErrors/EvalCommands.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								src/NadekoBot/Modules/Utility/VerboseErrors/EvalCommands.cs
									
									
									
									
									
										Normal 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);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										13
									
								
								src/NadekoBot/Modules/Utility/VerboseErrors/EvalGlobals.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/NadekoBot/Modules/Utility/VerboseErrors/EvalGlobals.cs
									
									
									
									
									
										Normal 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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -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);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -39,6 +39,7 @@
 | 
				
			|||||||
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 | 
					            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 | 
				
			||||||
        </PackageReference>
 | 
					        </PackageReference>
 | 
				
			||||||
        <PackageReference Include="Html2Markdown" Version="5.0.2.561" />
 | 
					        <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" Version="6.0.1" />
 | 
				
			||||||
        <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
 | 
					        <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1306,3 +1306,5 @@ patron:
 | 
				
			|||||||
patronmessage:
 | 
					patronmessage:
 | 
				
			||||||
  - patronmessage
 | 
					  - patronmessage
 | 
				
			||||||
  - patronmsg
 | 
					  - patronmsg
 | 
				
			||||||
 | 
					eval: 
 | 
				
			||||||
 | 
					  - eval
 | 
				
			||||||
@@ -2215,3 +2215,17 @@ patronmessage:
 | 
				
			|||||||
  desc: "Sends a message to all patrons of the specified tier and higher. Supports embeds."
 | 
					  desc: "Sends a message to all patrons of the specified tier and higher. Supports embeds."
 | 
				
			||||||
  args:
 | 
					  args:
 | 
				
			||||||
    - "x hello"
 | 
					    - "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");'
 | 
				
			||||||
		Reference in New Issue
	
	Block a user