From 2c990f08f8fb02fa196e62e71b45f2cd9571f8db Mon Sep 17 00:00:00 2001 From: RogueException Date: Fri, 18 Nov 2016 08:33:34 -0400 Subject: [PATCH] Readded fluent-style to EmbedBuilder --- .../Entities/Messages/EmbedBuilder.cs | 181 ++++++++++++++++++ src/Discord.Net.Core/Utils/EmbedBuilder.cs | 82 -------- 2 files changed, 181 insertions(+), 82 deletions(-) create mode 100644 src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs delete mode 100644 src/Discord.Net.Core/Utils/EmbedBuilder.cs diff --git a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs new file mode 100644 index 000000000..5c0c72e8f --- /dev/null +++ b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs @@ -0,0 +1,181 @@ +using System; +using System.Collections.Generic; +using Embed = Discord.API.Embed; +using Field = Discord.API.EmbedField; +using Author = Discord.API.EmbedAuthor; +using Footer = Discord.API.EmbedFooter; + +namespace Discord +{ + public class EmbedBuilder + { + private readonly Embed _model; + private readonly List _fields; + + public EmbedBuilder() + { + _model = new Embed(); + _model.Type = "rich"; + _fields = new List(); + } + + 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 EmbedAuthorBuilder Author { get; set; } + public EmbedFooterBuilder Footer { get; set; } + + public EmbedBuilder WithTitle(string title) + { + Title = title; + return this; + } + public EmbedBuilder WithDescription(string description) + { + Description = description; + return this; + } + public EmbedBuilder WithUrl(string url) + { + Url = url; + return this; + } + public EmbedBuilder WithColor(Color color) + { + Color = color; + return this; + } + + public EmbedBuilder WithAuthor(EmbedAuthorBuilder author) + { + Author = author; + return this; + } + public EmbedBuilder WithAuthor(Action action) + { + var author = new EmbedAuthorBuilder(); + action(author); + Author = author; + return this; + } + public EmbedBuilder WithFooter(EmbedFooterBuilder footer) + { + Footer = footer; + return this; + } + public EmbedBuilder WithFooter(Action action) + { + var footer = new EmbedFooterBuilder(); + action(footer); + Footer = footer; + return this; + } + + public EmbedBuilder AddField(Action action) + { + var field = new EmbedFieldBuilder(); + action(field); + _fields.Add(field.ToModel()); + return this; + } + + internal Embed Build() + { + _model.Author = Author?.ToModel(); + _model.Footer = Footer?.ToModel(); + _model.Fields = _fields.ToArray(); + return _model; + } + } + + public class EmbedFieldBuilder + { + private Field _model; + + public string Name { get { return _model.Name; } set { _model.Name = value; } } + public string Value { get { return _model.Value; } set { _model.Value = value; } } + public bool IsInline { get { return _model.Inline; } set { _model.Inline = value; } } + + public EmbedFieldBuilder() + { + _model = new Field(); + } + + public EmbedFieldBuilder WithName(string name) + { + Name = name; + return this; + } + public EmbedFieldBuilder WithValue(string value) + { + Value = value; + return this; + } + public EmbedFieldBuilder WithIsInline(bool isInline) + { + IsInline = isInline; + return this; + } + + internal Field ToModel() => _model; + } + + public class EmbedAuthorBuilder + { + private Author _model; + + public string Name { get { return _model.Name; } set { _model.Name = value; } } + public string Url { get { return _model.Url; } set { _model.Url = value; } } + public string IconUrl { get { return _model.IconUrl; } set { _model.IconUrl = value; } } + + public EmbedAuthorBuilder() + { + _model = new Author(); + } + + public EmbedAuthorBuilder WithName(string name) + { + Name = name; + return this; + } + public EmbedAuthorBuilder WithUrl(string url) + { + Url = url; + return this; + } + public EmbedAuthorBuilder WithIconUrl(string iconUrl) + { + IconUrl = iconUrl; + return this; + } + + internal Author ToModel() => _model; + } + + public class EmbedFooterBuilder + { + private Footer _model; + + public string Text { get { return _model.Text; } set { _model.Text = value; } } + public string IconUrl { get { return _model.IconUrl; } set { _model.IconUrl = value; } } + + public EmbedFooterBuilder() + { + _model = new Footer(); + } + + public EmbedFooterBuilder WithText(string text) + { + Text = text; + return this; + } + public EmbedFooterBuilder WithIconUrl(string iconUrl) + { + IconUrl = iconUrl; + return this; + } + + internal Footer ToModel() => _model; + } +} diff --git a/src/Discord.Net.Core/Utils/EmbedBuilder.cs b/src/Discord.Net.Core/Utils/EmbedBuilder.cs deleted file mode 100644 index dca4377eb..000000000 --- a/src/Discord.Net.Core/Utils/EmbedBuilder.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Collections.Generic; -using Embed = Discord.API.Embed; -using Field = Discord.API.EmbedField; -using Author = Discord.API.EmbedAuthor; -using Footer = Discord.API.EmbedFooter; - -namespace Discord -{ - public class EmbedBuilder - { - private Embed model = new Embed(); - private List fields = new List(); - - public EmbedBuilder() - { - model.Type = "rich"; - } - - 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 action) - { - var author = new EmbedBuilderAuthor(); - action(author); - model.Author = author.ToModel(); - } - public void SetFooter(Action action) - { - var footer = new EmbedBuilderFooter(); - action(footer); - model.Footer = footer.ToModel(); - } - public void AddField(Action action) - { - var field = new EmbedBuilderField(); - action(field); - fields.Add(field.ToModel()); - } - - internal Embed Build() - { - model.Fields = fields.ToArray(); - return model; - } - } - - public class EmbedBuilderField - { - private Field model = new Field(); - - public string Name { get { return model.Name; } set { model.Name = value; } } - public string Value { get { return model.Value; } set { model.Value = value; } } - public bool IsInline { get { return model.Inline; } set { model.Inline = value; } } - - internal Field ToModel() => model; - } - - public class EmbedBuilderAuthor - { - private Author model = new Author(); - - public string Name { get { return model.Name; } set { model.Name = value; } } - public string Url { get { return model.Url; } set { model.Url = value; } } - public string IconUrl { get { return model.IconUrl; } set { model.IconUrl = value; } } - - internal Author ToModel() => model; - } - - public class EmbedBuilderFooter - { - private Footer model = new Footer(); - - public string Text { get { return model.Text; } set { model.Text = value; } } - public string IconUrl { get { return model.IconUrl; } set { model.IconUrl = value; } } - - internal Footer ToModel() => model; - } -}