~img and ~rimg reworked

This commit is contained in:
Kwoth 2017-01-03 22:38:22 +01:00
parent 980a4a10d0
commit c7c555d010
4 changed files with 129 additions and 116 deletions

View File

@ -109,61 +109,75 @@ namespace NadekoBot.Modules.Searches
} }
[NadekoCommand, Usage, Description, Aliases] [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; return;
try
{ terms = WebUtility.UrlEncode(terms).Replace(' ', '+');
using (var http = new HttpClient())
{ var fullQueryLink = $"http://imgur.com/search?q={ terms }";
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 config = Configuration.Default.WithDefaultLoader();
var obj = JObject.Parse(await http.GetStringAsync(reqString).ConfigureAwait(false)); var document = await BrowsingContext.New(config).OpenAsync(fullQueryLink);
await Context.Channel.SendMessageAsync(obj["items"][0]["link"].ToString()).ConfigureAwait(false);
} var elems = document.QuerySelectorAll("a.image-list-link");
}
catch (HttpRequestException exception) if (!elems.Any())
{ return;
if (exception.Message.Contains("403 (Forbidden)"))
{ var img = (elems.FirstOrDefault()?.Children?.FirstOrDefault() as IHtmlImageElement);
await Context.Channel.SendErrorAsync("Daily limit reached!");
} if (img?.Source == null)
else return;
{
await Context.Channel.SendErrorAsync("Something went wrong."); var source = img.Source.Replace("b.", ".");
_log.Error(exception);
} 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] [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; return;
try
{ terms = WebUtility.UrlEncode(terms).Replace(' ', '+');
using (var http = new HttpClient())
{ var fullQueryLink = $"http://imgur.com/search?q={ terms }";
var rng = new NadekoRandom(); var config = Configuration.Default.WithDefaultLoader();
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 document = await BrowsingContext.New(config).OpenAsync(fullQueryLink);
var obj = JObject.Parse(await http.GetStringAsync(reqString).ConfigureAwait(false));
var items = obj["items"] as JArray; var elems = document.QuerySelectorAll("a.image-list-link").ToList();
await Context.Channel.SendMessageAsync(items[0]["link"].ToString()).ConfigureAwait(false);
} if (!elems.Any())
} return;
catch (HttpRequestException exception)
{ var img = (elems.ElementAtOrDefault(new NadekoRandom().Next(0, elems.Count))?.Children?.FirstOrDefault() as IHtmlImageElement);
if (exception.Message.Contains("403 (Forbidden)"))
{ if (img?.Source == null)
await Context.Channel.SendErrorAsync("Daily limit reached!"); return;
}
else var source = img.Source.Replace("b.", ".");
{
await Context.Channel.SendErrorAsync("Something went wrong."); var embed = new EmbedBuilder()
_log.Error(exception); .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] [NadekoCommand, Usage, Description, Aliases]

View File

@ -3245,33 +3245,6 @@ namespace NadekoBot.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to img i.
/// </summary>
public static string i_cmd {
get {
return ResourceManager.GetString("i_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Pulls the first image found using a search parameter. Use {0}ir for different results..
/// </summary>
public static string i_desc {
get {
return ResourceManager.GetString("i_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}i cute kitten`.
/// </summary>
public static string i_usage {
get {
return ResourceManager.GetString("i_usage", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to iam. /// Looks up a localized string similar to iam.
/// </summary> /// </summary>
@ -3326,6 +3299,33 @@ namespace NadekoBot.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to image img.
/// </summary>
public static string image_cmd {
get {
return ResourceManager.GetString("image_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Pulls the first image found using a search parameter. Use {0}imgr for different results..
/// </summary>
public static string image_desc {
get {
return ResourceManager.GetString("image_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}img cute kitten`.
/// </summary>
public static string image_usage {
get {
return ResourceManager.GetString("image_usage", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to imdb omdb. /// Looks up a localized string similar to imdb omdb.
/// </summary> /// </summary>
@ -3380,33 +3380,6 @@ namespace NadekoBot.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to ir.
/// </summary>
public static string ir_cmd {
get {
return ResourceManager.GetString("ir_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Pulls a random image using a search parameter..
/// </summary>
public static string ir_desc {
get {
return ResourceManager.GetString("ir_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}ir cute kitten`.
/// </summary>
public static string ir_usage {
get {
return ResourceManager.GetString("ir_usage", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to jcsc. /// Looks up a localized string similar to jcsc.
/// </summary> /// </summary>
@ -5405,6 +5378,33 @@ namespace NadekoBot.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to randomimage rimg.
/// </summary>
public static string randomimage_cmd {
get {
return ResourceManager.GetString("randomimage_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Pulls a random image using a search parameter..
/// </summary>
public static string randomimage_desc {
get {
return ResourceManager.GetString("randomimage_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}rimg cute kitten`.
/// </summary>
public static string randomimage_usage {
get {
return ResourceManager.GetString("randomimage_usage", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to remind. /// Looks up a localized string similar to remind.
/// </summary> /// </summary>

View File

@ -1926,23 +1926,23 @@
<data name="randomdog_usage" xml:space="preserve"> <data name="randomdog_usage" xml:space="preserve">
<value>`{0}woof`</value> <value>`{0}woof`</value>
</data> </data>
<data name="i_cmd" xml:space="preserve"> <data name="image_cmd" xml:space="preserve">
<value>img i</value> <value>image img</value>
</data> </data>
<data name="i_desc" xml:space="preserve"> <data name="image_desc" xml:space="preserve">
<value>Pulls the first image found using a search parameter. Use {0}ir for different results.</value> <value>Pulls the first image found using a search parameter. Use {0}imgr for different results.</value>
</data> </data>
<data name="i_usage" xml:space="preserve"> <data name="image_usage" xml:space="preserve">
<value>`{0}i cute kitten`</value> <value>`{0}img cute kitten`</value>
</data> </data>
<data name="ir_cmd" xml:space="preserve"> <data name="randomimage_cmd" xml:space="preserve">
<value>ir</value> <value>randomimage rimg</value>
</data> </data>
<data name="ir_desc" xml:space="preserve"> <data name="randomimage_desc" xml:space="preserve">
<value>Pulls a random image using a search parameter.</value> <value>Pulls a random image using a search parameter.</value>
</data> </data>
<data name="ir_usage" xml:space="preserve"> <data name="randomimage_usage" xml:space="preserve">
<value>`{0}ir cute kitten`</value> <value>`{0}rimg cute kitten`</value>
</data> </data>
<data name="lmgtfy_cmd" xml:space="preserve"> <data name="lmgtfy_cmd" xml:space="preserve">
<value>lmgtfy</value> <value>lmgtfy</value>

View File

@ -66,13 +66,12 @@ namespace NadekoBot.Services
{ {
try try
{ {
var usrMsg = msg as SocketUserMessage; var usrMsg = msg as SocketUserMessage;
if (usrMsg == null) if (usrMsg == null)
return; return;
if (!usrMsg.IsAuthor()) //if (!usrMsg.IsAuthor())
UserMessagesSent.AddOrUpdate(usrMsg.Author.Id, 1, (key, old) => ++old); // UserMessagesSent.AddOrUpdate(usrMsg.Author.Id, 1, (key, old) => ++old);
if (msg.Author.IsBot || !NadekoBot.Ready) //no bots if (msg.Author.IsBot || !NadekoBot.Ready) //no bots
return; return;