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,96 @@
using System;
using Discord;
using NadekoBot.Extensions;
using NadekoBot.Services;
namespace NadekoBot
{
public sealed class SmartEmbedText : SmartText
{
public string PlainText { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Url { get; set; }
public string Thumbnail { get; set; }
public string Image { get; set; }
public SmartTextEmbedAuthor Author { get; set; }
public SmartTextEmbedFooter Footer { get; set; }
public SmartTextEmbedField[] Fields { get; set; }
public uint Color { get; set; } = 7458112;
public bool IsValid =>
!string.IsNullOrWhiteSpace(Title) ||
!string.IsNullOrWhiteSpace(Description) ||
!string.IsNullOrWhiteSpace(Url) ||
!string.IsNullOrWhiteSpace(Thumbnail) ||
!string.IsNullOrWhiteSpace(Image) ||
(Footer != null && (!string.IsNullOrWhiteSpace(Footer.Text) || !string.IsNullOrWhiteSpace(Footer.IconUrl))) ||
(Fields != null && Fields.Length > 0);
public EmbedBuilder GetEmbed(IEmbedBuilderService eb)
{
var embed = new EmbedBuilder()
.WithColor(Color);
if (!string.IsNullOrWhiteSpace(Title))
embed.WithTitle(Title);
if (!string.IsNullOrWhiteSpace(Description))
embed.WithDescription(Description);
if (Url != null && Uri.IsWellFormedUriString(Url, UriKind.Absolute))
embed.WithUrl(Url);
if (Footer != null)
{
embed.WithFooter(efb =>
{
efb.WithText(Footer.Text);
if (Uri.IsWellFormedUriString(Footer.IconUrl, UriKind.Absolute))
efb.WithIconUrl(Footer.IconUrl);
});
}
if (Thumbnail != null && Uri.IsWellFormedUriString(Thumbnail, UriKind.Absolute))
embed.WithThumbnailUrl(Thumbnail);
if (Image != null && Uri.IsWellFormedUriString(Image, UriKind.Absolute))
embed.WithImageUrl(Image);
if (Author != null && !string.IsNullOrWhiteSpace(Author.Name))
{
if (!Uri.IsWellFormedUriString(Author.IconUrl, UriKind.Absolute))
Author.IconUrl = null;
if (!Uri.IsWellFormedUriString(Author.Url, UriKind.Absolute))
Author.Url = null;
embed.WithAuthor(Author.Name, Author.IconUrl, Author.Url);
}
if (Fields != null)
{
foreach (var f in Fields)
{
if (!string.IsNullOrWhiteSpace(f.Name) && !string.IsNullOrWhiteSpace(f.Value))
embed.AddField(f.Name, f.Value, f.Inline);
}
}
return embed;
}
public void NormalizeFields()
{
if (Fields != null && Fields.Length > 0)
{
foreach (var f in Fields)
{
f.Name = f.Name.TrimTo(256);
f.Value = f.Value.TrimTo(1024);
}
}
}
}
}

View File

@@ -0,0 +1,23 @@
namespace NadekoBot
{
public sealed class SmartPlainText : SmartText
{
public string Text { get; set; }
public SmartPlainText(string text)
{
Text = text;
}
public static implicit operator SmartPlainText(string input)
=> new SmartPlainText(input);
public static implicit operator string(SmartPlainText input)
=> input.Text;
public override string ToString()
{
return Text;
}
}
}

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

View File

@@ -0,0 +1,13 @@
using Newtonsoft.Json;
namespace NadekoBot
{
public class SmartTextEmbedAuthor
{
public string Name { get; set; }
public string IconUrl { get; set; }
[JsonProperty("icon_url")]
private string Icon_Url { set => IconUrl = value; }
public string Url { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace NadekoBot
{
public class SmartTextEmbedField
{
public string Name { get; set; }
public string Value { get; set; }
public bool Inline { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace NadekoBot
{
public class SmartTextEmbedFooter
{
public string Text { get; set; }
public string IconUrl { get; set; }
[JsonProperty("icon_url")]
private string Icon_Url { set => IconUrl = value; }
}
}

View File

@@ -185,5 +185,39 @@ namespace NadekoBot.Extensions
public static Task WarningAsync(this ICommandContext ctx)
=> ctx.Message.AddReactionAsync(new Emoji("⚠️"));
// todo fix extensions
// public static Task<IUserMessage> EditAsync(this IUserMessage message, SmartText text)
// {
// switch (text)
// {
// case SmartEmbedText set: return message.ModifyAsync(set.PlainText ?? "", set.GetEmbed());
// case SmartPlainText spt: return message.EditAsync(spt.Text);
// default:
// throw new ArgumentException(nameof(text));
// }
// }
//
// public static Task<IUserMessage> SendAsync(this IUser user, SmartText text)
// {
// switch (text)
// {
// case SmartEmbedText set: return user.SendAsync(set.PlainText ?? "", set.GetEmbed());
// case SmartPlainText spt: return user.SendAsync(spt.Text);
// default:
// throw new ArgumentException(nameof(text));
// }
// }
//
// public static Task<IUserMessage> SendAsync(this ITextChannel channel, SmartText text)
// {
// switch (text)
// {
// case SmartEmbedText set: return channel.SendAsync(set.PlainText ?? "", embed: set.GetEmbed());
// case SmartPlainText spt: return channel.SendAsync(spt.Text);
// default:
// throw new ArgumentException(nameof(text));
// }
// }
}
}