Beispiel #1
0
        public async Task UrbanDict([Leftover] string query)
        {
            await Context.Channel.TriggerTypingAsync().ConfigureAwait(false);

            query = HttpUtility.UrlEncode(query);
            using HttpClient http = _http.CreateClient();
            string response = await http.GetStringAsync(UrbanDictUrl + query)
                              .ConfigureAwait(false);

            try
            {
                UrbanModel[] items = response.Deserialize <UrbanResponse>().List;
                if (items.Any())
                {
                    await Context.SendPaginatedMessageAsync(0, p =>
                    {
                        UrbanModel item = items[p];
                        return(new EmbedBuilder().WithDynamicColor(Context)
                               .WithUrl(item.Permalink)
                               .WithAuthor(item.Word, "https://i.imgur.com/p1NqHdf.jpg")
                               .WithDescription(item.Definition));
                    }, items.Length, 1).ConfigureAwait(false);
                }
                else
                {
                    await Context.Channel.SendErrorAsync($"No results for `{query}`");
                }
            }
            catch (Exception e)
            {
                Log.Warn(e.Message);
            }
        }
Beispiel #2
0
        public async Task UrbanCmdAsync([Remainder] string query)
        {
            var jsonString = string.Empty;

            using (var client = new HttpClient())
            {
                var result = await client.GetAsync(string.Format("http://api.urbandictionary.com/v0/define?term={0}", query.Replace(' ', '+'))).ConfigureAwait(false);

                if (!result.IsSuccessStatusCode)
                {
                    await Context.MarkCmdFailedAsync($"UrbanDict API returned {result.StatusCode}").ConfigureAwait(false);

                    return;
                }

                jsonString = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
            }
            var urbanModel = UrbanModel.FromJson(jsonString);

            if (urbanModel?.List is null || urbanModel.List.Count == 0)
            {
                await ReplyAsync($"There are no definitions for word: {query.Bold()}.").ConfigureAwait(false);

                return;
            }

            var pages = new List <string>();

            foreach (var item in urbanModel.List.Where(x => !string.IsNullOrEmpty(x?.Definition) && !string.IsNullOrEmpty(x.Example)).OrderByDescending(x => x.ThumbsUp))
            {
                pages.Add(new StringBuilder()
                          .AppendLine(item !.Definition !.Replace("[", "").Replace("]", ""))
                          .AppendLine()
                          .AppendLine("Example:".Italics())
                          .AppendLine(item.Example !.Replace("[", "").Replace("]", ""))
                          .ToString());
            }
            var author = new EmbedAuthorBuilder()
                         .WithName("Urban Dictionary")
                         .WithIconUrl("https://d2gatte9o95jao.cloudfront.net/assets/apple-touch-icon-55f1ee4ebfd5444ef5f8d5ba836a2d41.png")
                         .WithUrl("https://urbandictionary.com");
            var msg = new PaginatedMessage()
            {
                Title = $"Definitions for {query.Italics()}", Author = author, Color = new Color(255, 84, 33), Pages = pages
            };

            await PagedReplyAsync(msg, false).ConfigureAwait(false);
        }