From 10b29cce8ac39e620ae00fd4fdcdfa9c03d8650a Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sat, 6 Nov 2021 16:52:49 +0100 Subject: [PATCH] Add function to convert Geekbot Embeds to Discord.Net Embeds for easier code sharing --- src/Core/Interactions/Embed/Embed.cs | 31 +++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Core/Interactions/Embed/Embed.cs b/src/Core/Interactions/Embed/Embed.cs index fd994a8..7974f54 100644 --- a/src/Core/Interactions/Embed/Embed.cs +++ b/src/Core/Interactions/Embed/Embed.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.Text.Json.Serialization; +using Discord; +using Color = System.Drawing.Color; namespace Geekbot.Core.Interactions.Embed { @@ -37,13 +38,16 @@ namespace Geekbot.Core.Interactions.Embed /// [JsonPropertyName("timestamp")] public DateTime? Timestamp { get; set; } + + [JsonIgnore] + private Color _color { get; set; } = System.Drawing.Color.Black; /// /// color code of the embed /// [JsonPropertyName("color")] - public uint Color { get; set; } - + public uint Color => (uint)(_color.R << 16 | _color.G << 8 | _color.B); + /// /// footer information /// @@ -84,7 +88,7 @@ namespace Geekbot.Core.Interactions.Embed /// fields information /// [JsonPropertyName("fields")] - public List Fields { get; set; } = new List(); + public List Fields { get; init; } = new List(); public void AddField(string name, string value, bool inline = false) { @@ -103,7 +107,24 @@ namespace Geekbot.Core.Interactions.Embed public void SetColor(Color c) { - Color = (uint)(c.R << 16 | c.G << 8 | c.B); + _color = c; + } + + public EmbedBuilder ToDiscordNetEmbed() + { + var eb = new EmbedBuilder(); + if (!string.IsNullOrEmpty(Title)) eb.Title = Title; + if (!string.IsNullOrEmpty(Description)) eb.Description = Description; + if (!string.IsNullOrEmpty(Url)) eb.Url = Url; + if (Timestamp != null) eb.Timestamp = Timestamp; + if (Color != 0) eb.WithColor(new Discord.Color(_color.R, _color.G, _color.B)); + if (Footer != null) eb.WithFooter(Footer.Text, Footer.IconUrl); + if (Image != null) eb.WithImageUrl(Image.Url); + if (Thumbnail != null) eb.WithThumbnailUrl(Thumbnail.Url); + if (Author != null) eb.WithAuthor(Author.Name, Author.IconUrl, Author.Url); + Fields.ForEach(field => eb.AddField(field.Name, field.Value, field.Inline)); + + return eb; } } } \ No newline at end of file