diff --git a/NadekoBot/Commands/RatelimitCommand.cs b/NadekoBot/Commands/RatelimitCommand.cs new file mode 100644 index 00000000..902ed0a1 --- /dev/null +++ b/NadekoBot/Commands/RatelimitCommand.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Concurrent; +using Discord.Commands; + +namespace NadekoBot.Commands { + internal class RatelimitCommand : IDiscordCommand { + + public static ConcurrentDictionary> RatelimitingChannels = new ConcurrentDictionary>(); + + 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 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 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())) { + await e.Channel.SendMessage("Slow mode initiated. " + + "Users can't send more than 1 message every 5 seconds."); + } + }); + } + } +} \ No newline at end of file diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index 1dccc5e4..39f69afb 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -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"); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index e2ef4796..533bc20d 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -171,6 +171,7 @@ +