From 718c82a8912481d14d2c52c4a379648be9144d90 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 25 Dec 2016 17:46:36 +0100 Subject: [PATCH] Pretty terrible `~define` added --- src/NadekoBot/Modules/Searches/Searches.cs | 36 +++++++++++++++++++ .../Resources/CommandStrings.Designer.cs | 27 ++++++++++++++ src/NadekoBot/Resources/CommandStrings.resx | 9 +++++ 3 files changed, 72 insertions(+) diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index df85301a..73e68412 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -472,6 +472,42 @@ namespace NadekoBot.Modules.Searches } } + [NadekoCommand, Usage, Description, Aliases] + [RequireContext(ContextType.Guild)] + public async Task Define(IUserMessage msg, [Remainder] string word) + { + var channel = (ITextChannel)msg.Channel; + + if (string.IsNullOrWhiteSpace(word)) + return; + + using (var http = new HttpClient()) + { + var res = await http.GetStringAsync("http://api.pearson.com/v2/dictionaries/entries?headword=" + WebUtility.UrlEncode(word.Trim())).ConfigureAwait(false); + + var data = JsonConvert.DeserializeObject(res); + + var sense = data.Results.Where(x => x.Senses != null && x.Senses[0].Definition != null).FirstOrDefault()?.Senses[0]; + + if (sense?.Definition == null) + return; + + string definition = sense.Definition.ToString(); + if (!(sense.Definition is string)) + definition = ((JArray)JToken.Parse(sense.Definition.ToString())).First.ToString(); + + var embed = new EmbedBuilder().WithOkColor() + .WithTitle("Define: " + word) + .WithDescription(definition) + .WithFooter(efb => efb.WithText(sense.Gramatical_info?.type)); + + if (sense.Examples != null) + embed.AddField(efb => efb.WithName("Example").WithValue(sense.Examples.First().text)); + + await channel.EmbedAsync(embed.Build()).ConfigureAwait(false); + } + } + [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task Hashtag(IUserMessage umsg, [Remainder] string query = null) diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index b95a917b..35bfb686 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -2003,6 +2003,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to define def. + /// + public static string define_cmd { + get { + return ResourceManager.GetString("define_cmd", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Finds a definition of a word.. + /// + public static string define_desc { + get { + return ResourceManager.GetString("define_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `{0}def heresy`. + /// + public static string define_usage { + get { + return ResourceManager.GetString("define_usage", resourceCulture); + } + } + /// /// Looks up a localized string similar to defvol dv. /// diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index 69333618..ee6f9a5a 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -2826,4 +2826,13 @@ `{0}fp` + + define def + + + Finds a definition of a word. + + + `{0}def heresy` + \ No newline at end of file