mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-03 16:24:27 -05:00 
			
		
		
		
	Added SmartText and inheritors SmartPlainText and SmartEmbedText which will replace CREmbed in the future
This commit is contained in:
		
							
								
								
									
										96
									
								
								src/NadekoBot/Common/SmartText/SmartEmbedText.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								src/NadekoBot/Common/SmartText/SmartEmbedText.cs
									
									
									
									
									
										Normal 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);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								src/NadekoBot/Common/SmartText/SmartPlainText.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/NadekoBot/Common/SmartText/SmartPlainText.cs
									
									
									
									
									
										Normal 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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										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);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								src/NadekoBot/Common/SmartText/SmartTextEmbedAuthor.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/NadekoBot/Common/SmartText/SmartTextEmbedAuthor.cs
									
									
									
									
									
										Normal 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; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								src/NadekoBot/Common/SmartText/SmartTextEmbedField.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/NadekoBot/Common/SmartText/SmartTextEmbedField.cs
									
									
									
									
									
										Normal 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; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								src/NadekoBot/Common/SmartText/SmartTextEmbedFooter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/NadekoBot/Common/SmartText/SmartTextEmbedFooter.cs
									
									
									
									
									
										Normal 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; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user