Move embed building logic for !urban into the shared command code

This commit is contained in:
Daan Boerlage 2021-11-06 16:53:37 +01:00
parent 10b29cce8a
commit 866c28b76b
Signed by: daan
GPG key ID: FCE070E1E4956606
3 changed files with 24 additions and 46 deletions

View file

@ -1,10 +1,8 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord;
using Discord.Commands; using Discord.Commands;
using Geekbot.Core; using Geekbot.Core;
using Geekbot.Core.ErrorHandling; using Geekbot.Core.ErrorHandling;
using Geekbot.Core.Extensions;
namespace Geekbot.Bot.Commands.Integrations namespace Geekbot.Bot.Commands.Integrations
{ {
@ -23,30 +21,14 @@ namespace Geekbot.Bot.Commands.Integrations
{ {
try try
{ {
var definition = await Geekbot.Commands.UrbanDictionary.UrbanDictionary.Run(word); var eb = await Geekbot.Commands.UrbanDictionary.UrbanDictionary.Run(word);
if (definition == null) if (eb == null)
{ {
await ReplyAsync("That word hasn't been defined..."); await ReplyAsync("That word hasn't been defined...");
return; return;
} }
var eb = new EmbedBuilder(); await ReplyAsync(string.Empty, false, eb.ToDiscordNetEmbed().Build());
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());
} }
catch (Exception e) catch (Exception e)
{ {

View file

@ -1,10 +1,12 @@
using System.Drawing;
using Geekbot.Core; using Geekbot.Core;
using Geekbot.Core.Interactions.Embed;
namespace Geekbot.Commands.UrbanDictionary; namespace Geekbot.Commands.UrbanDictionary;
public class UrbanDictionary public class UrbanDictionary
{ {
public static async Task<UrbanDictionaryListItem?> Run(string term) public static async Task<Embed?> Run(string term)
{ {
var definitions = await HttpAbstractions.Get<UrbanDictionaryResponse>(new Uri($"https://api.urbandictionary.com/v0/define?term={term}")); var definitions = await HttpAbstractions.Get<UrbanDictionaryResponse>(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)); 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;
} }
} }

View file

@ -41,29 +41,8 @@ public class UrbanDictionary : InteractionBase
{ {
var term = interaction.Data.Options.Find(o => o.Name == Options.Term); var term = interaction.Data.Options.Find(o => o.Name == Options.Term);
var definition = await Geekbot.Commands.UrbanDictionary.UrbanDictionary.Run(term.Value.GetString()); var eb = await Geekbot.Commands.UrbanDictionary.UrbanDictionary.Run(term.Value.GetString());
if (definition == null) return eb == null ? SimpleResponse("That word hasn't been defined...") : SimpleResponse(eb);
{
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);
} }
public override void OnException(Interaction interaction, Exception exception) public override void OnException(Interaction interaction, Exception exception)