fixd prune occasional bug #428

This commit is contained in:
Kwoth 2016-07-29 13:53:03 +02:00
parent 994d514353
commit 3b2df8c6bc
2 changed files with 9 additions and 13 deletions

View File

@ -25,16 +25,13 @@ namespace NadekoBot.Classes
{ {
private static DateTime lastRefreshed = DateTime.MinValue; private static DateTime lastRefreshed = DateTime.MinValue;
private static string token { get; set; } = ""; private static string token { get; set; } = "";
private static readonly HttpClient httpClient = new HttpClient();
public static async Task<Stream> GetResponseStreamAsync(string url, public static async Task<Stream> GetResponseStreamAsync(string url,
IEnumerable<KeyValuePair<string, string>> headers = null, RequestHttpMethod method = RequestHttpMethod.Get) IEnumerable<KeyValuePair<string, string>> headers = null, RequestHttpMethod method = RequestHttpMethod.Get)
{ {
if (string.IsNullOrWhiteSpace(url)) if (string.IsNullOrWhiteSpace(url))
throw new ArgumentNullException(nameof(url)); throw new ArgumentNullException(nameof(url));
//if its a post or there are no headers, use static httpclient var cl = new HttpClient();
// if there are headers and it's get, it's not threadsafe
var cl = headers == null || method == RequestHttpMethod.Post ? httpClient : new HttpClient();
cl.DefaultRequestHeaders.Clear(); cl.DefaultRequestHeaders.Clear();
switch (method) switch (method)
{ {

View File

@ -634,14 +634,13 @@ namespace NadekoBot.Modules.Administration
.Parameter("num", ParameterType.Optional) .Parameter("num", ParameterType.Optional)
.Do(async e => .Do(async e =>
{ {
Message[] msgs;
if (string.IsNullOrWhiteSpace(e.GetArg("user_or_num"))) // if nothing is set, clear nadeko's messages, no permissions required if (string.IsNullOrWhiteSpace(e.GetArg("user_or_num"))) // if nothing is set, clear nadeko's messages, no permissions required
{ {
msgs = (await e.Channel.DownloadMessages(100).ConfigureAwait(false));//.Where(m => m.User.Id == e.Server.CurrentUser.Id).ToArray(); var msgs = (await e.Channel.DownloadMessages(100).ConfigureAwait(false)).Where(m => m.User?.Id == e.Server.CurrentUser.Id)?.ToArray();
msgs = msgs.Where(m => m.User.Id == e.Server.CurrentUser.Id).ToArray(); if (msgs == null || !msgs.Any())
if (!msgs.Any())
return; return;
await e.Channel.DeleteMessages(msgs).ConfigureAwait(false); var toDelete = msgs as Message[] ?? msgs.ToArray();
await e.Channel.DeleteMessages(toDelete).ConfigureAwait(false);
return; return;
} }
if (!e.User.GetPermissions(e.Channel).ManageMessages) if (!e.User.GetPermissions(e.Channel).ManageMessages)
@ -668,10 +667,10 @@ namespace NadekoBot.Modules.Administration
val = 100; val = 100;
if (!int.TryParse(e.GetArg("num"), out val)) if (!int.TryParse(e.GetArg("num"), out val))
val = 100; val = 100;
msgs = (await e.Channel.DownloadMessages(100).ConfigureAwait(false)).Where(m => m.User.Id == usr.Id).Take(val).ToArray(); var mesgs = (await e.Channel.DownloadMessages(100).ConfigureAwait(false)).Where(m => m.User?.Id == usr.Id).Take(val);
if (!msgs.Any()) if (mesgs == null || !mesgs.Any())
return; return;
await e.Channel.DeleteMessages(msgs).ConfigureAwait(false); await e.Channel.DeleteMessages(mesgs as Message[] ?? mesgs.ToArray()).ConfigureAwait(false);
}); });
cgb.CreateCommand(Prefix + "die") cgb.CreateCommand(Prefix + "die")