Beispiel #1
0
        public async Task SearchAsync([Summary("Search Term")][Remainder] string search)
        {
            var          rawSearch      = search;
            const string bootCamp       = "CSGO Level Design Boot Camp";
            const string v2             = "Hammer Tutorial V2 Series";
            const string baseYouTubeUrl = "https://www.youtube.com/watch?v=";
            var          numericStart   = new Regex(@"^\d+");
            var          getSingle      = false;
            var          match          = "";
            var          embed          = new EmbedBuilder();
            var          valid          = false;

            if (new[] { "bc ", "bootcamp " }.Any(x => search.StartsWith(x, StringComparison.OrdinalIgnoreCase)))
            {
                //Remove the search shortcut text
                search = search.Substring(search.IndexOf(' ') + 1);

                //Find out if the user is requesting a specific # video
                if (numericStart.IsMatch(search))
                {
                    //Manually build the search term to best match this series
                    search = $"{bootCamp} - Day {numericStart.Match(search)} - ";
                }
                else
                {
                    //Else just concat them
                    search = $"{bootCamp} {search}";
                }
                getSingle = true;
                match     = bootCamp;
            }
            else if (new[] { "v2 ", "version2 " }.Any(x => search.StartsWith(x, StringComparison.OrdinalIgnoreCase)))
            {
                //Remove the search shortcut text
                search = search.Substring(search.IndexOf(' ') + 1);

                //Find out if the user is requesting a specific # video
                if (numericStart.IsMatch(search))
                {
                    //Manually build the search term to best match this series
                    search = $"{v2} #{numericStart.Match(search)}";
                }
                else
                {
                    //Else just concat them
                    search = $"{v2} {search}";
                }
                getSingle = true;
                match     = v2;
            }

            if (getSingle)
            {
                //Only want a single video back since a series was specified.
                var result = await _youTube.GetOneYouTubeVideo(search, match);

                //Error in request
                if (result != null)
                {
                    //Build proper reply
                    embed.WithAuthor(HttpUtility.HtmlDecode(result.Snippet.Title), _dataService.Guild.IconUrl,
                                     $"{baseYouTubeUrl}{result.Id.VideoId}")
                    .WithThumbnailUrl(result.Snippet.Thumbnails.High.Url)
                    .WithDescription(HttpUtility.HtmlDecode(result.Snippet.Description))
                    .WithColor(new Color(255, 0, 0));
                    valid = true;
                }
            }
            else //Multiple replies
            {
                var results = await _youTube.YouTubeSearch(search, 3);

                embed.WithAuthor($"Search results for {rawSearch}", _dataService.Guild.IconUrl,
                                 "https://www.youtube.com/c/tophattwaffle")
                .WithColor(new Color(255, 0, 0));
                string description = null;
                foreach (var result in results.Items)
                {
                    description +=
                        $"**[{HttpUtility.HtmlDecode(result.Snippet.Title)}]({baseYouTubeUrl}{result.Id.VideoId})**\n" +
                        $"{result.Snippet.Description}\n\n";
                }

                if (string.IsNullOrEmpty(description))
                {
                    await ReplyAsync(embed : new EmbedBuilder().WithAuthor("You must provide a search term...")
                                     .WithColor(165, 55, 55).Build());

                    return;
                }

                embed.WithDescription(description.Trim());
                valid = true;
            }

            if (!valid)
            {
                //Build reply blaming YouTube
                embed.WithAuthor("No results found!", _dataService.Guild.IconUrl)
                .WithDescription($"I could not find anything results with your search term of `{rawSearch}`. " +
                                 "Try a different search term.\n" +
                                 "*If you keep seeing this, it is likely a [YouTube API quota limit](https://www.reddit.com/r/webdev/comments/aqou5b/youtube_api_v3_quota_issues/)*")
                .WithColor(new Color(255, 0, 0));
            }

            await ReplyAsync(embed : embed.Build());
        }