nsfw perf improvements. ~hentaibomb has 5sec cooldown. events trigger in parallel

This commit is contained in:
Kwoth 2017-01-15 13:45:14 +01:00
parent 33687d8e39
commit 9052d96f22
4 changed files with 63 additions and 48 deletions

@ -1 +1 @@
Subproject commit 9155ac02ee245193825aa307fe43ed25eac9a45c Subproject commit e9dca6c648b23bd9e957d8f9eee516df6ce11091

View File

@ -37,8 +37,7 @@ namespace NadekoBot.Modules.Administration
static MuteCommands() static MuteCommands()
{ {
var _log = LogManager.GetCurrentClassLogger(); var _log = LogManager.GetCurrentClassLogger();
var sw = Stopwatch.StartNew();
var configs = NadekoBot.AllGuildConfigs; var configs = NadekoBot.AllGuildConfigs;
GuildMuteRoles = new ConcurrentDictionary<ulong, string>(configs GuildMuteRoles = new ConcurrentDictionary<ulong, string>(configs
.Where(c => !string.IsNullOrWhiteSpace(c.MuteRoleName)) .Where(c => !string.IsNullOrWhiteSpace(c.MuteRoleName))
@ -50,9 +49,6 @@ namespace NadekoBot.Modules.Administration
)); ));
NadekoBot.Client.UserJoined += Client_UserJoined; NadekoBot.Client.UserJoined += Client_UserJoined;
sw.Stop();
_log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s");
} }
private static async Task Client_UserJoined(IGuildUser usr) private static async Task Client_UserJoined(IGuildUser usr)

View File

@ -20,6 +20,7 @@ namespace NadekoBot.Modules.NSFW
public class NSFW : DiscordModule public class NSFW : DiscordModule
{ {
private static ConcurrentDictionary<ulong, Timer> AutoHentaiTimers { get; } = new ConcurrentDictionary<ulong, Timer>(); private static ConcurrentDictionary<ulong, Timer> AutoHentaiTimers { get; } = new ConcurrentDictionary<ulong, Timer>();
private static ConcurrentHashSet<ulong> _hentaiBombBlacklist { get; } = new ConcurrentHashSet<ulong>();
private async Task InternalHentai(IMessageChannel channel, string tag, bool noError) private async Task InternalHentai(IMessageChannel channel, string tag, bool noError)
{ {
@ -56,7 +57,8 @@ namespace NadekoBot.Modules.NSFW
await channel.EmbedAsync(new EmbedBuilder().WithOkColor() await channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithImageUrl(link) .WithImageUrl(link)
.WithDescription("Tag: " + tag)).ConfigureAwait(false); .WithDescription("Tag: " + tag))
.ConfigureAwait(false);
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -90,7 +92,7 @@ namespace NadekoBot.Modules.NSFW
if (tagsArr == null || tagsArr.Length == 0) if (tagsArr == null || tagsArr.Length == 0)
await InternalHentai(Context.Channel, null, true).ConfigureAwait(false); await InternalHentai(Context.Channel, null, true).ConfigureAwait(false);
else else
await InternalHentai(Context.Channel, tagsArr[new NadekoRandom().Next(0, tagsArr.Length)], true); await InternalHentai(Context.Channel, tagsArr[new NadekoRandom().Next(0, tagsArr.Length)], true).ConfigureAwait(false);
} }
catch { } catch { }
}, null, interval * 1000, interval * 1000); }, null, interval * 1000, interval * 1000);
@ -101,29 +103,39 @@ namespace NadekoBot.Modules.NSFW
return t; return t;
}); });
await Context.Channel.SendConfirmAsync($"Autohentai started. Reposting every {interval}s with one of the following tags:\n{string.Join(", ", tagsArr)}").ConfigureAwait(false); 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] [NadekoCommand, Usage, Description, Aliases]
public async Task HentaiBomb([Remainder] string tag = null) public async Task HentaiBomb([Remainder] string tag = null)
{ {
tag = tag?.Trim() ?? ""; if (!_hentaiBombBlacklist.Add(Context.User.Id))
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; return;
} try
{
tag = tag?.Trim() ?? "";
tag = "rating%3Aexplicit+" + tag;
await Context.Channel.SendMessageAsync(String.Join("\n\n", linksEnum)).ConfigureAwait(false); 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);
}
} }
@ -135,12 +147,13 @@ namespace NadekoBot.Modules.NSFW
var url = await GetDanbooruImageLink(tag).ConfigureAwait(false); var url = await GetDanbooruImageLink(tag).ConfigureAwait(false);
if (url == null) if (url == null)
await Context.Channel.SendErrorAsync(Context.User.Mention + " No results."); await Context.Channel.SendErrorAsync(Context.User.Mention + " No results.").ConfigureAwait(false);
else else
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithDescription(Context.User.Mention + " " + tag) .WithDescription(Context.User.Mention + " " + tag)
.WithImageUrl(url) .WithImageUrl(url)
.WithFooter(efb => efb.WithText("Danbooru"))).ConfigureAwait(false); .WithFooter(efb => efb.WithText("Danbooru")))
.ConfigureAwait(false);
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -172,7 +185,8 @@ namespace NadekoBot.Modules.NSFW
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithDescription(Context.User.Mention + " " + tag) .WithDescription(Context.User.Mention + " " + tag)
.WithImageUrl(url) .WithImageUrl(url)
.WithFooter(efb => efb.WithText("e621"))).ConfigureAwait(false); .WithFooter(efb => efb.WithText("e621")))
.ConfigureAwait(false);
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -217,14 +231,14 @@ namespace NadekoBot.Modules.NSFW
} }
} }
public static async Task<string> GetDanbooruImageLink(string tag) public static Task<string> GetDanbooruImageLink(string tag) => Task.Run(async () =>
{ {
try try
{ {
using (var http = new HttpClient()) using (var http = new HttpClient())
{ {
http.AddFakeHeaders(); http.AddFakeHeaders();
var data = await http.GetStreamAsync("https://danbooru.donmai.us/posts.xml?limit=100&tags=" + tag); var data = await http.GetStreamAsync("https://danbooru.donmai.us/posts.xml?limit=100&tags=" + tag).ConfigureAwait(false);
var doc = new XmlDocument(); var doc = new XmlDocument();
doc.Load(data); doc.Load(data);
var nodes = doc.GetElementsByTagName("file-url"); var nodes = doc.GetElementsByTagName("file-url");
@ -237,17 +251,17 @@ namespace NadekoBot.Modules.NSFW
{ {
return null; return null;
} }
} });
public static async Task<string> GetE621ImageLink(string tag) public static Task<string> GetE621ImageLink(string tag) => Task.Run(async () =>
{ {
try try
{ {
using (var http = new HttpClient()) using (var http = new HttpClient())
{ {
http.AddFakeHeaders(); http.AddFakeHeaders();
var data = await http.GetStreamAsync("http://e621.net/post/index.xml?tags=" + tag); var data = await http.GetStreamAsync("http://e621.net/post/index.xml?tags=" + tag).ConfigureAwait(false);
var doc = new XmlDocument(); var doc = new XmlDocument();
doc.Load(data); doc.Load(data);
var nodes = doc.GetElementsByTagName("file_url"); var nodes = doc.GetElementsByTagName("file_url");
@ -260,7 +274,7 @@ namespace NadekoBot.Modules.NSFW
{ {
return null; return null;
} }
} });
public static Task<string> GetYandereImageLink(string tag) => public static Task<string> GetYandereImageLink(string tag) =>
Searches.Searches.InternalDapiSearch(tag, Searches.Searches.DapiSearchType.Yandere); Searches.Searches.InternalDapiSearch(tag, Searches.Searches.DapiSearchType.Yandere);

View File

@ -517,7 +517,8 @@ namespace NadekoBot.Modules.Searches
.WithAuthor(eab => eab.WithUrl(link) .WithAuthor(eab => eab.WithUrl(link)
.WithIconUrl("http://res.cloudinary.com/urbandictionary/image/upload/a_exif,c_fit,h_200,w_200/v1394975045/b8oszuu3tbq7ebyo7vo1.jpg") .WithIconUrl("http://res.cloudinary.com/urbandictionary/image/upload/a_exif,c_fit,h_200,w_200/v1394975045/b8oszuu3tbq7ebyo7vo1.jpg")
.WithName(query)) .WithName(query))
.WithDescription(desc)); .WithDescription(desc))
.ConfigureAwait(false);
} }
catch catch
{ {
@ -572,9 +573,9 @@ namespace NadekoBot.Modules.Searches
var result = await http.GetStringAsync("https://en.wikipedia.org//w/api.php?action=query&format=json&prop=info&redirects=1&formatversion=2&inprop=url&titles=" + Uri.EscapeDataString(query)); var result = await http.GetStringAsync("https://en.wikipedia.org//w/api.php?action=query&format=json&prop=info&redirects=1&formatversion=2&inprop=url&titles=" + Uri.EscapeDataString(query));
var data = JsonConvert.DeserializeObject<WikipediaApiModel>(result); var data = JsonConvert.DeserializeObject<WikipediaApiModel>(result);
if (data.Query.Pages[0].Missing) if (data.Query.Pages[0].Missing)
await Context.Channel.SendErrorAsync("That page could not be found."); await Context.Channel.SendErrorAsync("That page could not be found.").ConfigureAwait(false);
else else
await Context.Channel.SendMessageAsync(data.Query.Pages[0].FullUrl); await Context.Channel.SendMessageAsync(data.Query.Pages[0].FullUrl).ConfigureAwait(false);
} }
} }
@ -588,7 +589,7 @@ namespace NadekoBot.Modules.Searches
img.BackgroundColor(new ImageSharp.Color(color)); img.BackgroundColor(new ImageSharp.Color(color));
await Context.Channel.SendFileAsync(img.ToStream(), $"{color}.png"); await Context.Channel.SendFileAsync(img.ToStream(), $"{color}.png").ConfigureAwait(false); ;
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -642,7 +643,7 @@ namespace NadekoBot.Modules.Searches
var response = $@"`Title:` {found["title"].ToString()} var response = $@"`Title:` {found["title"].ToString()}
`Quality:` {found["quality"]} `Quality:` {found["quality"]}
`URL:` {await NadekoBot.Google.ShortenUrl(found["url"].ToString()).ConfigureAwait(false)}"; `URL:` {await NadekoBot.Google.ShortenUrl(found["url"].ToString()).ConfigureAwait(false)}";
await Context.Channel.SendMessageAsync(response); await Context.Channel.SendMessageAsync(response).ConfigureAwait(false);
} }
catch catch
{ {
@ -774,20 +775,24 @@ namespace NadekoBot.Modules.Searches
} }
try try
{ {
using (var http = new HttpClient()) var toReturn = await Task.Run(async () =>
{ {
http.AddFakeHeaders(); using (var http = new HttpClient())
var data = await http.GetStreamAsync(website); {
var doc = new XmlDocument(); http.AddFakeHeaders();
doc.Load(data); var data = await http.GetStreamAsync(website).ConfigureAwait(false);
var doc = new XmlDocument();
doc.Load(data);
var node = doc.LastChild.ChildNodes[new NadekoRandom().Next(0, doc.LastChild.ChildNodes.Count)]; var node = doc.LastChild.ChildNodes[new NadekoRandom().Next(0, doc.LastChild.ChildNodes.Count)];
var url = node.Attributes["file_url"].Value; var url = node.Attributes["file_url"].Value;
if (!url.StartsWith("http")) if (!url.StartsWith("http"))
url = "https:" + url; url = "https:" + url;
return url; return url;
} }
}).ConfigureAwait(false);
return toReturn;
} }
catch catch
{ {