Move embed building logic for !urban into the shared command code
This commit is contained in:
parent
10b29cce8a
commit
866c28b76b
3 changed files with 24 additions and 46 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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<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}"));
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue