Restructured the project structure back to the way it was, there's no reasonable way to split the modules

This commit is contained in:
Kwoth
2024-04-26 22:26:24 +00:00
parent 6c9c8bf63e
commit e0819f760c
768 changed files with 192 additions and 1047 deletions

View File

@@ -0,0 +1,184 @@
#nullable disable warnings
using SixLabors.ImageSharp.PixelFormats;
using System.Text.Json.Serialization;
namespace NadekoBot;
public sealed record SmartEmbedArrayElementText : SmartEmbedTextBase
{
public string Color { get; init; } = string.Empty;
public SmartEmbedArrayElementText()
{
}
public SmartEmbedArrayElementText(IEmbed eb) : base(eb)
{
Color = eb.Color is { } c ? new Rgba32(c.R, c.G, c.B).ToHex() : string.Empty;
}
protected override EmbedBuilder GetEmbedInternal()
{
var embed = base.GetEmbedInternal();
if (Rgba32.TryParseHex(Color, out var color))
return embed.WithColor(color.ToDiscordColor());
return embed;
}
}
public sealed record SmartEmbedText : SmartEmbedTextBase
{
public string? PlainText { get; init; }
public uint Color { get; init; } = 7458112;
public SmartEmbedText()
{
}
private SmartEmbedText(IEmbed eb, string? plainText = null)
: base(eb)
=> (PlainText, Color) = (plainText, eb.Color?.RawValue ?? 0);
public static SmartEmbedText FromEmbed(IEmbed eb, string? plainText = null)
=> new(eb, plainText);
protected override EmbedBuilder GetEmbedInternal()
{
var embed = base.GetEmbedInternal();
return embed.WithColor(Color);
}
}
public abstract record SmartEmbedTextBase : SmartText
{
public string? Title { get; init; }
public string? Description { get; init; }
public string? Url { get; init; }
public string? Thumbnail { get; init; }
public string? Image { get; init; }
public SmartTextEmbedAuthor? Author { get; init; }
public SmartTextEmbedFooter? Footer { get; init; }
public SmartTextEmbedField[]? Fields { get; init; }
[JsonIgnore]
public bool IsValid
=> !string.IsNullOrWhiteSpace(Title)
|| !string.IsNullOrWhiteSpace(Description)
|| !string.IsNullOrWhiteSpace(Url)
|| !string.IsNullOrWhiteSpace(Thumbnail)
|| !string.IsNullOrWhiteSpace(Image)
|| (Footer is not null
&& (!string.IsNullOrWhiteSpace(Footer.Text) || !string.IsNullOrWhiteSpace(Footer.IconUrl)))
|| Fields is { Length: > 0 };
protected SmartEmbedTextBase()
{
}
protected SmartEmbedTextBase(IEmbed eb)
{
Title = eb.Title;
Description = eb.Description;
Url = eb.Url;
Thumbnail = eb.Thumbnail?.Url;
Image = eb.Image?.Url;
Author = eb.Author is { } ea
? new()
{
Name = ea.Name,
Url = ea.Url,
IconUrl = ea.IconUrl
}
: null;
Footer = eb.Footer is { } ef
? new()
{
Text = ef.Text,
IconUrl = ef.IconUrl
}
: null;
if (eb.Fields.Length > 0)
{
Fields = eb.Fields.Select(field
=> new SmartTextEmbedField
{
Inline = field.Inline,
Name = field.Name,
Value = field.Value
})
.ToArray();
}
}
public EmbedBuilder GetEmbed()
=> GetEmbedInternal();
protected virtual EmbedBuilder GetEmbedInternal()
{
var embed = new EmbedBuilder();
if (!string.IsNullOrWhiteSpace(Title))
embed.WithTitle(Title);
if (!string.IsNullOrWhiteSpace(Description))
embed.WithDescription(Description);
if (Url is not null && Uri.IsWellFormedUriString(Url, UriKind.Absolute))
embed.WithUrl(Url);
if (Footer is not null)
{
embed.WithFooter(efb =>
{
efb.WithText(Footer.Text);
if (Uri.IsWellFormedUriString(Footer.IconUrl, UriKind.Absolute))
efb.WithIconUrl(Footer.IconUrl);
});
}
if (Thumbnail is not null && Uri.IsWellFormedUriString(Thumbnail, UriKind.Absolute))
embed.WithThumbnailUrl(Thumbnail);
if (Image is not null && Uri.IsWellFormedUriString(Image, UriKind.Absolute))
embed.WithImageUrl(Image);
if (Author is not 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 is not 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 is { Length: > 0 })
{
foreach (var f in Fields)
{
f.Name = f.Name.TrimTo(256);
f.Value = f.Value.TrimTo(1024);
}
}
}
}

View File

@@ -0,0 +1,34 @@
#nullable disable
using System.Text.Json.Serialization;
namespace NadekoBot;
public sealed record SmartEmbedTextArray : SmartText
{
public string Content { get; set; }
public SmartEmbedArrayElementText[] Embeds { get; set; }
[JsonIgnore]
public bool IsValid
=> Embeds?.All(x => x.IsValid) ?? false;
public EmbedBuilder[] GetEmbedBuilders()
{
if (Embeds is null)
return Array.Empty<EmbedBuilder>();
return Embeds
.Where(x => x.IsValid)
.Select(em => em.GetEmbed())
.ToArray();
}
public void NormalizeFields()
{
if (Embeds is null)
return;
foreach(var eb in Embeds)
eb.NormalizeFields();
}
}

View File

@@ -0,0 +1,19 @@
#nullable disable
namespace NadekoBot;
public sealed record SmartPlainText : SmartText
{
public string Text { get; init; }
public SmartPlainText(string text)
=> Text = text;
public static implicit operator SmartPlainText(string input)
=> new(input);
public static implicit operator string(SmartPlainText input)
=> input.Text;
public override string ToString()
=> Text;
}

View File

@@ -0,0 +1,89 @@
#nullable disable
using Newtonsoft.Json.Linq;
using System.Text.Json.Serialization;
namespace NadekoBot;
public abstract record SmartText
{
[JsonIgnore]
public bool IsEmbed
=> this is SmartEmbedText;
[JsonIgnore]
public bool IsPlainText
=> this is SmartPlainText;
[JsonIgnore]
public bool IsEmbedArray
=> this is SmartEmbedTextArray;
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),
SmartEmbedTextArray arr => arr with
{
Content = arr.Content + 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),
SmartEmbedTextArray arr => arr with
{
Content = input + arr.Content
},
_ => throw new ArgumentOutOfRangeException(nameof(text))
};
public static SmartText CreateFrom(string input)
{
if (string.IsNullOrWhiteSpace(input))
return new SmartPlainText(input);
try
{
var doc = JObject.Parse(input);
var root = doc.Root;
if (root.Type == JTokenType.Object)
{
if (((JObject)root).TryGetValue("embeds", out _))
{
var arr = root.ToObject<SmartEmbedTextArray>();
if (arr is null)
return new SmartPlainText(input);
arr!.NormalizeFields();
return arr;
}
var obj = root.ToObject<SmartEmbedText>();
if (obj is null || !(obj.IsValid || !string.IsNullOrWhiteSpace(obj.PlainText)))
return new SmartPlainText(input);
obj.NormalizeFields();
return obj;
}
return new SmartPlainText(input);
}
catch
{
return new SmartPlainText(input);
}
}
}

View File

@@ -0,0 +1,16 @@
#nullable disable
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace NadekoBot;
public class SmartTextEmbedAuthor
{
public string Name { get; set; }
[JsonProperty("icon_url")]
[JsonPropertyName("icon_url")]
public string IconUrl { get; set; }
public string Url { get; set; }
}

View File

@@ -0,0 +1,9 @@
#nullable disable
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,14 @@
#nullable disable
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace NadekoBot;
public class SmartTextEmbedFooter
{
public string Text { get; set; }
[JsonProperty("icon_url")]
[JsonPropertyName("icon_url")]
public string IconUrl { get; set; }
}