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

View File

@ -20,6 +20,7 @@ namespace NadekoBot.Modules.NSFW
public class NSFW : DiscordModule
{
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)
{
@ -56,7 +57,8 @@ namespace NadekoBot.Modules.NSFW
await channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithImageUrl(link)
.WithDescription("Tag: " + tag)).ConfigureAwait(false);
.WithDescription("Tag: " + tag))
.ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
@ -90,7 +92,7 @@ namespace NadekoBot.Modules.NSFW
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);
await InternalHentai(Context.Channel, tagsArr[new NadekoRandom().Next(0, tagsArr.Length)], true).ConfigureAwait(false);
}
catch { }
}, null, interval * 1000, interval * 1000);
@ -101,29 +103,39 @@ namespace NadekoBot.Modules.NSFW
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]
public async Task HentaiBomb([Remainder] string tag = null)
{
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);
if (!_hentaiBombBlacklist.Add(Context.User.Id))
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);
if (url == null)
await Context.Channel.SendErrorAsync(Context.User.Mention + " No results.");
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);
.WithFooter(efb => efb.WithText("Danbooru")))
.ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
@ -172,7 +185,8 @@ namespace NadekoBot.Modules.NSFW
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithDescription(Context.User.Mention + " " + tag)
.WithImageUrl(url)
.WithFooter(efb => efb.WithText("e621"))).ConfigureAwait(false);
.WithFooter(efb => efb.WithText("e621")))
.ConfigureAwait(false);
}
[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
{
using (var http = new HttpClient())
{
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();
doc.Load(data);
var nodes = doc.GetElementsByTagName("file-url");
@ -237,17 +251,17 @@ namespace NadekoBot.Modules.NSFW
{
return null;
}
}
});
public static async Task<string> GetE621ImageLink(string tag)
public static Task<string> GetE621ImageLink(string tag) => Task.Run(async () =>
{
try
{
using (var http = new HttpClient())
{
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();
doc.Load(data);
var nodes = doc.GetElementsByTagName("file_url");
@ -260,7 +274,7 @@ namespace NadekoBot.Modules.NSFW
{
return null;
}
}
});
public static Task<string> GetYandereImageLink(string tag) =>
Searches.Searches.InternalDapiSearch(tag, Searches.Searches.DapiSearchType.Yandere);

View File

@ -517,7 +517,8 @@ namespace NadekoBot.Modules.Searches
.WithAuthor(eab => eab.WithUrl(link)
.WithIconUrl("http://res.cloudinary.com/urbandictionary/image/upload/a_exif,c_fit,h_200,w_200/v1394975045/b8oszuu3tbq7ebyo7vo1.jpg")
.WithName(query))
.WithDescription(desc));
.WithDescription(desc))
.ConfigureAwait(false);
}
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 data = JsonConvert.DeserializeObject<WikipediaApiModel>(result);
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
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));
await Context.Channel.SendFileAsync(img.ToStream(), $"{color}.png");
await Context.Channel.SendFileAsync(img.ToStream(), $"{color}.png").ConfigureAwait(false); ;
}
[NadekoCommand, Usage, Description, Aliases]
@ -642,7 +643,7 @@ namespace NadekoBot.Modules.Searches
var response = $@"`Title:` {found["title"].ToString()}
`Quality:` {found["quality"]}
`URL:` {await NadekoBot.Google.ShortenUrl(found["url"].ToString()).ConfigureAwait(false)}";
await Context.Channel.SendMessageAsync(response);
await Context.Channel.SendMessageAsync(response).ConfigureAwait(false);
}
catch
{
@ -774,20 +775,24 @@ namespace NadekoBot.Modules.Searches
}
try
{
using (var http = new HttpClient())
var toReturn = await Task.Run(async () =>
{
http.AddFakeHeaders();
var data = await http.GetStreamAsync(website);
var doc = new XmlDocument();
doc.Load(data);
using (var http = new HttpClient())
{
http.AddFakeHeaders();
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;
if (!url.StartsWith("http"))
url = "https:" + url;
return url;
}
var url = node.Attributes["file_url"].Value;
if (!url.StartsWith("http"))
url = "https:" + url;
return url;
}
}).ConfigureAwait(false);
return toReturn;
}
catch
{