Skip to content

Commit d8c438b

Browse files
authored
feat: better performance in embed configs (#20)
1 parent 311a642 commit d8c438b

4 files changed

Lines changed: 126 additions & 124 deletions

File tree

DiscordLab.Bot/API/Features/Embed/EmbedAuthorBuilder.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public class EmbedAuthorBuilder
1212
/// </summary>
1313
public EmbedAuthorBuilder()
1414
{
15-
Base = new();
1615
}
1716

1817
/// <summary>
@@ -22,42 +21,54 @@ public EmbedAuthorBuilder()
2221
/// <param name="builder">The <see cref="Discord.EmbedAuthorBuilder"/> instance.</param>
2322
public EmbedAuthorBuilder(Discord.EmbedAuthorBuilder builder)
2423
{
25-
Base = builder;
24+
Name = builder.Name;
25+
IconUrl = builder.IconUrl;
26+
Url = builder.Url;
2627
}
2728

2829
/// <summary>
2930
/// Gets or sets the author name.
3031
/// </summary>
3132
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
32-
public string? Name
33-
{
34-
get => Base.Name;
35-
set => Base.Name = value;
36-
}
33+
public string? Name { get; set; }
3734

3835
/// <summary>
3936
/// Gets or sets the icon URL.
4037
/// </summary>
4138
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
42-
public string? IconUrl
43-
{
44-
get => Base.IconUrl;
45-
set => Base.IconUrl = value;
46-
}
39+
public string? IconUrl { get; set; }
4740

4841
/// <summary>
4942
/// Gets or sets the URL.
5043
/// </summary>
5144
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
52-
public string? Url
53-
{
54-
get => Base.Url;
55-
set => Base.Url = value;
56-
}
45+
public string? Url { get; set; }
5746

5847
/// <summary>
5948
/// Gets the base of this builder.
6049
/// </summary>
6150
[YamlIgnore]
62-
public Discord.EmbedAuthorBuilder Base { get; init; }
51+
[Obsolete("Please use the properties of the DiscordLab.Bot.API.Features.Embed.EmbedAuthorBuilder instead.")]
52+
public Discord.EmbedAuthorBuilder Base => this;
53+
54+
/// <summary>
55+
/// Changes a <see cref="EmbedAuthorBuilder"/> into a <see cref="Discord.EmbedAuthorBuilder"/> instance.
56+
/// </summary>
57+
/// <param name="builder">The <see cref="EmbedAuthorBuilder"/> instance.</param>
58+
/// <returns>A copy of the <see cref="Discord.EmbedAuthorBuilder"/> instance.</returns>
59+
public static implicit operator Discord.EmbedAuthorBuilder(EmbedAuthorBuilder builder)
60+
{
61+
Discord.EmbedAuthorBuilder copy = new();
62+
63+
if (builder.Name != null)
64+
copy.WithName(builder.Name);
65+
66+
if (builder.Url != null)
67+
copy.WithUrl(builder.Url);
68+
69+
if (builder.IconUrl != null)
70+
copy.WithIconUrl(builder.IconUrl);
71+
72+
return copy;
73+
}
6374
}

DiscordLab.Bot/API/Features/Embed/EmbedBuilder.cs

Lines changed: 44 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,147 +10,116 @@ public class EmbedBuilder
1010
/// <summary>
1111
/// Gets or sets the embed title.
1212
/// </summary>
13-
public string Title
14-
{
15-
get => Base.Title;
16-
set => Base.Title = value;
17-
}
13+
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
14+
public string? Title { get; set; }
1815

1916
/// <summary>
2017
/// Gets or sets the embed description.
2118
/// </summary>
22-
public string Description
23-
{
24-
get => Base.Description;
25-
set => Base.Description = value;
26-
}
19+
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
20+
public string? Description { get; set; }
2721

2822
/// <summary>
2923
/// Gets or sets the embed fields.
3024
/// </summary>
31-
public IEnumerable<EmbedFieldBuilder> Fields
32-
{
33-
get => Base.Fields.Select(x => new EmbedFieldBuilder(x));
34-
set => Base.Fields = value.Select(x => x.Base).ToList();
35-
}
25+
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
26+
public IEnumerable<EmbedFieldBuilder>? Fields { get; set; }
3627

3728
/// <summary>
3829
/// Gets or sets the color of the embed. In string so #, 0x or the raw hex value will work.
3930
/// </summary>
4031
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
41-
public string? Color
42-
{
43-
get => Base.Color?.ToString();
44-
set
45-
{
46-
if (value == null)
47-
{
48-
Base.Color = null;
49-
return;
50-
}
51-
52-
Base.Color = Discord.Color.Parse(value);
53-
}
54-
}
32+
public string? Color { get; set; }
5533

5634
/// <summary>
5735
/// Gets or sets the thumbnail URL of the embed.
5836
/// </summary>
5937
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
60-
public string? ThumbnailUrl
61-
{
62-
get => Base.ThumbnailUrl;
63-
set => Base.ThumbnailUrl = value;
64-
}
38+
public string? ThumbnailUrl { get; set; }
6539

6640
/// <summary>
6741
/// Gets or sets the image URL of the embed.
6842
/// </summary>
6943
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
70-
public string? ImageUrl
71-
{
72-
get => Base.ImageUrl;
73-
set => Base.ImageUrl = value;
74-
}
44+
public string? ImageUrl { get; set; }
7545

7646
/// <summary>
7747
/// Gets or sets the URL of the embed.
7848
/// </summary>
7949
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
80-
public string? Url
81-
{
82-
get => Base.Url;
83-
set => Base.Url = value;
84-
}
50+
public string? Url { get; set; }
8551

8652
/// <summary>
8753
/// Gets or sets the footer of the embed.
8854
/// </summary>
8955
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
90-
public EmbedFooterBuilder? Footer
91-
{
92-
get => Base.Footer != null ? new(Base.Footer) : null;
93-
set => Base.Footer = value?.Base;
94-
}
56+
public EmbedFooterBuilder? Footer { get; set; }
9557

9658
/// <summary>
9759
/// Gets or sets the author of the embed.
9860
/// </summary>
9961
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitNull)]
100-
public EmbedAuthorBuilder? Author
101-
{
102-
get => Base.Author != null ? new(Base.Author) : null;
103-
set => Base.Author = value?.Base;
104-
}
62+
public EmbedAuthorBuilder? Author { get; set; }
10563

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

112-
[YamlIgnore]
113-
private Discord.EmbedBuilder Base { get; } = new();
114-
11570
/// <summary>
11671
/// Changes a <see cref="EmbedBuilder"/> into a <see cref="Discord.EmbedBuilder"/> instance.
11772
/// </summary>
11873
/// <param name="builder">The <see cref="EmbedBuilder"/> instance.</param>
11974
/// <returns>A copy of the <see cref="Discord.EmbedBuilder"/> instance.</returns>
12075
public static implicit operator Discord.EmbedBuilder(EmbedBuilder builder)
12176
{
77+
if (
78+
string.IsNullOrEmpty(builder.Title)
79+
&& string.IsNullOrEmpty(builder.Description)
80+
&& string.IsNullOrEmpty(builder.ThumbnailUrl)
81+
&& string.IsNullOrEmpty(builder.ImageUrl)
82+
&& (builder.Author == null || string.IsNullOrEmpty(builder.Author.Name))
83+
&& (builder.Fields == null || !builder.Fields.Any()))
84+
{
85+
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.");
86+
}
87+
12288
Discord.EmbedBuilder copy = new();
12389

124-
if (builder.Base.Title != null)
125-
copy.WithTitle(builder.Base.Title);
90+
if (builder.Title != null)
91+
copy.WithTitle(builder.Title);
12692

127-
if (builder.Base.Description != null)
128-
copy.WithDescription(builder.Base.Description);
93+
if (builder.Description != null)
94+
copy.WithDescription(builder.Description);
12995

130-
if (builder.Base.Color.HasValue)
131-
copy.WithColor(builder.Base.Color.Value);
96+
if (builder.Color != null)
97+
copy.WithColor(Discord.Color.Parse(builder.Color));
13298

133-
if (builder.Base.Url != null)
134-
copy.WithUrl(builder.Base.Url);
99+
if (builder.Url != null)
100+
copy.WithUrl(builder.Url);
135101

136-
if (builder.Base.ImageUrl != null)
137-
copy.WithImageUrl(builder.Base.ImageUrl);
102+
if (builder.ImageUrl != null)
103+
copy.WithImageUrl(builder.ImageUrl);
138104

139-
if (builder.Base.ThumbnailUrl != null)
140-
copy.WithThumbnailUrl(builder.Base.ThumbnailUrl);
105+
if (builder.ThumbnailUrl != null)
106+
copy.WithThumbnailUrl(builder.ThumbnailUrl);
141107

142108
if (builder.Timestamp)
143109
copy.WithCurrentTimestamp();
144110

145-
if (builder.Base.Footer != null)
146-
copy.WithFooter(builder.Base.Footer);
111+
if (builder.Footer != null)
112+
copy.WithFooter(builder.Footer);
147113

148-
if (builder.Base.Author != null)
149-
copy.WithAuthor(builder.Base.Author);
114+
if (builder.Author != null)
115+
copy.WithAuthor(builder.Author);
150116

151-
foreach (Discord.EmbedFieldBuilder field in builder.Base.Fields)
117+
if (builder.Fields != null)
152118
{
153-
copy.AddField(field.Name, field.Value, field.IsInline);
119+
foreach (var field in builder.Fields)
120+
{
121+
copy.AddField(field);
122+
}
154123
}
155124

156125
return copy;

DiscordLab.Bot/API/Features/Embed/EmbedFieldBuilder.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public class EmbedFieldBuilder
1212
/// </summary>
1313
public EmbedFieldBuilder()
1414
{
15-
Base = new();
1615
}
1716

1817
/// <summary>
@@ -22,40 +21,52 @@ public EmbedFieldBuilder()
2221
/// <param name="builder">The <see cref="Discord.EmbedFieldBuilder"/> instance.</param>
2322
public EmbedFieldBuilder(Discord.EmbedFieldBuilder builder)
2423
{
25-
Base = builder;
24+
Name = builder.Name;
25+
IsInline = builder.IsInline;
26+
27+
if (builder.Value is string value)
28+
Value = value;
2629
}
2730

2831
/// <summary>
2932
/// Gets or sets the field name.
3033
/// </summary>
31-
public string Name
32-
{
33-
get => Base.Name;
34-
set => Base.Name = value;
35-
}
34+
public string Name { get; set; } = string.Empty;
3635

3736
/// <summary>
3837
/// Gets or sets the field value.
3938
/// </summary>
40-
public string Value
41-
{
42-
get => Base.Value.ToString();
43-
set => Base.Value = value;
44-
}
39+
public string? Value { get; set; }
4540

4641
/// <summary>
4742
/// Gets or sets a value indicating whether the field is inline.
4843
/// </summary>
4944
[YamlMember(DefaultValuesHandling = DefaultValuesHandling.OmitDefaults)]
50-
public bool IsInline
51-
{
52-
get => Base.IsInline;
53-
set => Base.IsInline = value;
54-
}
45+
public bool IsInline { get; set; }
5546

5647
/// <summary>
5748
/// Gets the base builder.
5849
/// </summary>
5950
[YamlIgnore]
60-
public Discord.EmbedFieldBuilder Base { get; init; }
51+
[Obsolete("Please use the properties of the DiscordLab.Bot.API.Features.Embed.EmbedFieldBuilder instead.")]
52+
public Discord.EmbedFieldBuilder Base => this;
53+
54+
/// <summary>
55+
/// Changes a <see cref="EmbedFieldBuilder"/> into a <see cref="Discord.EmbedFieldBuilder"/> instance.
56+
/// </summary>
57+
/// <param name="builder">The <see cref="EmbedFieldBuilder"/> instance.</param>
58+
/// <returns>A copy of the <see cref="Discord.EmbedFieldBuilder"/> instance.</returns>
59+
public static implicit operator Discord.EmbedFieldBuilder(EmbedFieldBuilder builder)
60+
{
61+
Discord.EmbedFieldBuilder copy = new();
62+
63+
copy.WithName(builder.Name);
64+
65+
if (builder.Value != null)
66+
copy.WithValue(builder.Value);
67+
68+
copy.WithIsInline(builder.IsInline);
69+
70+
return copy;
71+
}
6172
}

0 commit comments

Comments
 (0)