using DSharpPlus.Entities;
namespace X10D.DSharpPlus;
///
/// Extension methods for .
///
public static class DiscordEmbedBuilderExtensions
{
///
/// Adds a field of any value type to the embed.
///
/// The to modify.
/// The name of the embed field.
/// The value of the embed field.
/// to display this field inline; otherwise, .
/// The type of .
/// The current instance of ; that is, .
/// is .
public static DiscordEmbedBuilder AddField(
this DiscordEmbedBuilder builder,
string name,
T? value,
bool inline = false)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
#else
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
#endif
return builder.AddField(name, value?.ToString(), inline);
}
///
/// Conditionally adds a field to the embed.
///
/// The to modify.
/// The condition whose value is used to determine whether the field will be added.
/// The name of the embed field.
/// The value of the embed field.
/// to display this field inline; otherwise, .
/// The type of .
/// The current instance of ; that is, .
/// is .
public static DiscordEmbedBuilder AddFieldIf(
this DiscordEmbedBuilder builder,
bool condition,
string name,
T? value,
bool inline = false)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
#else
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
#endif
if (condition)
{
builder.AddField(name, value?.ToString(), inline);
}
return builder;
}
///
/// Conditionally adds a field to the embed.
///
/// The to modify.
/// The predicate whose return value is used to determine whether the field will be added.
/// The name of the embed field.
/// The value of the embed field.
/// to display this field inline; otherwise, .
/// The type of .
/// The current instance of ; that is, .
///
/// is .
/// -or-
/// is .
///
public static DiscordEmbedBuilder AddFieldIf(
this DiscordEmbedBuilder builder,
Func predicate,
string name,
T? value,
bool inline = false)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(predicate);
#else
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
#endif
if (predicate.Invoke())
{
builder.AddField(name, value?.ToString(), inline);
}
return builder;
}
///
/// Conditionally adds a field to the embed.
///
/// The to modify.
/// The predicate whose return value is used to determine whether the field will be added.
/// The name of the embed field.
/// The delegate whose return value will be used as the value of the embed field.
/// to display this field inline; otherwise, .
/// The return type of .
/// The current instance of ; that is, .
///
/// is .
/// -or-
/// is .
/// -or-
/// is .
///
public static DiscordEmbedBuilder AddFieldIf(
this DiscordEmbedBuilder builder,
Func predicate,
string name,
Func valueFactory,
bool inline = false)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(predicate);
ArgumentNullException.ThrowIfNull(valueFactory);
#else
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
if (valueFactory is null)
{
throw new ArgumentNullException(nameof(valueFactory));
}
#endif
if (predicate.Invoke())
{
builder.AddField(name, valueFactory.Invoke()?.ToString(), inline);
}
return builder;
}
///
/// Conditionally adds a field to the embed.
///
/// The to modify.
/// The condition whose value is used to determine whether the field will be added.
/// The name of the embed field.
/// The delegate whose return value will be used as the value of the embed field.
/// to display this field inline; otherwise, .
/// The return type of .
/// The current instance of ; that is, .
///
/// is .
/// -or-
/// is .
///
public static DiscordEmbedBuilder AddFieldIf(
this DiscordEmbedBuilder builder,
bool condition,
string name,
Func valueFactory,
bool inline = false)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(valueFactory);
#else
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (valueFactory is null)
{
throw new ArgumentNullException(nameof(valueFactory));
}
#endif
if (condition)
{
builder.AddField(name, valueFactory.Invoke()?.ToString(), inline);
}
return builder;
}
///
/// Sets the embed's author.
///
/// The embed builder to modify.
/// The author.
/// The current instance of .
public static DiscordEmbedBuilder WithAuthor(this DiscordEmbedBuilder builder, DiscordUser user)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(user);
#else
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (user is null)
{
throw new ArgumentNullException(nameof(user));
}
#endif
return builder.WithAuthor(user.GetUsernameWithDiscriminator(), iconUrl: user.AvatarUrl);
}
}