From 52f979ec8a19600872c0bb73b4f7c27f1d2b6027 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sun, 13 Nov 2016 00:35:46 -0500 Subject: [PATCH] Support sending rich embeds, add an Embed Builder --- .../API/Rest/CreateMessageParams.cs | 3 + .../Entities/Messages/IEmbed.cs | 2 +- src/Discord.Net.Core/Utils/EmbedBuilder.cs | 132 ++++++++++++++++++ .../Entities/Channels/ChannelHelper.cs | 7 + .../Entities/Messages/Embed.cs | 14 +- 5 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 src/Discord.Net.Core/Utils/EmbedBuilder.cs diff --git a/src/Discord.Net.Core/API/Rest/CreateMessageParams.cs b/src/Discord.Net.Core/API/Rest/CreateMessageParams.cs index 9577ab579..a0dbb59dd 100644 --- a/src/Discord.Net.Core/API/Rest/CreateMessageParams.cs +++ b/src/Discord.Net.Core/API/Rest/CreateMessageParams.cs @@ -1,4 +1,5 @@ #pragma warning disable CS1591 +using System; using Newtonsoft.Json; namespace Discord.API.Rest @@ -13,6 +14,8 @@ namespace Discord.API.Rest public Optional Nonce { get; set; } [JsonProperty("tts")] public Optional IsTTS { get; set; } + [JsonProperty("embed")] + public Optional Embed { get; set; } public CreateMessageParams(string content) { diff --git a/src/Discord.Net.Core/Entities/Messages/IEmbed.cs b/src/Discord.Net.Core/Entities/Messages/IEmbed.cs index 311fbff01..c46b22d17 100644 --- a/src/Discord.Net.Core/Entities/Messages/IEmbed.cs +++ b/src/Discord.Net.Core/Entities/Messages/IEmbed.cs @@ -8,7 +8,7 @@ namespace Discord string Type { get; } string Title { get; } string Description { get; } - uint? Color { get; } + Color? Color { get; } EmbedAuthor? Author { get; } EmbedFooter? Footer { get; } EmbedProvider? Provider { get; } diff --git a/src/Discord.Net.Core/Utils/EmbedBuilder.cs b/src/Discord.Net.Core/Utils/EmbedBuilder.cs new file mode 100644 index 000000000..42861709f --- /dev/null +++ b/src/Discord.Net.Core/Utils/EmbedBuilder.cs @@ -0,0 +1,132 @@ +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 embed = new Embed(); + List fields = new List(); + + public EmbedBuilder() + { + embed.Type = "rich"; + } + + public EmbedBuilder Title(string title) + { + embed.Title = title; + return this; + } + public EmbedBuilder Description(string description) + { + embed.Description = description; + return this; + } + public EmbedBuilder Url(string url) + { + embed.Url = url; + return this; + } + public EmbedBuilder Color(Color color) + { + embed.Color = color.RawValue; + return this; + } + public EmbedBuilder Field(Func builder) + { + fields.Add(builder(new EmbedFieldBuilder()).Build()); + return this; + } + public EmbedBuilder Author(Func builder) + { + embed.Author = builder(new EmbedAuthorBuilder()).Build(); + return this; + } + public EmbedBuilder Footer(Func builder) + { + embed.Footer = builder(new EmbedFooterBuilder()).Build(); + return this; + } + public Embed Build() + { + embed.Fields = fields.ToArray(); + return embed; + } + + } + + public class EmbedFieldBuilder + { + private Field embedField = new Field(); + + public EmbedFieldBuilder Name(string name) + { + embedField.Name = name; + return this; + } + 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 + { + private Author author = new Author(); + + public EmbedAuthorBuilder Name(string name) + { + author.Name = name; + return this; + } + 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 + { + private Footer footer = new Footer(); + + public EmbedFooterBuilder Text(string text) + { + footer.Text = text; + return this; + } + public EmbedFooterBuilder IconUrl(string iconUrl) + { + footer.IconUrl = iconUrl; + return this; + } + public Footer Build() + { + return footer; + } + } +} diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs index 858269366..a27bf0493 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs @@ -116,6 +116,13 @@ namespace Discord.Rest var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false); return RestUserMessage.Create(client, guild, model); } + public static async Task SendRichMessageAsync(IChannel channel, BaseDiscordClient client, + string text, bool isTTS, API.Embed embed, IGuild guild, RequestOptions options) + { + var args = new CreateMessageParams(text) { IsTTS = isTTS, Embed = embed }; + var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false); + return RestUserMessage.Create(client, guild, model); + } public static async Task SendFileAsync(IChannel channel, BaseDiscordClient client, string filePath, string text, bool isTTS, IGuild guild, RequestOptions options) diff --git a/src/Discord.Net.Rest/Entities/Messages/Embed.cs b/src/Discord.Net.Rest/Entities/Messages/Embed.cs index e85bf9cca..540a39ea2 100644 --- a/src/Discord.Net.Rest/Entities/Messages/Embed.cs +++ b/src/Discord.Net.Rest/Entities/Messages/Embed.cs @@ -1,4 +1,5 @@ -using System.Collections.Immutable; +using System; +using System.Collections.Immutable; using System.Diagnostics; using System.Linq; using Model = Discord.API.Embed; @@ -12,7 +13,7 @@ namespace Discord public string Url { get; } public string Title { get; } public string Type { get; } - public uint? Color { get; } + public Color? Color { get; } public EmbedAuthor? Author { get; } public EmbedFooter? Footer { get; } public EmbedProvider? Provider { get; } @@ -23,7 +24,7 @@ namespace Discord string title, string description, string url, - uint? color, + Color? color, EmbedAuthor? author, EmbedFooter? footer, EmbedProvider? provider, @@ -34,12 +35,17 @@ namespace Discord Title = title; Description = description; Url = url; + Color = color; + Author = author; + Footer = footer; Provider = provider; Thumbnail = thumbnail; + Fields = fields; } internal static Embed Create(Model model) { - return new Embed(model.Type, model.Title, model.Description, model.Url, model.Color, + return new Embed(model.Type, model.Title, model.Description, model.Url, + model.Color.HasValue ? new Color(model.Color.Value) : (Color?)null, model.Author.IsSpecified ? EmbedAuthor.Create(model.Author.Value) : (EmbedAuthor?)null, model.Footer.IsSpecified ? EmbedFooter.Create(model.Footer.Value) : (EmbedFooter?)null, model.Provider.IsSpecified ? EmbedProvider.Create(model.Provider.Value) : (EmbedProvider?)null,