Merge embed functionality #minor

This commit is contained in:
Daniel_I_Am 2020-09-22 22:06:05 +02:00
commit 8e94179101
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
4 changed files with 107 additions and 5 deletions

View File

@ -2,8 +2,11 @@ using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot.Models; using ChaosBot.Models;
using ChaosBot.Services;
using ChaosBot.Services.ProgrammingLanguageInterpreter; using ChaosBot.Services.ProgrammingLanguageInterpreter;
using Discord;
using Discord.Commands; using Discord.Commands;
using Neo.IronLua;
namespace ChaosBot.Discord.Services namespace ChaosBot.Discord.Services
{ {
@ -29,9 +32,22 @@ namespace ChaosBot.Discord.Services
{ {
await context.Channel.SendMessageAsync(customCommand.Content); await context.Channel.SendMessageAsync(customCommand.Content);
} }
else if(customCommand.Type == CustomCommandType.CustomLua)
{
if (!ProgrammingLanguageInterpreterFacade.TryInterpret(customCommand, out string errorReason))
throw new Exception($"Could not execute code: {errorReason}");
}
else if (customCommand.Type == CustomCommandType.Embed)
{
EmbedBuilder embedBuilder = new EmbedBuilder();
CustomCommandEmbedBuilderFacade.SetDetails(embedBuilder, customCommand.Content);
Embed embed = embedBuilder.Build();
await context.Channel.SendMessageAsync(embed: embed);
}
else else
{ {
if (!ProgrammingLanguageInterpreterFacade.TryInterpret(customCommand))
throw new NotImplementedException($"No support for command type ${customCommand.Type}"); throw new NotImplementedException($"No support for command type ${customCommand.Type}");
} }

View File

@ -23,6 +23,7 @@ namespace ChaosBot.Models
public enum CustomCommandType public enum CustomCommandType
{ {
Basic = 0, Basic = 0,
CustomLua = 1 CustomLua = 1,
Embed = 2
} }
} }

View File

@ -0,0 +1,80 @@
using System;
using System.Text;
using System.Text.RegularExpressions;
using ChaosBot.Models;
using Discord;
namespace ChaosBot.Services
{
public static class CustomCommandEmbedBuilderFacade
{
public static void SetDetails(EmbedBuilder embedBuilder, string customCommandContent)
{
StringBuilder descriptionBuilder = new StringBuilder();
foreach (string line in customCommandContent.Split("\n"))
{
string prefix = "#";
if (line.StartsWith(prefix))
{
// This may require special treatment
string remainder = line.Substring(prefix.Length);
string pattern = @"^<([a-z]+)>(.+)<\/([a-z]+)>$";
Match match = Regex.Match(remainder, pattern);
// Ensure that there is some sort of match and that opening and closing tags are equal
if (match.Value == string.Empty || match.Groups[1] == match.Groups[3])
{
DealWithNonSpecialLine(descriptionBuilder, line);
}
else
{
string tag = match.Groups[1].Value;
string data = match.Groups[2].Value;
switch (tag)
{
case "color":
string colorPattern = @"^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})";
Match colorMatch = Regex.Match(data, colorPattern);
if (colorMatch.Value == string.Empty)
{
DealWithNonSpecialLine(descriptionBuilder, line);
}
else
{
int r = int.Parse(colorMatch.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
int g = int.Parse(colorMatch.Groups[2].Value, System.Globalization.NumberStyles.HexNumber);
int b = int.Parse(colorMatch.Groups[3].Value, System.Globalization.NumberStyles.HexNumber);
embedBuilder.Color = new Color(r, g, b);
}
break;
case "image":
embedBuilder.ImageUrl = data;
break;
case "title":
embedBuilder.Title = data;
break;
default:
DealWithNonSpecialLine(descriptionBuilder, line);
break;
}
}
}
else
{
// There is definitely no special treatment
DealWithNonSpecialLine(descriptionBuilder, line);
}
}
embedBuilder.Description = descriptionBuilder.ToString();
}
private static void DealWithNonSpecialLine(StringBuilder descriptionBuilder, string line)
{
// Possible todo: escape mentions or something?
descriptionBuilder.AppendLine(line);
}
}
}

View File

@ -4,11 +4,16 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
{ {
public static class ProgrammingLanguageInterpreterFacade public static class ProgrammingLanguageInterpreterFacade
{ {
public static bool TryInterpret(CustomCommand customCommand) public static bool TryInterpret(CustomCommand customCommand, out string errorReason)
{ {
errorReason = "";
IProgrammingLanguageInterpreter interpreter = IProgrammingLanguageInterpreter interpreter =
ProgrammingLanguageInterpreterFactory.GetInterpreter(customCommand.Type); ProgrammingLanguageInterpreterFactory.GetInterpreter(customCommand.Type);
if (interpreter == null) return false; if (interpreter == null)
{
errorReason = "Could not set up an interpreter for the code";
return false;
}
interpreter.Interpret(customCommand.Content, customCommand.Command); interpreter.Interpret(customCommand.Content, customCommand.Command);
return true; return true;