Browse Source

Adds overall embed length check

pull/711/head
moiph 8 years ago
parent
commit
ec42cef2fc
2 changed files with 10 additions and 0 deletions
  1. +3
    -0
      src/Discord.Net.Core/Entities/Messages/Embed.cs
  2. +7
    -0
      src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs

+ 3
- 0
src/Discord.Net.Core/Entities/Messages/Embed.cs View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;


namespace Discord namespace Discord
{ {
@@ -56,6 +57,8 @@ namespace Discord
Fields = fields; Fields = fields;
} }


public int Length => Title?.Length + Author?.Name?.Length + Description?.Length + Footer?.Text?.Length + Fields.Sum(f => f.Name.Length + f.Value.ToString().Length) ?? 0;

public override string ToString() => Title; public override string ToString() => Title;
private string DebuggerDisplay => $"{Title} ({Type})"; private string DebuggerDisplay => $"{Title} ({Type})";
} }


+ 7
- 0
src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs View File

@@ -11,6 +11,7 @@ namespace Discord
public const int MaxFieldCount = 25; public const int MaxFieldCount = 25;
public const int MaxTitleLength = 256; public const int MaxTitleLength = 256;
public const int MaxDescriptionLength = 2048; public const int MaxDescriptionLength = 2048;
public const int MaxEmbedLength = 6000; // user bot limit is 2000, but we don't validate that here.


public EmbedBuilder() public EmbedBuilder()
{ {
@@ -169,6 +170,12 @@ namespace Discord
for (int i = 0; i < Fields.Count; i++) for (int i = 0; i < Fields.Count; i++)
fields.Add(Fields[i].Build()); fields.Add(Fields[i].Build());
_embed.Fields = fields.ToImmutable(); _embed.Fields = fields.ToImmutable();

if (_embed.Length > MaxEmbedLength)
{
throw new InvalidOperationException($"Total embed length must be less than or equal to {MaxEmbedLength}");
}

return _embed; return _embed;
} }
public static implicit operator Embed(EmbedBuilder builder) => builder?.Build(); public static implicit operator Embed(EmbedBuilder builder) => builder?.Build();


Loading…
Cancel
Save