Browse Source

Add parse & tryparse to embedbuilder.

pull/2284/head
Armano den Boef 3 years ago
parent
commit
e9e739b1f6
1 changed files with 40 additions and 0 deletions
  1. +40
    -0
      src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs

+ 40
- 0
src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Linq; using System.Linq;
using Discord.Utils; using Discord.Utils;
using Newtonsoft.Json;


namespace Discord namespace Discord
{ {
@@ -155,6 +156,45 @@ namespace Discord
} }
} }


/// <summary>
/// Tries to parse a string into an <see cref="EmbedBuilder"/>.
/// </summary>
/// <param name="json">The json string to parse.</param>
/// <param name="builder">The <see cref="EmbedBuilder"/> with populated values. An empty instance if method returns <see langword="false"/>.</param>
/// <returns><see langword="true"/> if <paramref name="json"/> was succesfully parsed. <see langword="false"/> if not.</returns>
public static bool TryParse(string json, out EmbedBuilder builder)
{
var model = JsonConvert.DeserializeObject<Embed>(json);

builder = new EmbedBuilder();

if (model is not null)
{
builder = model.ToEmbedBuilder();
return true;
}

else
return false;
}

/// <summary>
/// Parses a string into an <see cref="EmbedBuilder"/>.
/// </summary>
/// <param name="json">The json string to parse.</param>
/// <returns>An <see cref="EmbedBuilder"/> with populated values from the passed <paramref name="json"/></returns>
/// <exception cref="InvalidOperationException">Thrown if the string passed is not valid json.</exception>
public static EmbedBuilder Parse(string json)
{
var model = JsonConvert.DeserializeObject<Embed>(json);

if (model is not null)
return model.ToEmbedBuilder();

else
throw new JsonSerializationException("The passed json string was not able to be parsed to an embed.");
}

/// <summary> /// <summary>
/// Sets the title of an <see cref="Embed"/>. /// Sets the title of an <see cref="Embed"/>.
/// </summary> /// </summary>


Loading…
Cancel
Save