chaosbot/ChaosBot/Services/CustomCommandEmbedBuilderFacade.cs

81 lines
3.4 KiB
C#

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