Browse Source

Add various property validation in EmbedBuilder

pull/711/head
moiph 8 years ago
parent
commit
bb2c587ac4
1 changed files with 81 additions and 11 deletions
  1. +81
    -11
      src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs

+ 81
- 11
src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs View File

@@ -8,14 +8,36 @@ namespace Discord
{
private readonly Embed _embed;

public const int MaxFieldCount = 25;
public const int MaxTitleLength = 256;
public const int MaxDescriptionLength = 2048;

public EmbedBuilder()
{
_embed = new Embed("rich");
Fields = new List<EmbedFieldBuilder>();
}

public string Title { get { return _embed.Title; } set { _embed.Title = value; } }
public string Description { get { return _embed.Description; } set { _embed.Description = value; } }
public string Title
{
get => _embed.Title;
set
{
if (value?.Length > MaxTitleLength) throw new ArgumentException($"Title length must be less than or equal to {MaxTitleLength}.");
_embed.Title = value;
}
}

public string Description
{
get => _embed.Description;
set
{
if (value?.Length > MaxDescriptionLength) throw new ArgumentException($"Description length must be less than or equal to {MaxDescriptionLength}.");
_embed.Description = value;
}
}

public string Url { get { return _embed.Url; } set { _embed.Url = value; } }
public string ThumbnailUrl { get { return _embed.Thumbnail?.Url; } set { _embed.Thumbnail = new EmbedThumbnail(value, null, null, null); } }
public string ImageUrl { get { return _embed.Image?.Url; } set { _embed.Image = new EmbedImage(value, null, null, null); } }
@@ -30,8 +52,10 @@ namespace Discord
get => _fields;
set
{
if (value != null) _fields = value;
else throw new ArgumentNullException("Cannot set an embed builder's fields collection to null", nameof(value));

if (value == null) throw new ArgumentNullException("Cannot set an embed builder's fields collection to null", nameof(value));
if (value.Count > MaxFieldCount) throw new ArgumentException($"Field count must be less than or equal to {MaxFieldCount}.");
_fields = value;
}
}

@@ -107,7 +131,7 @@ namespace Discord
.WithIsInline(false)
.WithName(name)
.WithValue(value);
Fields.Add(field);
AddField(field);
return this;
}
public EmbedBuilder AddInlineField(string name, object value)
@@ -116,11 +140,16 @@ namespace Discord
.WithIsInline(true)
.WithName(name)
.WithValue(value);
Fields.Add(field);
AddField(field);
return this;
}
public EmbedBuilder AddField(EmbedFieldBuilder field)
{
if (Fields.Count >= MaxFieldCount)
{
throw new ArgumentException($"Field count must be less than or equal to {MaxFieldCount}.");
}

Fields.Add(field);
return this;
}
@@ -128,7 +157,7 @@ namespace Discord
{
var field = new EmbedFieldBuilder();
action(field);
Fields.Add(field);
this.AddField(field);
return this;
}

@@ -149,8 +178,29 @@ namespace Discord
{
private EmbedField _field;

public string Name { get { return _field.Name; } set { _field.Name = value; } }
public object Value { get { return _field.Value; } set { _field.Value = value.ToString(); } }
public const int MaxFieldNameLength = 256;
public const int MaxFieldValueLength = 1024;

public string Name
{
get => _field.Name;
set
{
if (value?.Length > MaxFieldNameLength) throw new ArgumentException($"Field name length must be less than or equal to {MaxFieldNameLength}.");
_field.Name = value;
}
}

public object Value
{
get => _field.Value;
set
{
var stringValue = value.ToString();
if (stringValue.Length > MaxFieldValueLength) throw new ArgumentException($"Field value length must be less than or equal to {MaxFieldValueLength}.");
_field.Value = stringValue;
}
}
public bool IsInline { get { return _field.Inline; } set { _field.Inline = value; } }

public EmbedFieldBuilder()
@@ -182,7 +232,17 @@ namespace Discord
{
private EmbedAuthor _author;

public string Name { get { return _author.Name; } set { _author.Name = value; } }
public const int MaxAuthorNameLength = 256;

public string Name
{
get => _author.Name;
set
{
if (value?.Length > MaxAuthorNameLength) throw new ArgumentException($"Author name length must be less than or equal to {MaxAuthorNameLength}.");
_author.Name = value;
}
}
public string Url { get { return _author.Url; } set { _author.Url = value; } }
public string IconUrl { get { return _author.IconUrl; } set { _author.IconUrl = value; } }

@@ -215,7 +275,17 @@ namespace Discord
{
private EmbedFooter _footer;

public string Text { get { return _footer.Text; } set { _footer.Text = value; } }
public const int MaxFooterTextLength = 2048;

public string Text
{
get => _footer.Text;
set
{
if (value?.Length > MaxFooterTextLength) throw new ArgumentException($"Footer text length must be less than or equal to {MaxFooterTextLength}.");
_footer.Text = value;
}
}
public string IconUrl { get { return _footer.IconUrl; } set { _footer.IconUrl = value; } }

public EmbedFooterBuilder()


Loading…
Cancel
Save