ratelimited done, closes #45
This commit is contained in:
parent
ed0fc7fd9d
commit
707418fc46
53
NadekoBot/Commands/RatelimitCommand.cs
Normal file
53
NadekoBot/Commands/RatelimitCommand.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using Discord.Commands;
|
||||
|
||||
namespace NadekoBot.Commands {
|
||||
internal class RatelimitCommand : IDiscordCommand {
|
||||
|
||||
public static ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, DateTime>> RatelimitingChannels = new ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, DateTime>>();
|
||||
|
||||
private static readonly TimeSpan ratelimitTime = new TimeSpan(0, 0, 0, 5);
|
||||
|
||||
public RatelimitCommand() {
|
||||
NadekoBot.Client.MessageReceived += async (s, e) => {
|
||||
if (e.Channel.IsPrivate)
|
||||
return;
|
||||
ConcurrentDictionary<ulong, DateTime> userTimePair;
|
||||
if (!RatelimitingChannels.TryGetValue(e.Channel.Id, out userTimePair)) return;
|
||||
DateTime lastMessageTime;
|
||||
if (userTimePair.TryGetValue(e.User.Id, out lastMessageTime)) {
|
||||
if (DateTime.Now - lastMessageTime < ratelimitTime) {
|
||||
try {
|
||||
await e.Message.Delete();
|
||||
} catch { }
|
||||
return;
|
||||
}
|
||||
}
|
||||
userTimePair.AddOrUpdate(e.User.Id, id => DateTime.Now, (id, dt) => DateTime.Now);
|
||||
};
|
||||
}
|
||||
|
||||
public void Init(CommandGroupBuilder cgb) {
|
||||
cgb.CreateCommand(".slowmode")
|
||||
.Description("Toggles slow mode. When ON, users will be able to send only 1 message every 5 seconds.")
|
||||
.Parameter("minutes", ParameterType.Optional)
|
||||
.Do(async e => {
|
||||
//var minutesStr = e.GetArg("minutes");
|
||||
//if (string.IsNullOrWhiteSpace(minutesStr)) {
|
||||
// RatelimitingChannels.Remove(e.Channel.Id);
|
||||
// return;
|
||||
//}
|
||||
ConcurrentDictionary<ulong, DateTime> throwaway;
|
||||
if (RatelimitingChannels.TryRemove(e.Channel.Id, out throwaway)) {
|
||||
await e.Channel.SendMessage("Slow mode disabled.");
|
||||
return;
|
||||
}
|
||||
if (RatelimitingChannels.TryAdd(e.Channel.Id, new ConcurrentDictionary<ulong, DateTime>())) {
|
||||
await e.Channel.SendMessage("Slow mode initiated. " +
|
||||
"Users can't send more than 1 message every 5 seconds.");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ namespace NadekoBot.Modules {
|
||||
commands.Add(new ServerGreetCommand());
|
||||
commands.Add(new LogCommand());
|
||||
commands.Add(new PlayingRotate());
|
||||
commands.Add(new Commands.RatelimitCommand());
|
||||
}
|
||||
|
||||
public override string Prefix { get; } = ".";
|
||||
@ -195,7 +196,7 @@ namespace NadekoBot.Modules {
|
||||
|
||||
cgb.CreateCommand(".k").Alias(".kick")
|
||||
.Parameter("user")
|
||||
.Parameter("msg",ParameterType.Unparsed)
|
||||
.Parameter("msg", ParameterType.Unparsed)
|
||||
.Description("Kicks a mentioned user.")
|
||||
.Do(async e => {
|
||||
var msg = e.GetArg("msg");
|
||||
|
@ -171,6 +171,7 @@
|
||||
<Compile Include="Commands\PollCommand.cs" />
|
||||
<Compile Include="Modules\NSFW.cs" />
|
||||
<Compile Include="Modules\Permissions.cs" />
|
||||
<Compile Include="Commands\RatelimitCommand.cs" />
|
||||
<Compile Include="Modules\Searches.cs" />
|
||||
<Compile Include="Modules\Trello.cs" />
|
||||
<Compile Include="NadekoBot.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user