Merge lua functionality #minor
This commit is contained in:
commit
6aaa877b0e
@ -34,8 +34,12 @@ namespace ChaosBot.Discord.Services
|
|||||||
}
|
}
|
||||||
else if(customCommand.Type == CustomCommandType.CustomLua)
|
else if(customCommand.Type == CustomCommandType.CustomLua)
|
||||||
{
|
{
|
||||||
if (!ProgrammingLanguageInterpreterFacade.TryInterpret(customCommand, out string errorReason))
|
if (!ProgrammingLanguageInterpreterFacade.TryInterpret(context, customCommand,
|
||||||
throw new Exception($"Could not execute code: {errorReason}");
|
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)
|
else if (customCommand.Type == CustomCommandType.Embed)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -2,6 +2,6 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
|||||||
{
|
{
|
||||||
public interface IProgrammingLanguageInterpreter
|
public interface IProgrammingLanguageInterpreter
|
||||||
{
|
{
|
||||||
void Interpret(string content, string command);
|
string Interpret(string content, string command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Neo.IronLua;
|
using Neo.IronLua;
|
||||||
using NLog;
|
using NLog;
|
||||||
@ -7,8 +9,9 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
|||||||
internal class LuaProgrammingLanguageInterpreter : IProgrammingLanguageInterpreter
|
internal class LuaProgrammingLanguageInterpreter : IProgrammingLanguageInterpreter
|
||||||
{
|
{
|
||||||
private static readonly ILogger Logger = Program.GetLogger();
|
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())
|
using (Lua lua = new Lua())
|
||||||
{
|
{
|
||||||
@ -17,11 +20,27 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
|||||||
// This is a runtime type check
|
// This is a runtime type check
|
||||||
dynamic env = lua.CreateEnvironment();
|
dynamic env = lua.CreateEnvironment();
|
||||||
|
|
||||||
foreach (string line in (new Regex("\n")).Split(content))
|
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)
|
||||||
{
|
{
|
||||||
env.dochunk(line, $"{command}.lua");
|
StringBuilder sb = new StringBuilder();
|
||||||
}
|
|
||||||
|
foreach (object parameter in parameters)
|
||||||
|
{
|
||||||
|
sb.Append(parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_outputBuilder.Append(sb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
|
using System;
|
||||||
using ChaosBot.Models;
|
using ChaosBot.Models;
|
||||||
|
using Discord.Commands;
|
||||||
|
|
||||||
namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
||||||
{
|
{
|
||||||
public static class ProgrammingLanguageInterpreterFacade
|
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 = "";
|
errorReason = "";
|
||||||
IProgrammingLanguageInterpreter interpreter =
|
IProgrammingLanguageInterpreter interpreter =
|
||||||
@ -15,8 +17,21 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
interpreter.Interpret(customCommand.Content, customCommand.Command);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
errorReason = $"There was an error with your code ({ex.GetType().Name}): {ex.Message}";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user