From fdd38c8d7f9292b6ab5e25ac9596b973e07fcca6 Mon Sep 17 00:00:00 2001 From: Finite Reality Date: Thu, 29 Jun 2017 23:44:08 +0100 Subject: [PATCH] Add embed builder extensions (#460) * Add embed builder extensions People in #dotnet_discord-net suggested that this should be part of the lib after I demonstrated it * Move some extensions into EmbedBuilder [2] Apparently git didn't like that previous commit * Fix error with EmbedBuilderExtensions A summary of issues which happened: - Git decided to add an amend commit (I told it to quit?) - VS Code thinks everything is an error so it wasn't helpful - dotnet decided to think there was no error until I deleted all build outputs and rebuild Sometimes I question my ability to use version control properly. --- .../Entities/Messages/EmbedBuilder.cs | 32 +++++++++++++++++++ .../Extensions/EmbedBuilderExtensions.cs | 20 ++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/Discord.Net.Rest/Extensions/EmbedBuilderExtensions.cs diff --git a/src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs b/src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs index c299bd1a1..2331f6749 100644 --- a/src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs +++ b/src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs @@ -137,6 +137,17 @@ namespace Discord Author = author; return this; } + public EmbedBuilder WithAuthor(string name, string iconUrl = null, string url = null) + { + var author = new EmbedAuthorBuilder + { + Name = name, + IconUrl = iconUrl, + Url = url + }; + Author = author; + return this; + } public EmbedBuilder WithFooter(EmbedFooterBuilder footer) { Footer = footer; @@ -149,6 +160,16 @@ namespace Discord Footer = footer; return this; } + public EmbedBuilder WithFooter(string text, string iconUrl = null) + { + var footer = new EmbedFooterBuilder + { + Text = text, + IconUrl = iconUrl + }; + Footer = footer; + return this; + } public EmbedBuilder AddField(string name, object value) { @@ -185,6 +206,17 @@ namespace Discord this.AddField(field); return this; } + public EmbedBuilder AddField(string title, string text, bool inline = false) + { + var field = new EmbedFieldBuilder + { + Name = title, + Value = text, + IsInline = inline + }; + _fields.Add(field); + return this; + } public Embed Build() { diff --git a/src/Discord.Net.Rest/Extensions/EmbedBuilderExtensions.cs b/src/Discord.Net.Rest/Extensions/EmbedBuilderExtensions.cs new file mode 100644 index 000000000..64f96c93f --- /dev/null +++ b/src/Discord.Net.Rest/Extensions/EmbedBuilderExtensions.cs @@ -0,0 +1,20 @@ +namespace Discord +{ + public static class EmbedBuilderExtensions + { + public static EmbedBuilder WithColor(this EmbedBuilder builder, uint rawValue) => + builder.WithColor(new Color(rawValue)); + + public static EmbedBuilder WithColor(this EmbedBuilder builder, byte r, byte g, byte b) => + builder.WithColor(new Color(r, g, b)); + + public static EmbedBuilder WithColor(this EmbedBuilder builder, float r, float g, float b) => + builder.WithColor(new Color(r, g, b)); + + public static EmbedBuilder WithAuthor(this EmbedBuilder builder, IUser user) => + builder.WithAuthor($"{user.Username}#{user.Discriminator}", user.AvatarUrl); + + public static EmbedBuilder WithAuthor(this EmbedBuilder builder, IGuildUser user) => + builder.WithAuthor($"{user.Nickname ?? user.Username}#{user.Discriminator}", user.AvatarUrl); + } +}