示例#1
0
        public async Task ExecuteGroupAsync(CommandContext ctx,
                                            [RemainingText, Description("Query.")] string query)
        {
            UrbanDictData data = await UrbanDictService.GetDefinitionForTermAsync(query);

            if (data == null)
            {
                await this.InformFailureAsync(ctx, "No results found!");

                return;
            }

            await ctx.SendCollectionInPagesAsync(
                $"Urban Dictionary search results for \"{query}\"",
                data.List,
                res => {
                var sb = new StringBuilder("Definition by ");
                sb.Append(Formatter.Bold(res.Author)).AppendLine().AppendLine();
                sb.Append(Formatter.Bold(res.Word)).Append(" :");
                sb.AppendLine(Formatter.BlockCode(res.Definition.Trim().Truncate(1000)));
                if (!string.IsNullOrWhiteSpace(res.Example))
                {
                    sb.Append("Examples:").AppendLine(Formatter.BlockCode(res.Example.Trim().Truncate(250)));
                }
                sb.Append(res.Permalink);
                return(sb.ToString());
            },
                this.ModuleColor,
                1
                );
        }
        public static async Task <UrbanDictData> GetDefinitionForTermAsync(string query)
        {
            if (string.IsNullOrWhiteSpace(query))
            {
                throw new ArgumentException("Query missing", nameof(query));
            }

            string result = await _http.GetStringAsync($"{_url}/define?term={WebUtility.UrlEncode(query)}").ConfigureAwait(false);

            UrbanDictData data = JsonConvert.DeserializeObject <UrbanDictData>(result);

            if (data.ResultType == "no_results" || !data.List.Any())
            {
                return(null);
            }

            foreach (UrbanDictList res in data.List)
            {
                res.Definition = new string(res.Definition.ToCharArray().Where(c => c != ']' && c != '[').ToArray());
                if (!string.IsNullOrWhiteSpace(res.Example))
                {
                    res.Example = new string(res.Example.ToCharArray().Where(c => c != ']' && c != '[').ToArray());
                }
            }

            return(data);
        }
示例#3
0
        public async Task ExecuteGroupAsync(CommandContext ctx,
                                            [RemainingText, Description("Query.")] string query)
        {
            UrbanDictData data = await UrbanDictService.GetDefinitionForTermAsync(query);

            if (data is null)
            {
                await this.InformFailureAsync(ctx, "No results found!");

                return;
            }

            await ctx.SendCollectionInPagesAsync(
                $"Urban Dictionary search results for \"{query}\"",
                data.List,
                res => res.ToInfoString(),
                this.ModuleColor,
                1
                );
        }