Fixed a nullref message when the bot is loading medusae. Added support for multiple embeds in features which support custom embeds in the form of

{plainText:text-here, embeds: [embedObject, embedObject, embedObject]}
This commit is contained in:
Kwoth
2022-04-17 09:58:30 +02:00
parent 29d94640af
commit 18400dc53a
10 changed files with 104 additions and 23 deletions

View File

@@ -1,4 +1,5 @@
using Cloneable;
#nullable enable
using Cloneable;
using NadekoBot.Common.Yml;
namespace Nadeko.Medusa;
@@ -10,7 +11,7 @@ public sealed partial class MedusaConfig : ICloneable<MedusaConfig>
public int Version { get; set; } = 1;
[Comment("List of medusae automatically loaded at startup")]
public List<string> Loaded { get; set; }
public List<string>? Loaded { get; set; }
public MedusaConfig()
{

View File

@@ -18,7 +18,7 @@ public sealed class MedusaConfigService : ConfigServiceBase<MedusaConfig>, IMedu
}
public IReadOnlyCollection<string> GetLoadedMedusae()
=> Data.Loaded.ToList();
=> Data.Loaded?.ToList() ?? new List<string>();
public void AddLoadedMedusa(string name)
{
@@ -26,6 +26,9 @@ public sealed class MedusaConfigService : ConfigServiceBase<MedusaConfig>, IMedu
ModifyConfig(conf =>
{
if (conf.Loaded is null)
conf.Loaded = new();
if(!conf.Loaded.Contains(name))
conf.Loaded.Add(name);
});
@@ -37,6 +40,9 @@ public sealed class MedusaConfigService : ConfigServiceBase<MedusaConfig>, IMedu
ModifyConfig(conf =>
{
if (conf.Loaded is null)
conf.Loaded = new();
conf.Loaded.Remove(name);
});
}

View File

@@ -36,9 +36,17 @@ public class Replacer
{
SmartEmbedText embedData => Replace(embedData),
SmartPlainText plain => Replace(plain),
SmartEmbedTextArray arr => Replace(arr),
_ => throw new ArgumentOutOfRangeException(nameof(data), "Unsupported argument type")
};
public SmartEmbedTextArray Replace(SmartEmbedTextArray embedArr)
=> new()
{
Embeds = embedArr.Embeds.Map(Replace),
PlainText = Replace(embedArr.PlainText)
};
public SmartPlainText Replace(SmartPlainText plainText)
=> Replace(plainText.Text);

View File

@@ -0,0 +1,28 @@
#nullable disable
namespace NadekoBot;
public sealed record SmartEmbedTextArray : SmartText
{
public string PlainText { get; set; }
public SmartEmbedText[] Embeds { get; set; }
public bool IsValid
=> Embeds?.All(x => x.IsValid) ?? false;
public EmbedBuilder[] GetEmbedBuilders()
{
if (Embeds is null)
return Array.Empty<EmbedBuilder>();
return Embeds.Map(em => em.GetEmbed());
}
public void NormalizeFields()
{
if (Embeds is null)
return;
foreach(var eb in Embeds)
eb.NormalizeFields();
}
}

View File

@@ -1,5 +1,5 @@
#nullable disable
using Newtonsoft.Json;
using System.Text.Json;
namespace NadekoBot;
@@ -11,6 +11,15 @@ public abstract record SmartText
public bool IsPlainText
=> this is SmartPlainText;
public bool IsEmbedArray
=> this is SmartEmbedTextArray;
private static readonly JsonSerializerOptions _opts = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public static SmartText operator +(SmartText text, string input)
=> text switch
{
@@ -19,6 +28,10 @@ public abstract record SmartText
PlainText = set.PlainText + input
},
SmartPlainText spt => new SmartPlainText(spt.Text + input),
SmartEmbedTextArray arr => arr with
{
PlainText = arr.PlainText + input
},
_ => throw new ArgumentOutOfRangeException(nameof(text))
};
@@ -30,27 +43,46 @@ public abstract record SmartText
PlainText = input + set.PlainText
},
SmartPlainText spt => new SmartPlainText(input + spt.Text),
SmartEmbedTextArray arr => arr with
{
PlainText = input + arr.PlainText
},
_ => throw new ArgumentOutOfRangeException(nameof(text))
};
[CanBeNull]
public static SmartText CreateFrom(string input)
{
if (string.IsNullOrWhiteSpace(input) || !input.TrimStart().StartsWith("{"))
if (string.IsNullOrWhiteSpace(input))
return new SmartPlainText(input);
try
{
var smartEmbedText = JsonConvert.DeserializeObject<SmartEmbedText>(input);
var doc = JsonDocument.Parse(input);
var root = doc.RootElement;
if (root.ValueKind == JsonValueKind.Object)
{
if (root.TryGetProperty("embeds", out _))
{
var arr = root.Deserialize<SmartEmbedTextArray>(_opts);
if (smartEmbedText is null)
throw new FormatException();
if (arr is null)
return new SmartPlainText(input);
smartEmbedText.NormalizeFields();
arr!.NormalizeFields();
return arr;
}
if (!smartEmbedText.IsValid)
return new SmartPlainText(input);
var obj = root.Deserialize<SmartEmbedText>(_opts);
return smartEmbedText;
if (obj is null)
return new SmartPlainText(input);
obj.NormalizeFields();
return obj;
}
return new SmartPlainText(input);
}
catch
{