Cleaned up EmbedBuilder

This commit is contained in:
RogueException
2016-11-14 20:16:24 -04:00
parent c2722cf7c4
commit af6c3e10c3

View File

@@ -9,124 +9,74 @@ namespace Discord
{ {
public class EmbedBuilder public class EmbedBuilder
{ {
private Embed embed = new Embed(); private Embed model = new Embed();
List<Field> fields = new List<Field>(); private List<Field> fields = new List<Field>();
public EmbedBuilder() public EmbedBuilder()
{ {
embed.Type = "rich"; model.Type = "rich";
} }
public EmbedBuilder Title(string title) public string Title { get { return model.Title; } set { model.Title = value; } }
public string Description { get { return model.Description; } set { model.Description = value; } }
public string Url { get { return model.Url; } set { model.Url = value; } }
public Color? Color { get { return model.Color.HasValue ? new Color(model.Color.Value) : (Color?)null; } set { model.Color = value?.RawValue; } }
public void SetAuthor(Action<EmbedBuilderAuthor> action)
{ {
embed.Title = title; var author = new EmbedBuilderAuthor();
return this; action(author);
model.Author = author.ToModel();
} }
public EmbedBuilder Description(string description) public void SetFooter(Action<EmbedBuilderFooter> action)
{ {
embed.Description = description; var footer = new EmbedBuilderFooter();
return this; action(footer);
model.Footer = footer.ToModel();
} }
public EmbedBuilder Url(string url) public void AddField(Action<EmbedBuilderField> action)
{ {
embed.Url = url; var field = new EmbedBuilderField();
return this; action(field);
} fields.Add(field.ToModel());
public EmbedBuilder Color(Color color)
{
embed.Color = color.RawValue;
return this;
}
public EmbedBuilder Field(Func<EmbedFieldBuilder, EmbedFieldBuilder> builder)
{
fields.Add(builder(new EmbedFieldBuilder()).Build());
return this;
}
public EmbedBuilder Author(Func<EmbedAuthorBuilder, EmbedAuthorBuilder> builder)
{
embed.Author = builder(new EmbedAuthorBuilder()).Build();
return this;
}
public EmbedBuilder Footer(Func<EmbedFooterBuilder, EmbedFooterBuilder> builder)
{
embed.Footer = builder(new EmbedFooterBuilder()).Build();
return this;
}
public Embed Build()
{
embed.Fields = fields.ToArray();
return embed;
} }
} internal Embed Build()
public class EmbedFieldBuilder
{
private Field embedField = new Field();
public EmbedFieldBuilder Name(string name)
{ {
embedField.Name = name; model.Fields = fields.ToArray();
return this; return model;
}
public EmbedFieldBuilder Value(string value)
{
embedField.Value = value;
return this;
}
public EmbedFieldBuilder Inline(bool inline)
{
embedField.Inline = inline;
return this;
}
public Field Build()
{
return embedField;
} }
} }
public class EmbedAuthorBuilder public class EmbedBuilderField
{ {
private Author author = new Author(); private Field model = new Field();
public EmbedAuthorBuilder Name(string name) public string Name { get { return model.Name; } set { model.Name = value; } }
{ public string Value { get { return model.Value; } set { model.Value = value; } }
author.Name = name; public bool IsInline { get { return model.Inline; } set { model.Inline = value; } }
return this;
} internal Field ToModel() => model;
public EmbedAuthorBuilder Url(string url)
{
author.Url = url;
return this;
}
public EmbedAuthorBuilder IconUrl(string iconUrl)
{
author.IconUrl = iconUrl;
return this;
}
public Author Build()
{
return author;
}
} }
public class EmbedFooterBuilder public class EmbedBuilderAuthor
{ {
private Footer footer = new Footer(); private Author model = new Author();
public EmbedFooterBuilder Text(string text) public string Name { get { return model.Name; } set { model.Name = value; } }
{ public string Url { get { return model.Url; } set { model.Url = value; } }
footer.Text = text; public string IconUrl { get { return model.IconUrl; } set { model.IconUrl = value; } }
return this;
} internal Author ToModel() => model;
public EmbedFooterBuilder IconUrl(string iconUrl) }
{
footer.IconUrl = iconUrl; public class EmbedBuilderFooter
return this; {
} private Footer model = new Footer();
public Footer Build()
{ public string Text { get { return model.Text; } set { model.Text = value; } }
return footer; public string IconUrl { get { return model.IconUrl; } set { model.IconUrl = value; } }
}
internal Footer ToModel() => model;
} }
} }