chaosbot/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs

38 lines
1.3 KiB
C#

using System;
using ChaosBot.Models;
using Discord.Commands;
namespace ChaosBot.Services.ProgrammingLanguageInterpreter
{
public static class ProgrammingLanguageInterpreterFacade
{
public static bool TryInterpret(SocketCommandContext context, CustomCommand customCommand, out string errorReason)
{
errorReason = "";
IProgrammingLanguageInterpreter interpreter =
ProgrammingLanguageInterpreterFactory.GetInterpreter(customCommand.Type);
if (interpreter == null)
{
errorReason = "Could not set up an interpreter for the code";
return false;
}
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;
}
}
}
}