Added SmartText and inheritors SmartPlainText and SmartEmbedText which will replace CREmbed in the future

This commit is contained in:
Kwoth
2021-07-10 23:31:12 +02:00
parent 5e4754fa40
commit 236c286278
7 changed files with 224 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
using Newtonsoft.Json;
namespace NadekoBot
{
// todo 3.3 check if saving embeds in db has IsEmbed field, to prevent rechecking and generating exceptions on every use
public abstract class SmartText
{
public bool IsEmbed => this is SmartEmbedText;
public bool IsPlainText => this is SmartPlainText;
public static SmartText CreateFrom(string input)
{
if (string.IsNullOrWhiteSpace(input) || !input.Trim().StartsWith("{"))
{
return new SmartPlainText(input);
}
try
{
var smartEmbedText = JsonConvert.DeserializeObject<SmartEmbedText>(input);
smartEmbedText.NormalizeFields();
if (!smartEmbedText.IsValid)
{
return new SmartPlainText(input);
}
return smartEmbedText;
}
catch
{
return new SmartPlainText(input);
}
}
}
}