Files
nadekobot/src/NadekoBot/Common/SmartText/SmartText.cs
Kwoth ade880a6e6 - .gvc should now properly trigger when a user is already in a gvc and changes his activity
- .gvc should now properly detect multiple activities
- Rewrote repeat raw query bitshift to linqtodb query with division (thx kotz)
2022-01-03 06:52:41 +01:00

54 lines
1.5 KiB
C#

#nullable disable
using Newtonsoft.Json;
namespace NadekoBot;
public abstract record SmartText
{
public bool IsEmbed
=> this is SmartEmbedText;
public bool IsPlainText
=> this is SmartPlainText;
public static SmartText operator +(SmartText text, string input)
=> text switch
{
SmartEmbedText set => set with { PlainText = set.PlainText + input },
SmartPlainText spt => new SmartPlainText(spt.Text + input),
_ => throw new ArgumentOutOfRangeException(nameof(text))
};
public static SmartText operator +(string input, SmartText text)
=> text switch
{
SmartEmbedText set => set with { PlainText = input + set.PlainText },
SmartPlainText spt => new SmartPlainText(input + spt.Text),
_ => throw new ArgumentOutOfRangeException(nameof(text))
};
public static SmartText CreateFrom(string input)
{
if (string.IsNullOrWhiteSpace(input) || !input.TrimStart().StartsWith("{"))
return new SmartPlainText(input);
try
{
var smartEmbedText = JsonConvert.DeserializeObject<SmartEmbedText>(input);
if (smartEmbedText is null)
throw new();
smartEmbedText.NormalizeFields();
if (!smartEmbedText.IsValid)
return new SmartPlainText(input);
return smartEmbedText;
}
catch
{
return new SmartPlainText(input);
}
}
}