Example #1
0
        public void ShowImageResultEntry(Google.Apis.Customsearch.v1.Data.Result result)
        {
            PictureBox pic = new PictureBox();

            pic.ImageLocation = result.Link;
            pic.SizeMode      = PictureBoxSizeMode.Zoom;
            pic.Height        = picturesHeight;

            double ar = (double)result.Image.Width / (double)result.Image.Height;

            pic.Width = (int)Math.Round(pic.Height * ar);

            pic.Cursor = Cursors.Hand;

            pic.Click += Pic_Click;
            flowLayoutPanel1.Controls.Add(pic);
        }
Example #2
0
 public static async Task Image(SocketMessage message, string[] arg, string msg)
 {
     if (arg.Count() > 1)
     {
         try
         {
             Google.Apis.Customsearch.v1.Data.Result r = GoogleHelper.SearchImage(msg.Substring(2), !message.Channel.IsNsfw);
             await message.Channel.SendMessageAsync(r.Link);
         }
         catch
         {
             await message.Channel.SendMessageAsync($":no_entry: `No results found`");
         }
     }
     else
     {
         await message.Channel.SendMessageAsync($":no_entry: `Please enter a search term`");
     }
 }
Example #3
0
 public static async Task Google(SocketMessage message, string[] arg, string msg)
 {
     if (arg.Count() > 1)
     {
         try
         {
             Google.Apis.Customsearch.v1.Data.Result r = GoogleHelper.Search(msg.Substring(2), !message.Channel.IsNsfw);
             await message.Channel.SendMessageAsync("", false, GenGoogleEmbed(r.Title, r.Link, r.Snippet));
         }
         catch
         {
             await message.Channel.SendMessageAsync($":no_entry: `No results found`");
         }
     }
     else
     {
         await message.Channel.SendMessageAsync($":white_check_mark: `Please enter a search term`");
     }
 }
        [Description("Type the name of an existing item and it will show up with a link in chat.")] // this will be displayed to tell users what this command does when they invoke help
        public async Task Linkerator(CommandContext context, [Description("The item you want to link.")] string item)
        {
            // let's trigger a typing indicator to let users know we're working
            await context.TriggerTypingAsync();

            // Use the default configuration for AngleSharp
            var config = Configuration.Default;

            // Create a new context for evaluating webpages with the given config
            var browseContext = BrowsingContext.New(config);

            string apiKey             = Environment.GetEnvironmentVariable("GOOGLE_API_KEY");
            string customSearchEngine = Environment.GetEnvironmentVariable("SEARCH_ENGINE_KEY");
            var    svc = new Google.Apis.Customsearch.v1.CustomsearchService(new Google.Apis.Services.BaseClientService.Initializer {
                ApiKey = apiKey
            });
            var listRequest = svc.Cse.List(item);

            listRequest.Cx  = customSearchEngine;
            listRequest.Num = 5; // get first 5 search results
            var searchItems = listRequest.Execute();

            // wrap it into an embed
            var embed = new DiscordEmbedBuilder();

            embed.WithFooter($"Search results for {item}...");

            bool itemFound  = false;
            var  searchItem = new Google.Apis.Customsearch.v1.Data.Result();

            if (searchItems.Items != null)
            {
                foreach (var currentItem in searchItems.Items)
                {
                    if (currentItem.Title.Contains("Item") && !itemFound)
                    {
                        itemFound  = true;
                        searchItem = currentItem;
                    }
                }
            }

            if (itemFound) // got a result, and it's an item!
            {
                embed.WithTitle(searchItem.Title.Replace(" - Item - World of Warcraft", ""));
                embed.WithDescription(searchItem.Snippet);
                embed.WithUrl(searchItem.Link);

                // Source to be parsed
                string response = await client.GetStringAsync(searchItem.Link);

                var document = await browseContext.OpenAsync(req => req.Content(response));

                // all <link> tags
                var links = document.QuerySelectorAll("link");
                foreach (var link in links)
                {
                    // looking for <link rel="image_src" href="item icon image">
                    if (link.HasAttribute("rel") && link.GetAttribute("rel").Contains("image_src") &&
                        link.HasAttribute("href"))
                    {
                        embed.WithThumbnailUrl(link.GetAttribute("href"));
                    }
                }
            }
            else // not an item
            {
                DiscordEmoji searchEmoji = DiscordEmoji.FromName(context.Client, ":grimacing:");
                embed.WithTitle($"{searchEmoji} Couldn't find a page for this item, is it possible you misspelled it or it isn't an item from Classic WoW?");
            }

            // respond with content
            await context.RespondAsync(embed : embed);
        }