diff --git a/ChaosBot/Discord/Services/CustomCommandHandler.cs b/ChaosBot/Discord/Services/CustomCommandHandler.cs index 50b77b4..b1e3bd3 100644 --- a/ChaosBot/Discord/Services/CustomCommandHandler.cs +++ b/ChaosBot/Discord/Services/CustomCommandHandler.cs @@ -34,8 +34,12 @@ namespace ChaosBot.Discord.Services } else if(customCommand.Type == CustomCommandType.CustomLua) { - if (!ProgrammingLanguageInterpreterFacade.TryInterpret(customCommand, out string errorReason)) - throw new Exception($"Could not execute code: {errorReason}"); + if (!ProgrammingLanguageInterpreterFacade.TryInterpret(context, customCommand, + out string errorReason)) + { + await context.Channel.SendMessageAsync(errorReason); + throw new Exception($"Could not execute code for command '{customCommand.Command}': {errorReason}"); + } } else if (customCommand.Type == CustomCommandType.Embed) { diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs index eca64a5..a9828fd 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs @@ -2,6 +2,6 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter { public interface IProgrammingLanguageInterpreter { - void Interpret(string content, string command); + string Interpret(string content, string command); } } diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs index a7e5363..8da885d 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs @@ -1,3 +1,5 @@ +using System; +using System.Text; using System.Text.RegularExpressions; using Neo.IronLua; using NLog; @@ -7,8 +9,9 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter internal class LuaProgrammingLanguageInterpreter : IProgrammingLanguageInterpreter { private static readonly ILogger Logger = Program.GetLogger(); + private readonly StringBuilder _outputBuilder = new StringBuilder(); - public void Interpret(string content, string command) + public string Interpret(string content, string command) { using (Lua lua = new Lua()) { @@ -17,11 +20,27 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter // This is a runtime type check dynamic env = lua.CreateEnvironment(); - foreach (string line in (new Regex("\n")).Split(content)) - { - env.dochunk(line, $"{command}.lua"); - } + ParamsDelegate printDel = Print; + env.print = printDel; + + env.dochunk(content, $"{command}.lua"); + + return _outputBuilder.ToString(); } } + + private delegate void ParamsDelegate(params object[] parameters); + + private void Print(params object[] parameters) + { + StringBuilder sb = new StringBuilder(); + + foreach (object parameter in parameters) + { + sb.Append(parameter); + } + + _outputBuilder.Append(sb); + } } } diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs index b327ebc..e48d28a 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs @@ -1,10 +1,12 @@ +using System; using ChaosBot.Models; +using Discord.Commands; namespace ChaosBot.Services.ProgrammingLanguageInterpreter { public static class ProgrammingLanguageInterpreterFacade { - public static bool TryInterpret(CustomCommand customCommand, out string errorReason) + public static bool TryInterpret(SocketCommandContext context, CustomCommand customCommand, out string errorReason) { errorReason = ""; IProgrammingLanguageInterpreter interpreter = @@ -14,9 +16,22 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter errorReason = "Could not set up an interpreter for the code"; return false; } - - interpreter.Interpret(customCommand.Content, customCommand.Command); - return true; + + try + { + string output = interpreter.Interpret(customCommand.Content, customCommand.Command); + + if (output.Length > 0) + context.Channel.SendMessageAsync(output); + else + context.Channel.SendMessageAsync($"Command '{customCommand.Command}' had no output."); + return true; + } + catch (Exception ex) + { + errorReason = $"There was an error with your code ({ex.GetType().Name}): {ex.Message}"; + return false; + } } } }