diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index b919335c..643ea0e0 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -109,61 +109,75 @@ namespace NadekoBot.Modules.Searches } [NadekoCommand, Usage, Description, Aliases] - public async Task I([Remainder] string query = null) + public async Task Image([Remainder] string terms = null) { - if (string.IsNullOrWhiteSpace(query)) + terms = terms?.Trim(); + if (string.IsNullOrWhiteSpace(terms)) return; - try - { - using (var http = new HttpClient()) - { - var reqString = $"https://www.googleapis.com/customsearch/v1?q={Uri.EscapeDataString(query)}&cx=018084019232060951019%3Ahs5piey28-e&num=1&searchType=image&fields=items%2Flink&key={NadekoBot.Credentials.GoogleApiKey}"; - var obj = JObject.Parse(await http.GetStringAsync(reqString).ConfigureAwait(false)); - await Context.Channel.SendMessageAsync(obj["items"][0]["link"].ToString()).ConfigureAwait(false); - } - } - catch (HttpRequestException exception) - { - if (exception.Message.Contains("403 (Forbidden)")) - { - await Context.Channel.SendErrorAsync("Daily limit reached!"); - } - else - { - await Context.Channel.SendErrorAsync("Something went wrong."); - _log.Error(exception); - } - } + + terms = WebUtility.UrlEncode(terms).Replace(' ', '+'); + + var fullQueryLink = $"http://imgur.com/search?q={ terms }"; + var config = Configuration.Default.WithDefaultLoader(); + var document = await BrowsingContext.New(config).OpenAsync(fullQueryLink); + + var elems = document.QuerySelectorAll("a.image-list-link"); + + if (!elems.Any()) + return; + + var img = (elems.FirstOrDefault()?.Children?.FirstOrDefault() as IHtmlImageElement); + + if (img?.Source == null) + return; + + var source = img.Source.Replace("b.", "."); + + var embed = new EmbedBuilder() + .WithOkColor() + .WithAuthor(eab => eab.WithName("Image Search For: " + terms.TrimTo(50)) + .WithUrl(fullQueryLink) + .WithIconUrl("http://s.imgur.com/images/logo-1200-630.jpg?")) + .WithDescription(source) + .WithImageUrl(source) + .WithTitle(Context.User.Mention); + await Context.Channel.EmbedAsync(embed).ConfigureAwait(false); } [NadekoCommand, Usage, Description, Aliases] - public async Task Ir([Remainder] string query = null) + public async Task RandomImage([Remainder] string terms = null) { - if (string.IsNullOrWhiteSpace(query)) + terms = terms?.Trim(); + if (string.IsNullOrWhiteSpace(terms)) return; - try - { - using (var http = new HttpClient()) - { - var rng = new NadekoRandom(); - var reqString = $"https://www.googleapis.com/customsearch/v1?q={Uri.EscapeDataString(query)}&cx=018084019232060951019%3Ahs5piey28-e&num=1&searchType=image&start={ rng.Next(1, 50) }&fields=items%2Flink&key={NadekoBot.Credentials.GoogleApiKey}"; - var obj = JObject.Parse(await http.GetStringAsync(reqString).ConfigureAwait(false)); - var items = obj["items"] as JArray; - await Context.Channel.SendMessageAsync(items[0]["link"].ToString()).ConfigureAwait(false); - } - } - catch (HttpRequestException exception) - { - if (exception.Message.Contains("403 (Forbidden)")) - { - await Context.Channel.SendErrorAsync("Daily limit reached!"); - } - else - { - await Context.Channel.SendErrorAsync("Something went wrong."); - _log.Error(exception); - } - } + + terms = WebUtility.UrlEncode(terms).Replace(' ', '+'); + + var fullQueryLink = $"http://imgur.com/search?q={ terms }"; + var config = Configuration.Default.WithDefaultLoader(); + var document = await BrowsingContext.New(config).OpenAsync(fullQueryLink); + + var elems = document.QuerySelectorAll("a.image-list-link").ToList(); + + if (!elems.Any()) + return; + + var img = (elems.ElementAtOrDefault(new NadekoRandom().Next(0, elems.Count))?.Children?.FirstOrDefault() as IHtmlImageElement); + + if (img?.Source == null) + return; + + var source = img.Source.Replace("b.", "."); + + var embed = new EmbedBuilder() + .WithOkColor() + .WithAuthor(eab => eab.WithName("Image Search For: " + terms.TrimTo(50)) + .WithUrl(fullQueryLink) + .WithIconUrl("http://s.imgur.com/images/logo-1200-630.jpg?")) + .WithDescription(source) + .WithImageUrl(source) + .WithTitle(Context.User.Mention); + await Context.Channel.EmbedAsync(embed).ConfigureAwait(false); } [NadekoCommand, Usage, Description, Aliases] diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index 70259971..431ca335 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -3245,33 +3245,6 @@ namespace NadekoBot.Resources { } } - /// - /// Looks up a localized string similar to img i. - /// - public static string i_cmd { - get { - return ResourceManager.GetString("i_cmd", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Pulls the first image found using a search parameter. Use {0}ir for different results.. - /// - public static string i_desc { - get { - return ResourceManager.GetString("i_desc", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to `{0}i cute kitten`. - /// - public static string i_usage { - get { - return ResourceManager.GetString("i_usage", resourceCulture); - } - } - /// /// Looks up a localized string similar to iam. /// @@ -3326,6 +3299,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to image img. + /// + public static string image_cmd { + get { + return ResourceManager.GetString("image_cmd", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Pulls the first image found using a search parameter. Use {0}imgr for different results.. + /// + public static string image_desc { + get { + return ResourceManager.GetString("image_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `{0}img cute kitten`. + /// + public static string image_usage { + get { + return ResourceManager.GetString("image_usage", resourceCulture); + } + } + /// /// Looks up a localized string similar to imdb omdb. /// @@ -3380,33 +3380,6 @@ namespace NadekoBot.Resources { } } - /// - /// Looks up a localized string similar to ir. - /// - public static string ir_cmd { - get { - return ResourceManager.GetString("ir_cmd", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Pulls a random image using a search parameter.. - /// - public static string ir_desc { - get { - return ResourceManager.GetString("ir_desc", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to `{0}ir cute kitten`. - /// - public static string ir_usage { - get { - return ResourceManager.GetString("ir_usage", resourceCulture); - } - } - /// /// Looks up a localized string similar to jcsc. /// @@ -5405,6 +5378,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to randomimage rimg. + /// + public static string randomimage_cmd { + get { + return ResourceManager.GetString("randomimage_cmd", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Pulls a random image using a search parameter.. + /// + public static string randomimage_desc { + get { + return ResourceManager.GetString("randomimage_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `{0}rimg cute kitten`. + /// + public static string randomimage_usage { + get { + return ResourceManager.GetString("randomimage_usage", resourceCulture); + } + } + /// /// Looks up a localized string similar to remind. /// diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index fe484754..104128cb 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -1926,23 +1926,23 @@ `{0}woof` - - img i + + image img - - Pulls the first image found using a search parameter. Use {0}ir for different results. + + Pulls the first image found using a search parameter. Use {0}imgr for different results. - - `{0}i cute kitten` + + `{0}img cute kitten` - - ir + + randomimage rimg - + Pulls a random image using a search parameter. - - `{0}ir cute kitten` + + `{0}rimg cute kitten` lmgtfy diff --git a/src/NadekoBot/Services/CommandHandler.cs b/src/NadekoBot/Services/CommandHandler.cs index 9c899d8c..d99930b5 100644 --- a/src/NadekoBot/Services/CommandHandler.cs +++ b/src/NadekoBot/Services/CommandHandler.cs @@ -66,13 +66,12 @@ namespace NadekoBot.Services { try { - var usrMsg = msg as SocketUserMessage; if (usrMsg == null) return; - if (!usrMsg.IsAuthor()) - UserMessagesSent.AddOrUpdate(usrMsg.Author.Id, 1, (key, old) => ++old); + //if (!usrMsg.IsAuthor()) + // UserMessagesSent.AddOrUpdate(usrMsg.Author.Id, 1, (key, old) => ++old); if (msg.Author.IsBot || !NadekoBot.Ready) //no bots return;