Store my changes
This commit is contained in:
parent
6aaa877b0e
commit
514a835233
@ -192,7 +192,7 @@ namespace ChaosBot.Discord.Services
|
||||
{
|
||||
if (!command.IsSpecified)
|
||||
{
|
||||
LoggingFacade.Error($"Command execution failed [{context.User.Username}#{context.User.Discriminator} -> {command.Value.Name}]");
|
||||
LoggingFacade.Error($"Command execution failed [{context.User.Username}#{context.User.Discriminator} -> {context.Message.Content.Split(" ").First()}]");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,10 @@
|
||||
using Discord.Commands;
|
||||
|
||||
namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
||||
{
|
||||
public interface IProgrammingLanguageInterpreter
|
||||
{
|
||||
string Interpret(string content, string command);
|
||||
string Interpret(SocketCommandContext context, string content, string command);
|
||||
void StopExecution();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Discord.Commands;
|
||||
using Neo.IronLua;
|
||||
using NLog;
|
||||
|
||||
@ -10,37 +12,42 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
||||
{
|
||||
private static readonly ILogger Logger = Program.GetLogger();
|
||||
private readonly StringBuilder _outputBuilder = new StringBuilder();
|
||||
private Lua _lua;
|
||||
|
||||
public string Interpret(string content, string command)
|
||||
public string Interpret(SocketCommandContext context, string content, string command)
|
||||
{
|
||||
using (Lua lua = new Lua())
|
||||
using (_lua = new Lua())
|
||||
{
|
||||
// This needs to be dynamic if we want to call
|
||||
// functions from within the lua environment
|
||||
// This is a runtime type check
|
||||
dynamic env = lua.CreateEnvironment();
|
||||
dynamic env = _lua.CreateEnvironment();
|
||||
|
||||
ParamsDelegate printDel = Print;
|
||||
env.print = printDel;
|
||||
|
||||
env.dochunk(content, $"{command}.lua");
|
||||
env.context = context;
|
||||
|
||||
string code = content;
|
||||
|
||||
env.dochunk(code, $"{command}.lua");
|
||||
|
||||
return _outputBuilder.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public void StopExecution()
|
||||
{
|
||||
_lua.Dispose();
|
||||
}
|
||||
|
||||
private delegate void ParamsDelegate(params object[] parameters);
|
||||
|
||||
private void Print(params object[] parameters)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
string str = string.Join(" ", from param in parameters select param == null ? string.Empty : param.ToString());
|
||||
|
||||
foreach (object parameter in parameters)
|
||||
{
|
||||
sb.Append(parameter);
|
||||
}
|
||||
|
||||
_outputBuilder.Append(sb);
|
||||
_outputBuilder.Append(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ChaosBot.Models;
|
||||
using Discord.Commands;
|
||||
|
||||
@ -19,7 +21,25 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
||||
|
||||
try
|
||||
{
|
||||
string output = interpreter.Interpret(customCommand.Content, customCommand.Command);
|
||||
Thread taskThread = null;
|
||||
Task<string> task = Task.Run(() =>
|
||||
{
|
||||
taskThread = Thread.CurrentThread;
|
||||
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
|
||||
return interpreter.Interpret(context, customCommand.Content, customCommand.Command);
|
||||
});
|
||||
|
||||
const int timeout = 1000;
|
||||
bool isTaskCompleted = task.Wait(TimeSpan.FromMilliseconds(timeout));
|
||||
|
||||
if (!isTaskCompleted)
|
||||
{
|
||||
interpreter.StopExecution();
|
||||
taskThread.Abort();
|
||||
throw new TimeoutException($"Command '{customCommand.Command}' took more than {timeout}ms to run. It has been killed.");
|
||||
}
|
||||
|
||||
string output = task.Result;
|
||||
|
||||
if (output.Length > 0)
|
||||
context.Channel.SendMessageAsync(output);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user