From cbe2aa23c955d01dcf31455a45cf448f922d95dc Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 15 Sep 2020 23:25:40 +0200 Subject: [PATCH] Make sure that examples in the !urban command are not longer than 1024 characters. --- .../Integrations/UbranDictionary/UrbanDictionary.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index bc1f478..72bc8cd 100644 --- a/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -33,12 +33,6 @@ namespace Geekbot.Bot.Commands.Integrations.UbranDictionary var definition = definitions.List.First(e => !string.IsNullOrWhiteSpace(e.Example)); - var description = definition.Definition; - if (description.Length > 1801) - { - description = description.Substring(0, 1800) + " [...]"; - } - var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder { @@ -46,8 +40,11 @@ namespace Geekbot.Bot.Commands.Integrations.UbranDictionary Url = definition.Permalink }); eb.WithColor(new Color(239, 255, 0)); - if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = description; - if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", definition.Example ?? "(no example given...)"); + + 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 (!string.IsNullOrEmpty(definition.ThumbsUp)) eb.AddInlineField("Upvotes", definition.ThumbsUp); if (!string.IsNullOrEmpty(definition.ThumbsDown)) eb.AddInlineField("Downvotes", definition.ThumbsDown); if (definitions.Tags?.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags));