Port !urban to a / command
This commit is contained in:
parent
ea17ce2866
commit
34f15402b4
7 changed files with 136 additions and 49 deletions
|
@ -1,26 +0,0 @@
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace Geekbot.Bot.Commands.Integrations.UbranDictionary
|
|
||||||
{
|
|
||||||
internal class UrbanListItemDto
|
|
||||||
{
|
|
||||||
|
|
||||||
[JsonPropertyName("definition")]
|
|
||||||
public string Definition { get; set; }
|
|
||||||
|
|
||||||
[JsonPropertyName("permalink")]
|
|
||||||
public string Permalink { get; set; }
|
|
||||||
|
|
||||||
[JsonPropertyName("thumbs_up")]
|
|
||||||
public int ThumbsUp { get; set; }
|
|
||||||
|
|
||||||
[JsonPropertyName("word")]
|
|
||||||
public string Word { get; set; }
|
|
||||||
|
|
||||||
[JsonPropertyName("example")]
|
|
||||||
public string Example { get; set; }
|
|
||||||
|
|
||||||
[JsonPropertyName("thumbs_down")]
|
|
||||||
public int ThumbsDown { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace Geekbot.Bot.Commands.Integrations.UbranDictionary
|
|
||||||
{
|
|
||||||
internal class UrbanResponseDto
|
|
||||||
{
|
|
||||||
[JsonPropertyName("tags")]
|
|
||||||
public string[] Tags { get; set; }
|
|
||||||
|
|
||||||
[JsonPropertyName("list")]
|
|
||||||
public List<UrbanListItemDto> List { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
@ -7,7 +6,7 @@ using Geekbot.Core;
|
||||||
using Geekbot.Core.ErrorHandling;
|
using Geekbot.Core.ErrorHandling;
|
||||||
using Geekbot.Core.Extensions;
|
using Geekbot.Core.Extensions;
|
||||||
|
|
||||||
namespace Geekbot.Bot.Commands.Integrations.UbranDictionary
|
namespace Geekbot.Bot.Commands.Integrations
|
||||||
{
|
{
|
||||||
public class UrbanDictionary : TransactionModuleBase
|
public class UrbanDictionary : TransactionModuleBase
|
||||||
{
|
{
|
||||||
|
@ -24,22 +23,21 @@ namespace Geekbot.Bot.Commands.Integrations.UbranDictionary
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var definitions = await HttpAbstractions.Get<UrbanResponseDto>(new Uri($"https://api.urbandictionary.com/v0/define?term={word}"));
|
var definition = await Geekbot.Commands.UrbanDictionary.UrbanDictionary.Run(word);
|
||||||
if (definitions.List.Count == 0)
|
if (definition == null)
|
||||||
{
|
{
|
||||||
await ReplyAsync("That word hasn't been defined...");
|
await ReplyAsync("That word hasn't been defined...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var definition = definitions.List.First(e => !string.IsNullOrWhiteSpace(e.Example));
|
|
||||||
|
|
||||||
var eb = new EmbedBuilder();
|
var eb = new EmbedBuilder();
|
||||||
eb.WithAuthor(new EmbedAuthorBuilder
|
eb.WithAuthor(new EmbedAuthorBuilder
|
||||||
{
|
{
|
||||||
Name = definition.Word,
|
Name = definition.Word,
|
||||||
Url = definition.Permalink
|
Url = definition.Permalink
|
||||||
});
|
});
|
||||||
eb.WithColor(new Color(239, 255, 0));
|
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;
|
static string ShortenIfToLong(string str, int maxLength) => str.Length > maxLength ? $"{str.Substring(0, maxLength - 5)}[...]" : str;
|
||||||
|
|
||||||
|
@ -47,9 +45,8 @@ namespace Geekbot.Bot.Commands.Integrations.UbranDictionary
|
||||||
if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", ShortenIfToLong(definition.Example, 1024));
|
if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", ShortenIfToLong(definition.Example, 1024));
|
||||||
if (definition.ThumbsUp != 0) eb.AddInlineField("Upvotes", definition.ThumbsUp);
|
if (definition.ThumbsUp != 0) eb.AddInlineField("Upvotes", definition.ThumbsUp);
|
||||||
if (definition.ThumbsDown != 0) eb.AddInlineField("Downvotes", definition.ThumbsDown);
|
if (definition.ThumbsDown != 0) eb.AddInlineField("Downvotes", definition.ThumbsDown);
|
||||||
if (definitions.Tags?.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags));
|
|
||||||
|
|
||||||
await ReplyAsync("", false, eb.Build());
|
await ReplyAsync(string.Empty, false, eb.Build());
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
20
src/Commands/UrbanDictionary/UrbanDictionary.cs
Normal file
20
src/Commands/UrbanDictionary/UrbanDictionary.cs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
using Geekbot.Core;
|
||||||
|
|
||||||
|
namespace Geekbot.Commands.UrbanDictionary;
|
||||||
|
|
||||||
|
public class UrbanDictionary
|
||||||
|
{
|
||||||
|
public static async Task<UrbanDictionaryListItem?> Run(string term)
|
||||||
|
{
|
||||||
|
var definitions = await HttpAbstractions.Get<UrbanDictionaryResponse>(new Uri($"https://api.urbandictionary.com/v0/define?term={term}"));
|
||||||
|
|
||||||
|
if (definitions.List.Count == 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var definition = definitions.List.First(e => !string.IsNullOrWhiteSpace(e.Example));
|
||||||
|
|
||||||
|
return definition;
|
||||||
|
}
|
||||||
|
}
|
24
src/Commands/UrbanDictionary/UrbanDictionaryListItem.cs
Normal file
24
src/Commands/UrbanDictionary/UrbanDictionaryListItem.cs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Geekbot.Commands.UrbanDictionary;
|
||||||
|
|
||||||
|
public record UrbanDictionaryListItem
|
||||||
|
{
|
||||||
|
[JsonPropertyName("definition")]
|
||||||
|
public string Definition { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("permalink")]
|
||||||
|
public string Permalink { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("thumbs_up")]
|
||||||
|
public int ThumbsUp { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("word")]
|
||||||
|
public string Word { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("example")]
|
||||||
|
public string Example { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("thumbs_down")]
|
||||||
|
public int ThumbsDown { get; set; }
|
||||||
|
}
|
12
src/Commands/UrbanDictionary/UrbanDictionaryResponse.cs
Normal file
12
src/Commands/UrbanDictionary/UrbanDictionaryResponse.cs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Geekbot.Commands.UrbanDictionary;
|
||||||
|
|
||||||
|
public struct UrbanDictionaryResponse
|
||||||
|
{
|
||||||
|
[JsonPropertyName("tags")]
|
||||||
|
public string[] Tags { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("list")]
|
||||||
|
public List<UrbanDictionaryListItem?> List { get; set; }
|
||||||
|
}
|
74
src/Web/Commands/UrbanDictionary.cs
Normal file
74
src/Web/Commands/UrbanDictionary.cs
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Geekbot.Core.Interactions;
|
||||||
|
using Geekbot.Core.Interactions.ApplicationCommand;
|
||||||
|
using Geekbot.Core.Interactions.Embed;
|
||||||
|
using Geekbot.Core.Interactions.Request;
|
||||||
|
using Geekbot.Core.Interactions.Response;
|
||||||
|
|
||||||
|
namespace Geekbot.Web.Commands;
|
||||||
|
|
||||||
|
public class UrbanDictionary : InteractionBase
|
||||||
|
{
|
||||||
|
private struct Options
|
||||||
|
{
|
||||||
|
internal const string Term = "term";
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Command GetCommandInfo()
|
||||||
|
{
|
||||||
|
return new Command()
|
||||||
|
{
|
||||||
|
Name = "urban",
|
||||||
|
Description = "Lookup something on urban dictionary",
|
||||||
|
Type = CommandType.ChatInput,
|
||||||
|
Options = new List<Option>
|
||||||
|
{
|
||||||
|
new ()
|
||||||
|
{
|
||||||
|
Name = Options.Term,
|
||||||
|
Description = "The term to lookup",
|
||||||
|
Required = true,
|
||||||
|
Type = OptionType.String
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task<InteractionResponse> Exec(Interaction interaction)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnException(Interaction interaction, Exception exception)
|
||||||
|
{
|
||||||
|
base.OnException(interaction, exception);
|
||||||
|
Console.WriteLine(exception);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue