More defensive programming

pull/811/head
Tyrrrz 3 years ago
parent a06b448f4a
commit b8567d384f

@ -52,9 +52,14 @@ public partial record Channel
var name = var name =
// Guild channel // Guild channel
json.GetPropertyOrNull("name")?.GetStringOrNull() ?? json.GetPropertyOrNull("name")?.GetStringOrNull() ??
// DM channel // DM channel
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name) json.GetPropertyOrNull("recipients")?
.EnumerateArrayOrNull()?
.Select(User.Parse)
.Select(u => u.Name)
.Pipe(s => string.Join(", ", s)) ?? .Pipe(s => string.Join(", ", s)) ??
// Fallback // Fallback
id.ToString(); id.ToString();

@ -47,7 +47,7 @@ public partial record Embed
var footer = json.GetPropertyOrNull("footer")?.Pipe(EmbedFooter.Parse); var footer = json.GetPropertyOrNull("footer")?.Pipe(EmbedFooter.Parse);
var fields = var fields =
json.GetPropertyOrNull("fields")?.EnumerateArray().Select(EmbedField.Parse).ToArray() ?? json.GetPropertyOrNull("fields")?.EnumerateArrayOrNull()?.Select(EmbedField.Parse).ToArray() ??
Array.Empty<EmbedField>(); Array.Empty<EmbedField>();
return new Embed( return new Embed(

@ -53,19 +53,19 @@ public record Message(
}; };
var attachments = var attachments =
json.GetPropertyOrNull("attachments")?.EnumerateArray().Select(Attachment.Parse).ToArray() ?? json.GetPropertyOrNull("attachments")?.EnumerateArrayOrNull()?.Select(Attachment.Parse).ToArray() ??
Array.Empty<Attachment>(); Array.Empty<Attachment>();
var embeds = var embeds =
json.GetPropertyOrNull("embeds")?.EnumerateArray().Select(Embed.Parse).ToArray() ?? json.GetPropertyOrNull("embeds")?.EnumerateArrayOrNull()?.Select(Embed.Parse).ToArray() ??
Array.Empty<Embed>(); Array.Empty<Embed>();
var reactions = var reactions =
json.GetPropertyOrNull("reactions")?.EnumerateArray().Select(Reaction.Parse).ToArray() ?? json.GetPropertyOrNull("reactions")?.EnumerateArrayOrNull()?.Select(Reaction.Parse).ToArray() ??
Array.Empty<Reaction>(); Array.Empty<Reaction>();
var mentionedUsers = var mentionedUsers =
json.GetPropertyOrNull("mentions")?.EnumerateArray().Select(User.Parse).ToArray() ?? json.GetPropertyOrNull("mentions")?.EnumerateArrayOrNull()?.Select(User.Parse).ToArray() ??
Array.Empty<User>(); Array.Empty<User>();
return new Message( return new Message(

Loading…
Cancel
Save