Skip to content

Commit

Permalink
Eval QoL changes
Browse files Browse the repository at this point in the history
  Ctx changed to Context
  Stack Trace is written to a file, that file is sent, then deleted; instead of putting the entire thing in an embed.

LoggingService changed to print the Stack Trace of an Exception rather than just the message.
  • Loading branch information
GreemDev committed Feb 8, 2019
1 parent 9634a0f commit 19047c4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
19 changes: 14 additions & 5 deletions Volte/Core/Commands/Modules/Owner/EvalCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Discord;
Expand Down Expand Up @@ -34,7 +35,7 @@ public async Task Eval([Remainder] string code) {
}

var objects = new EvalObjects {
Ctx = Context,
Context = Context,
CommandService = VolteBot.CommandService,
Config = Db.GetConfig(Context.Guild),
DatabaseService = Db,
Expand All @@ -50,30 +51,38 @@ public async Task Eval([Remainder] string code) {
sopts = sopts.WithImports(imports).WithReferences(AppDomain.CurrentDomain.GetAssemblies()
.Where(x => !x.IsDynamic && !string.IsNullOrWhiteSpace(x.Location)));

var msg = await embed.WithDescription("Evaluating...").SendTo(Context.Channel);
var msg = await embed.WithTitle("Evaluating...").SendTo(Context.Channel);
var sw = new Stopwatch();
sw.Start();
try {
res = await CSharpScript.EvaluateAsync(code, sopts, objects, typeof(EvalObjects));
sw.Stop();
if (res != null) {
await msg.DeleteAsync();
await embed.WithDescription("**Eval**")
await embed.WithTitle("Eval")
.AddField("Elapsed Time", $"{sw.ElapsedMilliseconds}ms")
.AddField("Input", Format.Code(code, "cs"))
.AddField("Output", Format.Code(code, "cs"))
.SendTo(Context.Channel);
}
else {
await msg.DeleteAsync();
await embed.WithDescription("**Eval**")
await embed.WithTitle("Eval")
.AddField("Elapsed Time", $"{sw.ElapsedMilliseconds}ms")
.AddField("Input", Format.Code(code, "cs"))
.AddField("Output", "No output.")
.SendTo(Context.Channel);
}
}
catch (Exception e) {
await msg.ModifyAsync(m =>
m.Embed = embed.WithDescription($"Error! {e.Message}\n```{e.StackTrace}```").Build());
m.Embed = embed
.WithDescription($"`{e.Message}`")
.WithTitle("Error")
.Build());
File.WriteAllText("data/EvalError.log", $"{e.Message}\n{e.StackTrace}");
await Context.Channel.SendFileAsync("data/EvalError.log");
File.Delete("data/EvalError.log");
}
finally {
GC.Collect();
Expand Down
2 changes: 1 addition & 1 deletion Volte/Core/Data/Objects/EvalObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Volte.Core.Data.Objects {
public class EvalObjects {
public VolteContext Ctx { get; set; }
public VolteContext Context { get; set; }
public Server Config { get; set; }
public LoggingService Logger { get; set; }
public CommandService CommandService { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion Volte/Core/Services/LoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public void DoLog(LogSeverity s, string src, string message, Exception e) {
Append(message, Color.White);

if (e != null)
Append(e.Message, Color.IndianRed);
Append($"{e.Message}\n{e.StackTrace}", Color.IndianRed);


Write(Environment.NewLine);
}
Expand Down

0 comments on commit 19047c4

Please sign in to comment.