Skip to content

Commit

Permalink
fix: embed fields throw if old embeds are received from discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Jul 5, 2024
1 parent 30673ab commit da5843f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 43 deletions.
21 changes: 21 additions & 0 deletions DisCatSharp/Entities/Embed/DiscordEmbedBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,27 @@ public DiscordEmbedBuilder AddField(DiscordEmbedField field)
if (this._fields.Count >= 25)
throw new InvalidOperationException("Cannot add more than 25 fields.");


if (string.IsNullOrWhiteSpace(field.Name))
{
ArgumentNullException.ThrowIfNull(field.Name);

throw new ArgumentException("Name cannot be empty or whitespace.", nameof(field));
}

if (field.Name.Length > 256)
throw new ArgumentException("Embed field name length cannot exceed 256 characters.", nameof(field));

if (string.IsNullOrWhiteSpace(field.Value))
{
ArgumentNullException.ThrowIfNull(field.Value);

throw new ArgumentException("Value cannot be empty or whitespace.", nameof(field));
}

if (field.Value.Length > 1024)
throw new ArgumentException("Embed field value length cannot exceed 1024 characters.", nameof(field));

this._fields.Add(field);

return this;
Expand Down
46 changes: 3 additions & 43 deletions DisCatSharp/Entities/Embed/DiscordEmbedField.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

using Newtonsoft.Json;

namespace DisCatSharp.Entities;
Expand All @@ -9,57 +7,19 @@ namespace DisCatSharp.Entities;
/// </summary>
public sealed class DiscordEmbedField : ObservableApiObject
{
private string _name;

/// <summary>
/// The name of the field.
/// Must be non-null, non-empty and &lt;= 256 characters.
/// </summary>
[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name
{
get => this._name;
set
{
if (string.IsNullOrWhiteSpace(value))
{
ArgumentNullException.ThrowIfNull(value);

throw new ArgumentException("Name cannot be empty or whitespace.", nameof(value));
}

if (value.Length > 256)
throw new ArgumentException("Embed field name length cannot exceed 256 characters.");

this._name = value;
}
}
public string Name { get; set; }

private string _value;

/// <summary>
/// /// <summary>
/// The value of the field.
/// Must be non-null, non-empty and &lt;= 1024 characters.
/// </summary>
[JsonProperty("value", NullValueHandling = NullValueHandling.Ignore)]
public string Value
{
get => this._value;
set
{
if (string.IsNullOrWhiteSpace(value))
{
ArgumentNullException.ThrowIfNull(value);

throw new ArgumentException("Value cannot be empty or whitespace.", nameof(value));
}

if (value.Length > 1024)
throw new ArgumentException("Embed field value length cannot exceed 1024 characters.");

this._value = value;
}
}
public string Value { get; set; }

/// <summary>
/// Whether or not this field should display inline.
Expand Down

1 comment on commit da5843f

@Yuki-42
Copy link

@Yuki-42 Yuki-42 commented on da5843f Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Please sign in to comment.