From 866c28b76bf9639ba0c4e8fcb92cb6f362725ea3 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sat, 6 Nov 2021 16:53:37 +0100 Subject: [PATCH] Move embed building logic for !urban into the shared command code --- .../Commands/Integrations/UrbanDictionary.cs | 24 +++--------------- .../UrbanDictionary/UrbanDictionary.cs | 21 ++++++++++++++-- src/Web/Commands/UrbanDictionary.cs | 25 ++----------------- 3 files changed, 24 insertions(+), 46 deletions(-) diff --git a/src/Bot/Commands/Integrations/UrbanDictionary.cs b/src/Bot/Commands/Integrations/UrbanDictionary.cs index c0ae96a..44fe868 100644 --- a/src/Bot/Commands/Integrations/UrbanDictionary.cs +++ b/src/Bot/Commands/Integrations/UrbanDictionary.cs @@ -1,10 +1,8 @@ using System; using System.Threading.Tasks; -using Discord; using Discord.Commands; using Geekbot.Core; using Geekbot.Core.ErrorHandling; -using Geekbot.Core.Extensions; namespace Geekbot.Bot.Commands.Integrations { @@ -23,30 +21,14 @@ namespace Geekbot.Bot.Commands.Integrations { try { - var definition = await Geekbot.Commands.UrbanDictionary.UrbanDictionary.Run(word); - if (definition == null) + var eb = await Geekbot.Commands.UrbanDictionary.UrbanDictionary.Run(word); + if (eb == null) { await ReplyAsync("That word hasn't been defined..."); return; } - var eb = new EmbedBuilder(); - eb.WithAuthor(new EmbedAuthorBuilder - { - Name = definition.Word, - Url = definition.Permalink - }); - var c = System.Drawing.Color.Gold; - eb.WithColor(new Color(c.R, c.G, c.B)); - - static string ShortenIfToLong(string str, int maxLength) => str.Length > maxLength ? $"{str.Substring(0, maxLength - 5)}[...]" : str; - - if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = ShortenIfToLong(definition.Definition, 1800); - if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", ShortenIfToLong(definition.Example, 1024)); - if (definition.ThumbsUp != 0) eb.AddInlineField("Upvotes", definition.ThumbsUp); - if (definition.ThumbsDown != 0) eb.AddInlineField("Downvotes", definition.ThumbsDown); - - await ReplyAsync(string.Empty, false, eb.Build()); + await ReplyAsync(string.Empty, false, eb.ToDiscordNetEmbed().Build()); } catch (Exception e) { diff --git a/src/Commands/UrbanDictionary/UrbanDictionary.cs b/src/Commands/UrbanDictionary/UrbanDictionary.cs index a437daf..7b99c2b 100644 --- a/src/Commands/UrbanDictionary/UrbanDictionary.cs +++ b/src/Commands/UrbanDictionary/UrbanDictionary.cs @@ -1,10 +1,12 @@ +using System.Drawing; using Geekbot.Core; +using Geekbot.Core.Interactions.Embed; namespace Geekbot.Commands.UrbanDictionary; public class UrbanDictionary { - public static async Task Run(string term) + public static async Task Run(string term) { var definitions = await HttpAbstractions.Get(new Uri($"https://api.urbandictionary.com/v0/define?term={term}")); @@ -15,6 +17,21 @@ public class UrbanDictionary var definition = definitions.List.First(e => !string.IsNullOrWhiteSpace(e.Example)); - return definition; + static string ShortenIfToLong(string str, int maxLength) => str.Length > maxLength ? $"{str[..(maxLength - 5)]}[...]" : str; + + var eb = new Embed(); + eb.Author = new() + { + Name = definition.Word, + Url = definition.Permalink + }; + eb.SetColor(Color.Gold); + + if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = ShortenIfToLong(definition.Definition, 1800); + if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", ShortenIfToLong(definition.Example, 1024)); + if (definition.ThumbsUp != 0) eb.AddInlineField("Upvotes", definition.ThumbsUp.ToString()); + if (definition.ThumbsDown != 0) eb.AddInlineField("Downvotes", definition.ThumbsDown.ToString()); + + return eb; } } \ No newline at end of file diff --git a/src/Web/Commands/UrbanDictionary.cs b/src/Web/Commands/UrbanDictionary.cs index ca5794f..4931979 100644 --- a/src/Web/Commands/UrbanDictionary.cs +++ b/src/Web/Commands/UrbanDictionary.cs @@ -41,29 +41,8 @@ public class UrbanDictionary : InteractionBase { var term = interaction.Data.Options.Find(o => o.Name == Options.Term); - var definition = await Geekbot.Commands.UrbanDictionary.UrbanDictionary.Run(term.Value.GetString()); - if (definition == null) - { - return SimpleResponse("That word hasn't been defined..."); - } - - static string ShortenIfToLong(string str, int maxLength) => str.Length > maxLength ? $"{str.Substring(0, maxLength - 5)}[...]" : str; - - var eb = new Embed(); - eb.Author = new() - { - Name = definition.Word, - Url = definition.Permalink - }; - eb.SetColor(Color.Gold); - - if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = ShortenIfToLong(definition.Definition, 1800); - if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", ShortenIfToLong(definition.Example, 1024)); - if (definition.ThumbsUp != 0) eb.AddInlineField("Upvotes", definition.ThumbsUp.ToString()); - if (definition.ThumbsDown != 0) eb.AddInlineField("Downvotes", definition.ThumbsDown.ToString()); - - - return SimpleResponse(eb); + var eb = await Geekbot.Commands.UrbanDictionary.UrbanDictionary.Run(term.Value.GetString()); + return eb == null ? SimpleResponse("That word hasn't been defined...") : SimpleResponse(eb); } public override void OnException(Interaction interaction, Exception exception)