Fixed .showembed, closes #410

This commit is contained in:
Kwoth
2023-05-03 02:23:10 +02:00
parent e8706d4006
commit 4dd31d6a0b
4 changed files with 21 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
#nullable disable warnings #nullable disable warnings
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using System.Text.Json.Serialization;
namespace NadekoBot; namespace NadekoBot;
@@ -7,14 +8,14 @@ public sealed record SmartEmbedArrayElementText : SmartEmbedTextBase
{ {
public string Color { get; init; } = string.Empty; public string Color { get; init; } = string.Empty;
public SmartEmbedArrayElementText() : base() public SmartEmbedArrayElementText()
{ {
} }
public SmartEmbedArrayElementText(IEmbed eb) : base(eb) 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() protected override EmbedBuilder GetEmbedInternal()
@@ -63,6 +64,7 @@ public abstract record SmartEmbedTextBase : SmartText
public SmartTextEmbedFooter Footer { get; init; } public SmartTextEmbedFooter Footer { get; init; }
public SmartTextEmbedField[] Fields { get; init; } public SmartTextEmbedField[] Fields { get; init; }
[JsonIgnore]
public bool IsValid public bool IsValid
=> !string.IsNullOrWhiteSpace(Title) => !string.IsNullOrWhiteSpace(Title)
|| !string.IsNullOrWhiteSpace(Description) || !string.IsNullOrWhiteSpace(Description)
@@ -100,7 +102,7 @@ public abstract record SmartEmbedTextBase : SmartText
IconUrl = ef.IconUrl IconUrl = ef.IconUrl
} }
: null; : null;
if (eb.Fields.Length > 0) if (eb.Fields.Length > 0)
{ {
Fields = eb.Fields.Select(field Fields = eb.Fields.Select(field

View File

@@ -1,4 +1,6 @@
#nullable disable #nullable disable
using System.Text.Json.Serialization;
namespace NadekoBot; namespace NadekoBot;
public sealed record SmartEmbedTextArray : SmartText public sealed record SmartEmbedTextArray : SmartText
@@ -6,6 +8,7 @@ public sealed record SmartEmbedTextArray : SmartText
public string Content { get; set; } public string Content { get; set; }
public SmartEmbedArrayElementText[] Embeds { get; set; } public SmartEmbedArrayElementText[] Embeds { get; set; }
[JsonIgnore]
public bool IsValid public bool IsValid
=> Embeds?.All(x => x.IsValid) ?? false; => Embeds?.All(x => x.IsValid) ?? false;

View File

@@ -1,16 +1,20 @@
#nullable disable #nullable disable
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.Text.Json.Serialization;
namespace NadekoBot; namespace NadekoBot;
public abstract record SmartText public abstract record SmartText
{ {
[JsonIgnore]
public bool IsEmbed public bool IsEmbed
=> this is SmartEmbedText; => this is SmartEmbedText;
[JsonIgnore]
public bool IsPlainText public bool IsPlainText
=> this is SmartPlainText; => this is SmartPlainText;
[JsonIgnore]
public bool IsEmbedArray public bool IsEmbedArray
=> this is SmartEmbedTextArray; => this is SmartEmbedTextArray;

View File

@@ -550,15 +550,20 @@ public partial class Utility : NadekoModule
return; return;
} }
var embed = msg.Embeds.FirstOrDefault(); if (!msg.Embeds.Any())
if (embed is null)
{ {
await ReplyErrorLocalizedAsync(strs.not_found); await ReplyErrorLocalizedAsync(strs.not_found);
return; return;
} }
var json = SmartEmbedText.FromEmbed(embed, msg.Content).ToJson(_showEmbedSerializerOptions); var json = new SmartEmbedTextArray()
await SendConfirmAsync(Format.Sanitize(json).Replace("](", "]\\(")); {
Content = msg.Content,
Embeds = msg.Embeds
.Map(x => new SmartEmbedArrayElementText(x))
}.ToJson(_showEmbedSerializerOptions);
await SendConfirmAsync(Format.Code(json, "json").Replace("](", "]\\("));
} }
[Cmd] [Cmd]