improvements to getresponse, prune and clr (performance/ratelimit)
This commit is contained in:
parent
95cc71b787
commit
62b2f0c869
@ -12,7 +12,7 @@ namespace NadekoBot
|
||||
{
|
||||
public class NadekoStats
|
||||
{
|
||||
public string BotVersion = "0.8-beta7";
|
||||
public string BotVersion = "0.8-beta8";
|
||||
|
||||
private static readonly NadekoStats _instance = new NadekoStats();
|
||||
public static NadekoStats Instance => _instance;
|
||||
|
@ -9,6 +9,7 @@ using NadekoBot.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
using NadekoBot.Commands;
|
||||
using System.IO;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace NadekoBot.Modules {
|
||||
class Administration : DiscordModule {
|
||||
@ -375,28 +376,37 @@ namespace NadekoBot.Modules {
|
||||
});
|
||||
|
||||
|
||||
ConcurrentDictionary<Server, bool> pruneDict = new ConcurrentDictionary<Server, bool>();
|
||||
cgb.CreateCommand(".prune")
|
||||
.Parameter("num", ParameterType.Required)
|
||||
.Description("Prunes a number of messages from the current channel.\n**Usage**: .prune 50")
|
||||
.Do(async e => {
|
||||
if (!e.User.ServerPermissions.ManageMessages) return;
|
||||
|
||||
if (pruneDict.ContainsKey(e.Server))
|
||||
return;
|
||||
int num;
|
||||
|
||||
if (!Int32.TryParse(e.GetArg("num"), out num) || num < 1) {
|
||||
await e.Send("Incorrect amount.");
|
||||
return;
|
||||
}
|
||||
pruneDict.TryAdd(e.Server, true);
|
||||
await Task.Factory.StartNew(async () => {
|
||||
try {
|
||||
Message last = null;
|
||||
while (num > 0) {
|
||||
var msgs = await e.Channel.DownloadMessages(num, last?.Id);
|
||||
last = msgs.LastOrDefault();
|
||||
msgs.ForEach(async m => await m.Delete());
|
||||
foreach (var m in msgs) {
|
||||
await m.Delete();
|
||||
await Task.Delay(500);
|
||||
}
|
||||
num -= 100;
|
||||
}
|
||||
} catch (Exception) { await e.Send("Failed pruning. Make sure the bot has correct permissions."); }
|
||||
}, TaskCreationOptions.LongRunning);
|
||||
|
||||
bool throwAway;
|
||||
pruneDict.TryRemove(e.Server, out throwAway);
|
||||
});
|
||||
|
||||
cgb.CreateCommand(".die")
|
||||
@ -412,13 +422,25 @@ namespace NadekoBot.Modules {
|
||||
}
|
||||
});
|
||||
|
||||
ConcurrentDictionary<Server, bool> clearDictionary = new ConcurrentDictionary<Server, bool>();
|
||||
cgb.CreateCommand(".clr")
|
||||
.Description("Clears some of nadeko's messages from the current channel.")
|
||||
.Do(async e => {
|
||||
try {
|
||||
if (clearDictionary.ContainsKey(e.Server))
|
||||
return;
|
||||
clearDictionary.TryAdd(e.Server, true);
|
||||
var msgs = await e.Channel.DownloadMessages(100);
|
||||
|
||||
msgs.Where(msg => msg.User.Id == client.CurrentUser.Id)
|
||||
.ForEach(async m => { try { await m.Delete(); } catch (Exception) { } });
|
||||
await Task.Run(async () => {
|
||||
var ms = msgs.Where(msg => msg.User.Id == client.CurrentUser.Id);
|
||||
foreach (var m in ms) {
|
||||
try { await m.Delete(); } catch (Exception) { }
|
||||
await Task.Delay(500);
|
||||
}
|
||||
});
|
||||
} catch (Exception) {}
|
||||
bool throwaway;
|
||||
clearDictionary.TryRemove(e.Server, out throwaway);
|
||||
});
|
||||
cgb.CreateCommand(".newname")
|
||||
.Description("Give the bot a new name.")
|
||||
|
@ -183,14 +183,14 @@ namespace NadekoBot.Modules {
|
||||
await e.Send($":anger: Error {ex}");
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
cgb.CreateCommand("~osu")
|
||||
.Description("desc")
|
||||
.Parameter("arg", ParameterType.Required)
|
||||
.Do(async e => {
|
||||
var arg = e.GetArg("arg");
|
||||
//make request to osu
|
||||
//print useful data
|
||||
var res = await GetResponseStream($"http://lemmmy.pw/osusig/sig.php?uname=kwoth&flagshadow&xpbar&xpbarhex&pp=2");
|
||||
await e.Channel.SendFile($"_{e.GetArg("arg")}.png", res);
|
||||
});
|
||||
|
||||
cgb.CreateCommand("~osubind")
|
||||
@ -209,11 +209,21 @@ namespace NadekoBot.Modules {
|
||||
//if exists save bind pair to parse.com
|
||||
//if not valid error
|
||||
});
|
||||
*/
|
||||
});
|
||||
}
|
||||
|
||||
public static async Task<Stream> GetResponseStream(string v) =>
|
||||
(await ((HttpWebRequest)WebRequest.Create(v)).GetResponseAsync()).GetResponseStream();
|
||||
public static async Task<Stream> GetResponseStream(string v) {
|
||||
var wr = (HttpWebRequest)WebRequest.Create(v);
|
||||
try {
|
||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||
wr.UserAgent = @"Mozilla/5.0 (Windows NT 6.2; Win64; x64)";
|
||||
return (await (wr).GetResponseAsync()).GetResponseStream();
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine("error in getresponse stream " + ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> GetResponseAsync(string v) =>
|
||||
await new StreamReader((await ((HttpWebRequest)WebRequest.Create(v)).GetResponseAsync()).GetResponseStream()).ReadToEndAsync();
|
||||
|
Loading…
Reference in New Issue
Block a user