Merge lua functionality #minor

This commit is contained in:
Daniel_I_Am 2020-09-23 20:51:06 +02:00
commit 6aaa877b0e
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
4 changed files with 50 additions and 12 deletions

View File

@ -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)
{

View File

@ -2,6 +2,6 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
{
public interface IProgrammingLanguageInterpreter
{
void Interpret(string content, string command);
string Interpret(string content, string command);
}
}

View File

@ -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);
}
}
}

View File

@ -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 =
@ -15,8 +17,21 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
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;
}
}
}
}