Skip to content

Commit

Permalink
minor changes to eval
Browse files Browse the repository at this point in the history
- "code-quick-shortcut" interaction event: delete ephemeral follow-up message if eval returns null
- put eval imports in one place and reference from both "code-quick-shortcut" & /eval
  • Loading branch information
FloatingMilkshake committed Apr 9, 2024
1 parent e7308b5 commit 8c30f7b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
9 changes: 5 additions & 4 deletions Commands/Owner/EvalCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
public class EvalCommands : ApplicationCommandModule
{
private static readonly List<string> RestrictedTerms = ["poweroff", "shutdown", "reboot", "halt"];
public static readonly string[] EvalImports = ["System", "System.Collections.Generic", "System.Linq",
"System.Text", "System.Threading.Tasks", "DSharpPlus", "DSharpPlus.SlashCommands",
"DSharpPlus.Interactivity", "DSharpPlus.Entities", "Microsoft.Extensions.Logging",
Assembly.GetExecutingAssembly().GetName().Name];

// The idea for this command, and a lot of the code, is taken from Erisa's Lykos. References are linked below.
// https://github.com/Erisa/Lykos/blob/5f9c17c/src/Modules/Owner.cs#L116-L144
Expand Down Expand Up @@ -113,10 +117,7 @@ await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWith
Globals globals = new(Program.Discord, ctx);

var scriptOptions = ScriptOptions.Default;
scriptOptions = scriptOptions.WithImports("System", "System.Collections.Generic", "System.Linq",
"System.Text", "System.Threading.Tasks", "DSharpPlus", "DSharpPlus.SlashCommands",
"DSharpPlus.Interactivity", "DSharpPlus.Entities", "Microsoft.Extensions.Logging",
Assembly.GetExecutingAssembly().GetName().Name);
scriptOptions = scriptOptions.WithImports(EvalImports);
scriptOptions = scriptOptions.WithReferences(AppDomain.CurrentDomain.GetAssemblies()
.Where(xa => !xa.IsDynamic && !string.IsNullOrWhiteSpace(xa.Location)));

Expand Down
9 changes: 3 additions & 6 deletions Events/ComponentInteractionEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,7 @@ await e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredChannelM
EventGlobals globals = new(Program.Discord, ctx);

var scriptOptions = ScriptOptions.Default;
scriptOptions = scriptOptions.WithImports("System", "System.Collections.Generic", "System.Linq",
"System.Text", "System.Threading.Tasks", "DSharpPlus", "DSharpPlus.SlashCommands",
"DSharpPlus.Interactivity", "Microsoft.Extensions.Logging");
scriptOptions = scriptOptions.WithImports(EvalCommands.EvalImports);
scriptOptions = scriptOptions.WithReferences(AppDomain.CurrentDomain.GetAssemblies()
.Where(xa => !xa.IsDynamic && !string.IsNullOrWhiteSpace(xa.Location)));

Expand All @@ -257,14 +255,13 @@ await e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredChannelM

if (result is null)
{
await ctx.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().WithContent("null"));
await ctx.DeleteOriginalResponseAsync();
}
else
{
if (result.ReturnValue is null)
{
await ctx.CreateFollowupMessageAsync(
new DiscordFollowupMessageBuilder().WithContent("null"));
await ctx.DeleteOriginalResponseAsync();
}
else
{
Expand Down

0 comments on commit 8c30f7b

Please sign in to comment.