Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions DiscordLab.Bot/API/Features/Embed/EmbedAuthorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class EmbedAuthorBuilder
/// </summary>
public EmbedAuthorBuilder()
{
Base = new();
}

/// <summary>
Expand All @@ -22,42 +21,54 @@ public EmbedAuthorBuilder()
/// <param name="builder">The <see cref="Discord.EmbedAuthorBuilder"/> instance.</param>
public EmbedAuthorBuilder(Discord.EmbedAuthorBuilder builder)
{
Base = builder;
Name = builder.Name;
IconUrl = builder.IconUrl;
Url = builder.Url;
}

/// <summary>
/// Gets or sets the author name.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public string? Name
{
get => Base.Name;
set => Base.Name = value;
}
public string? Name { get; set; }

/// <summary>
/// Gets or sets the icon URL.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public string? IconUrl
{
get => Base.IconUrl;
set => Base.IconUrl = value;
}
public string? IconUrl { get; set; }

/// <summary>
/// Gets or sets the URL.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public string? Url
{
get => Base.Url;
set => Base.Url = value;
}
public string? Url { get; set; }

/// <summary>
/// Gets the base of this builder.
/// </summary>
[YamlIgnore]
public Discord.EmbedAuthorBuilder Base { get; init; }
[Obsolete("Please use the properties of the DiscordLab.Bot.API.Features.Embed.EmbedAuthorBuilder instead.")]
public Discord.EmbedAuthorBuilder Base => this;

/// <summary>
/// Changes a <see cref="EmbedAuthorBuilder"/> into a <see cref="Discord.EmbedAuthorBuilder"/> instance.
/// </summary>
/// <param name="builder">The <see cref="EmbedAuthorBuilder"/> instance.</param>
/// <returns>A copy of the <see cref="Discord.EmbedAuthorBuilder"/> instance.</returns>
public static implicit operator Discord.EmbedAuthorBuilder(EmbedAuthorBuilder builder)
{
Discord.EmbedAuthorBuilder copy = new();

if (builder.Name != null)
copy.WithName(builder.Name);

if (builder.Url != null)
copy.WithUrl(builder.Url);

if (builder.IconUrl != null)
copy.WithIconUrl(builder.IconUrl);

return copy;
}
}
119 changes: 44 additions & 75 deletions DiscordLab.Bot/API/Features/Embed/EmbedBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,147 +10,116 @@ public class EmbedBuilder
/// <summary>
/// Gets or sets the embed title.
/// </summary>
public string Title
{
get => Base.Title;
set => Base.Title = value;
}
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public string? Title { get; set; }

/// <summary>
/// Gets or sets the embed description.
/// </summary>
public string Description
{
get => Base.Description;
set => Base.Description = value;
}
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public string? Description { get; set; }

/// <summary>
/// Gets or sets the embed fields.
/// </summary>
public IEnumerable<EmbedFieldBuilder> Fields
{
get => Base.Fields.Select(x => new EmbedFieldBuilder(x));
set => Base.Fields = value.Select(x => x.Base).ToList();
}
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public IEnumerable<EmbedFieldBuilder>? Fields { get; set; }

/// <summary>
/// Gets or sets the color of the embed. In string so #, 0x or the raw hex value will work.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public string? Color
{
get => Base.Color?.ToString();
set
{
if (value == null)
{
Base.Color = null;
return;
}

Base.Color = Discord.Color.Parse(value);
}
}
public string? Color { get; set; }

/// <summary>
/// Gets or sets the thumbnail URL of the embed.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public string? ThumbnailUrl
{
get => Base.ThumbnailUrl;
set => Base.ThumbnailUrl = value;
}
public string? ThumbnailUrl { get; set; }

/// <summary>
/// Gets or sets the image URL of the embed.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public string? ImageUrl
{
get => Base.ImageUrl;
set => Base.ImageUrl = value;
}
public string? ImageUrl { get; set; }

/// <summary>
/// Gets or sets the URL of the embed.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public string? Url
{
get => Base.Url;
set => Base.Url = value;
}
public string? Url { get; set; }

/// <summary>
/// Gets or sets the footer of the embed.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public EmbedFooterBuilder? Footer
{
get => Base.Footer != null ? new(Base.Footer) : null;
set => Base.Footer = value?.Base;
}
public EmbedFooterBuilder? Footer { get; set; }

/// <summary>
/// Gets or sets the author of the embed.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
public EmbedAuthorBuilder? Author
{
get => Base.Author != null ? new(Base.Author) : null;
set => Base.Author = value?.Base;
}
public EmbedAuthorBuilder? Author { get; set; }

/// <summary>
/// Gets or sets a value indicating whether a timestamp will be added to the footer of this embed.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitDefaults)]
public bool Timestamp { get; set; }

[YamlIgnore]
private Discord.EmbedBuilder Base { get; } = new();

/// <summary>
/// Changes a <see cref="EmbedBuilder"/> into a <see cref="Discord.EmbedBuilder"/> instance.
/// </summary>
/// <param name="builder">The <see cref="EmbedBuilder"/> instance.</param>
/// <returns>A copy of the <see cref="Discord.EmbedBuilder"/> instance.</returns>
public static implicit operator Discord.EmbedBuilder(EmbedBuilder builder)
{
if (
string.IsNullOrEmpty(builder.Title)
&& string.IsNullOrEmpty(builder.Description)
&& string.IsNullOrEmpty(builder.ThumbnailUrl)
&& string.IsNullOrEmpty(builder.ImageUrl)
&& (builder.Author == null || string.IsNullOrEmpty(builder.Author.Name))
&& (builder.Fields == null || !builder.Fields.Any()))
{
throw new ArgumentNullException("An embed must contain at least on of the following: title, description, thumbnail, image, author (with a name) or at least 1 field.");
}

Discord.EmbedBuilder copy = new();

if (builder.Base.Title != null)
copy.WithTitle(builder.Base.Title);
if (builder.Title != null)
copy.WithTitle(builder.Title);

if (builder.Base.Description != null)
copy.WithDescription(builder.Base.Description);
if (builder.Description != null)
copy.WithDescription(builder.Description);

if (builder.Base.Color.HasValue)
copy.WithColor(builder.Base.Color.Value);
if (builder.Color != null)
copy.WithColor(Discord.Color.Parse(builder.Color));

if (builder.Base.Url != null)
copy.WithUrl(builder.Base.Url);
if (builder.Url != null)
copy.WithUrl(builder.Url);

if (builder.Base.ImageUrl != null)
copy.WithImageUrl(builder.Base.ImageUrl);
if (builder.ImageUrl != null)
copy.WithImageUrl(builder.ImageUrl);

if (builder.Base.ThumbnailUrl != null)
copy.WithThumbnailUrl(builder.Base.ThumbnailUrl);
if (builder.ThumbnailUrl != null)
copy.WithThumbnailUrl(builder.ThumbnailUrl);

if (builder.Timestamp)
copy.WithCurrentTimestamp();

if (builder.Base.Footer != null)
copy.WithFooter(builder.Base.Footer);
if (builder.Footer != null)
copy.WithFooter(builder.Footer);

if (builder.Base.Author != null)
copy.WithAuthor(builder.Base.Author);
if (builder.Author != null)
copy.WithAuthor(builder.Author);

foreach (Discord.EmbedFieldBuilder field in builder.Base.Fields)
if (builder.Fields != null)
{
copy.AddField(field.Name, field.Value, field.IsInline);
foreach (var field in builder.Fields)
{
copy.AddField(field);
}
}

return copy;
Expand Down
47 changes: 29 additions & 18 deletions DiscordLab.Bot/API/Features/Embed/EmbedFieldBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class EmbedFieldBuilder
/// </summary>
public EmbedFieldBuilder()
{
Base = new();
}

/// <summary>
Expand All @@ -22,40 +21,52 @@ public EmbedFieldBuilder()
/// <param name="builder">The <see cref="Discord.EmbedFieldBuilder"/> instance.</param>
public EmbedFieldBuilder(Discord.EmbedFieldBuilder builder)
{
Base = builder;
Name = builder.Name;
IsInline = builder.IsInline;

if (builder.Value is string value)
Value = value;
}

/// <summary>
/// Gets or sets the field name.
/// </summary>
public string Name
{
get => Base.Name;
set => Base.Name = value;
}
public string Name { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the field value.
/// </summary>
public string Value
{
get => Base.Value.ToString();
set => Base.Value = value;
}
public string? Value { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the field is inline.
/// </summary>
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitDefaults)]
public bool IsInline
{
get => Base.IsInline;
set => Base.IsInline = value;
}
public bool IsInline { get; set; }

/// <summary>
/// Gets the base builder.
/// </summary>
[YamlIgnore]
public Discord.EmbedFieldBuilder Base { get; init; }
[Obsolete("Please use the properties of the DiscordLab.Bot.API.Features.Embed.EmbedFieldBuilder instead.")]
public Discord.EmbedFieldBuilder Base => this;

/// <summary>
/// Changes a <see cref="EmbedFieldBuilder"/> into a <see cref="Discord.EmbedFieldBuilder"/> instance.
/// </summary>
/// <param name="builder">The <see cref="EmbedFieldBuilder"/> instance.</param>
/// <returns>A copy of the <see cref="Discord.EmbedFieldBuilder"/> instance.</returns>
public static implicit operator Discord.EmbedFieldBuilder(EmbedFieldBuilder builder)
{
Discord.EmbedFieldBuilder copy = new();

copy.WithName(builder.Name);

if (builder.Value != null)
copy.WithValue(builder.Value);

copy.WithIsInline(builder.IsInline);

return copy;
}
}
Loading