~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]
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]

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>
/// Looks up a localized string similar to iam.
/// </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>
/// Looks up a localized string similar to imdb omdb.
/// </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>
/// Looks up a localized string similar to jcsc.
/// </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>
/// Looks up a localized string similar to remind.
/// </summary>

View File

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

View File

@ -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;