2016-08-15 14:57:40 +00:00
using Discord ;
using Discord.Commands ;
using NadekoBot.Attributes ;
using Newtonsoft.Json.Linq ;
using System ;
using System.Linq ;
using System.Threading.Tasks ;
using NadekoBot.Services ;
using System.Net.Http ;
using System.Text.RegularExpressions ;
2016-08-15 16:46:40 +00:00
using System.Xml.Linq ;
2016-08-21 16:00:03 +00:00
using NadekoBot.Extensions ;
2016-12-25 15:26:13 +00:00
using System.Xml ;
2016-12-29 13:56:15 +00:00
using System.Threading ;
using System.Collections.Concurrent ;
2016-08-15 14:57:40 +00:00
namespace NadekoBot.Modules.NSFW
{
2016-09-10 19:45:12 +00:00
[NadekoModule("NSFW", "~")]
2016-08-18 21:00:54 +00:00
public class NSFW : DiscordModule
2016-08-15 14:57:40 +00:00
{
2017-02-11 15:26:04 +00:00
2016-12-29 13:56:15 +00:00
private static ConcurrentDictionary < ulong , Timer > AutoHentaiTimers { get ; } = new ConcurrentDictionary < ulong , Timer > ( ) ;
2017-01-15 12:45:14 +00:00
private static ConcurrentHashSet < ulong > _hentaiBombBlacklist { get ; } = new ConcurrentHashSet < ulong > ( ) ;
2016-12-29 13:56:15 +00:00
2017-01-01 13:06:32 +00:00
private async Task InternalHentai ( IMessageChannel channel , string tag , bool noError )
2016-08-15 14:57:40 +00:00
{
tag = tag ? . Trim ( ) ? ? "" ;
2016-11-05 23:59:21 +00:00
tag = "rating%3Aexplicit+" + tag ;
var rng = new NadekoRandom ( ) ;
Task < string > provider = Task . FromResult ( "" ) ;
2016-12-25 15:26:13 +00:00
switch ( rng . Next ( 0 , 4 ) )
2016-11-05 23:59:21 +00:00
{
case 0 :
provider = GetDanbooruImageLink ( tag ) ;
break ;
case 1 :
provider = GetGelbooruImageLink ( tag ) ;
break ;
case 2 :
provider = GetKonachanImageLink ( tag ) ;
break ;
2016-11-18 15:36:54 +00:00
case 3 :
2016-11-14 21:34:11 +00:00
provider = GetYandereImageLink ( tag ) ;
break ;
2016-11-05 23:59:21 +00:00
default :
break ;
}
var link = await provider . ConfigureAwait ( false ) ;
if ( string . IsNullOrWhiteSpace ( link ) )
2017-01-03 15:10:46 +00:00
{
2017-01-08 13:53:14 +00:00
if ( ! noError )
2017-01-03 15:10:46 +00:00
await channel . SendErrorAsync ( "No results found." ) . ConfigureAwait ( false ) ;
return ;
}
await channel . EmbedAsync ( new EmbedBuilder ( ) . WithOkColor ( )
. WithImageUrl ( link )
2017-01-15 12:45:14 +00:00
. WithDescription ( "Tag: " + tag ) )
. ConfigureAwait ( false ) ;
2016-12-29 13:56:15 +00:00
}
2017-01-01 13:06:32 +00:00
[NadekoCommand, Usage, Description, Aliases]
public Task Hentai ( [ Remainder ] string tag = null ) = >
InternalHentai ( Context . Channel , tag , false ) ;
2017-02-11 15:26:04 +00:00
//[NadekoCommand, Usage, Description, Aliases]
//[RequireUserPermission(ChannelPermission.ManageMessages)]
//public async Task AutoHentai(int interval = 0, string tags = null)
//{
// Timer t;
// if (interval == 0)
// {
// if (AutoHentaiTimers.TryRemove(Context.Channel.Id, out t))
// {
// t.Change(Timeout.Infinite, Timeout.Infinite); //proper way to disable the timer
// await Context.Channel.SendConfirmAsync("Autohentai stopped.").ConfigureAwait(false);
// }
// return;
// }
// if (interval < 20)
// return;
// var tagsArr = tags?.Split('|');
// t = new Timer(async (state) =>
// {
// try
// {
// if (tagsArr == null || tagsArr.Length == 0)
// await InternalHentai(Context.Channel, null, true).ConfigureAwait(false);
// else
// await InternalHentai(Context.Channel, tagsArr[new NadekoRandom().Next(0, tagsArr.Length)], true).ConfigureAwait(false);
// }
// catch { }
// }, null, interval * 1000, interval * 1000);
// AutoHentaiTimers.AddOrUpdate(Context.Channel.Id, t, (key, old) =>
// {
// old.Change(Timeout.Infinite, Timeout.Infinite);
// return t;
// });
// await Context.Channel.SendConfirmAsync($"Autohentai started. Reposting every {interval}s with one of the following tags:\n{string.Join(", ", tagsArr)}")
// .ConfigureAwait(false);
//}
//[NadekoCommand, Usage, Description, Aliases]
//public async Task HentaiBomb([Remainder] string tag = null)
//{
// if (!_hentaiBombBlacklist.Add(Context.User.Id))
// return;
// try
// {
// tag = tag?.Trim() ?? "";
// tag = "rating%3Aexplicit+" + tag;
// var links = await Task.WhenAll(GetGelbooruImageLink(tag),
// GetDanbooruImageLink(tag),
// GetKonachanImageLink(tag),
// GetYandereImageLink(tag)).ConfigureAwait(false);
// var linksEnum = links?.Where(l => l != null);
// if (links == null || !linksEnum.Any())
// {
// await Context.Channel.SendErrorAsync("No results found.").ConfigureAwait(false);
// return;
// }
// await Context.Channel.SendMessageAsync(String.Join("\n\n", linksEnum)).ConfigureAwait(false);
// }
// finally {
// await Task.Delay(5000).ConfigureAwait(false);
// _hentaiBombBlacklist.TryRemove(Context.User.Id);
// }
//}
2016-11-14 21:34:11 +00:00
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2017-01-01 12:26:17 +00:00
public Task Yandere ( [ Remainder ] string tag = null )
= > Searches . Searches . InternalDapiCommand ( Context . Message , tag , Searches . Searches . DapiSearchType . Yandere ) ;
2016-08-15 14:57:40 +00:00
2016-12-25 15:26:13 +00:00
[NadekoCommand, Usage, Description, Aliases]
2017-01-01 12:26:17 +00:00
public Task Konachan ( [ Remainder ] string tag = null )
= > Searches . Searches . InternalDapiCommand ( Context . Message , tag , Searches . Searches . DapiSearchType . Konachan ) ;
2017-02-11 15:26:04 +00:00
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2017-01-01 12:26:17 +00:00
public async Task E621 ( [ Remainder ] string tag = null )
2016-08-15 14:57:40 +00:00
{
tag = tag ? . Trim ( ) ? ? "" ;
2016-12-25 15:26:13 +00:00
var url = await GetE621ImageLink ( tag ) . ConfigureAwait ( false ) ;
if ( url = = null )
2017-01-01 15:39:24 +00:00
await Context . Channel . SendErrorAsync ( Context . User . Mention + " No results." ) ;
2016-08-15 14:57:40 +00:00
else
2017-01-01 15:39:24 +00:00
await Context . Channel . EmbedAsync ( new EmbedBuilder ( ) . WithOkColor ( )
2017-01-01 12:26:17 +00:00
. WithDescription ( Context . User . Mention + " " + tag )
2016-12-25 15:26:13 +00:00
. WithImageUrl ( url )
2017-01-15 12:45:14 +00:00
. WithFooter ( efb = > efb . WithText ( "e621" ) ) )
. ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
2017-02-06 06:53:27 +00:00
[NadekoCommand, Usage, Description, Aliases]
public Task Rule34 ( [ Remainder ] string tag = null )
= > Searches . Searches . InternalDapiCommand ( Context . Message , tag , Searches . Searches . DapiSearchType . Rule34 ) ;
2017-02-02 03:58:39 +00:00
[NadekoCommand, Usage, Description, Aliases]
public async Task Danbooru ( [ Remainder ] string tag = null )
{
tag = tag ? . Trim ( ) ? ? "" ;
var url = await GetDanbooruImageLink ( tag ) . ConfigureAwait ( false ) ;
if ( url = = null )
await Context . Channel . SendErrorAsync ( Context . User . Mention + " No results." ) . ConfigureAwait ( false ) ;
else
await Context . Channel . EmbedAsync ( new EmbedBuilder ( ) . WithOkColor ( )
. WithDescription ( Context . User . Mention + " " + tag )
. WithImageUrl ( url )
. WithFooter ( efb = > efb . WithText ( "Danbooru" ) ) )
. ConfigureAwait ( false ) ;
}
2017-02-02 04:02:37 +00:00
public static Task < string > GetDanbooruImageLink ( string tag ) = > Task . Run ( async ( ) = >
{
try
{
using ( var http = new HttpClient ( ) )
{
http . AddFakeHeaders ( ) ;
var data = await http . GetStreamAsync ( "https://danbooru.donmai.us/posts.xml?limit=100&tags=" + tag ) . ConfigureAwait ( false ) ;
var doc = new XmlDocument ( ) ;
doc . Load ( data ) ;
var nodes = doc . GetElementsByTagName ( "file-url" ) ;
var node = nodes [ new NadekoRandom ( ) . Next ( 0 , nodes . Count ) ] ;
return "https://danbooru.donmai.us" + node . InnerText ;
}
}
catch
{
return null ;
}
} ) ;
2017-02-02 03:58:39 +00:00
[NadekoCommand, Usage, Description, Aliases]
public Task Gelbooru ( [ Remainder ] string tag = null )
= > Searches . Searches . InternalDapiCommand ( Context . Message , tag , Searches . Searches . DapiSearchType . Gelbooru ) ;
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2017-01-01 12:26:17 +00:00
public async Task Cp ( )
2016-08-15 14:57:40 +00:00
{
2017-01-01 12:26:17 +00:00
await Context . Channel . SendMessageAsync ( "http://i.imgur.com/MZkY1md.jpg" ) . ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2017-01-01 12:26:17 +00:00
public async Task Boobs ( )
2016-08-15 14:57:40 +00:00
{
try
{
JToken obj ;
using ( var http = new HttpClient ( ) )
{
2017-01-18 16:48:16 +00:00
obj = JArray . Parse ( await http . GetStringAsync ( $"http://api.oboobs.ru/boobs/{ new NadekoRandom().Next(0, 10330) }" ) . ConfigureAwait ( false ) ) [ 0 ] ;
2016-08-15 14:57:40 +00:00
}
2017-01-01 12:26:17 +00:00
await Context . Channel . SendMessageAsync ( $"http://media.oboobs.ru/{ obj[" preview "].ToString() }" ) . ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
catch ( Exception ex )
{
2017-01-01 12:26:17 +00:00
await Context . Channel . SendErrorAsync ( ex . Message ) . ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2017-01-01 12:26:17 +00:00
public async Task Butts ( )
2016-08-15 14:57:40 +00:00
{
try
{
JToken obj ;
using ( var http = new HttpClient ( ) )
{
2017-01-18 16:48:16 +00:00
obj = JArray . Parse ( await http . GetStringAsync ( $"http://api.obutts.ru/butts/{ new NadekoRandom().Next(0, 4335) }" ) . ConfigureAwait ( false ) ) [ 0 ] ;
2016-08-15 14:57:40 +00:00
}
2017-01-01 12:26:17 +00:00
await Context . Channel . SendMessageAsync ( $"http://media.obutts.ru/{ obj[" preview "].ToString() }" ) . ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
catch ( Exception ex )
{
2017-01-01 12:26:17 +00:00
await Context . Channel . SendErrorAsync ( ex . Message ) . ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
}
2017-01-15 12:45:14 +00:00
public static Task < string > GetE621ImageLink ( string tag ) = > Task . Run ( async ( ) = >
2016-08-15 14:57:40 +00:00
{
try
{
2016-08-15 16:46:40 +00:00
using ( var http = new HttpClient ( ) )
{
2016-08-21 16:00:03 +00:00
http . AddFakeHeaders ( ) ;
2017-01-15 12:45:14 +00:00
var data = await http . GetStreamAsync ( "http://e621.net/post/index.xml?tags=" + tag ) . ConfigureAwait ( false ) ;
2016-12-25 15:26:13 +00:00
var doc = new XmlDocument ( ) ;
doc . Load ( data ) ;
var nodes = doc . GetElementsByTagName ( "file_url" ) ;
var node = nodes [ new NadekoRandom ( ) . Next ( 0 , nodes . Count ) ] ;
return node . InnerText ;
2016-08-15 16:46:40 +00:00
}
2016-08-15 14:57:40 +00:00
}
2016-12-25 15:26:13 +00:00
catch
2016-08-15 14:57:40 +00:00
{
2016-12-25 15:26:13 +00:00
return null ;
2016-08-15 14:57:40 +00:00
}
2017-01-15 12:45:14 +00:00
} ) ;
2016-12-25 15:26:13 +00:00
2017-02-06 12:00:39 +00:00
public static Task < string > GetRule34ImageLink ( string tag ) = >
Searches . Searches . InternalDapiSearch ( tag , Searches . Searches . DapiSearchType . Rule34 ) ;
2016-12-25 15:26:13 +00:00
public static Task < string > GetYandereImageLink ( string tag ) = >
Searches . Searches . InternalDapiSearch ( tag , Searches . Searches . DapiSearchType . Yandere ) ;
public static Task < string > GetKonachanImageLink ( string tag ) = >
Searches . Searches . InternalDapiSearch ( tag , Searches . Searches . DapiSearchType . Konachan ) ;
public static Task < string > GetGelbooruImageLink ( string tag ) = >
Searches . Searches . InternalDapiSearch ( tag , Searches . Searches . DapiSearchType . Gelbooru ) ;
2017-01-22 20:20:23 +00:00
}
2016-12-25 15:26:13 +00:00
}