mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-13 10:48:26 -04:00
Added SmartText and inheritors SmartPlainText and SmartEmbedText which will replace CREmbed in the future
This commit is contained in:
37
src/NadekoBot/Common/SmartText/SmartText.cs
Normal file
37
src/NadekoBot/Common/SmartText/SmartText.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user