.antispam is a bit smarter now, it will take messages from the last 30 minutes into consideration

This commit is contained in:
Master Kwoth 2017-05-16 16:46:03 +02:00
parent d4be914cde
commit cf9d58a7eb

View File

@ -10,6 +10,8 @@ using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
{ {
@ -35,30 +37,51 @@ namespace NadekoBot.Modules.Administration
= new ConcurrentDictionary<ulong, UserSpamStats>(); = new ConcurrentDictionary<ulong, UserSpamStats>();
} }
public class UserSpamStats public class UserSpamStats : IDisposable
{ {
public int Count { get; set; } public int Count => timers.Count;
public string LastMessage { get; set; } public string LastMessage { get; set; }
public UserSpamStats(string msg) private ConcurrentQueue<Timer> timers { get; }
{
Count = 1;
LastMessage = msg.ToUpperInvariant();
}
public UserSpamStats(IUserMessage msg)
{
LastMessage = msg.Content.ToUpperInvariant();
timers = new ConcurrentQueue<Timer>();
ApplyNextMessage(msg);
}
private readonly object applyLock = new object();
public void ApplyNextMessage(IUserMessage message) public void ApplyNextMessage(IUserMessage message)
{ {
var upperMsg = message.Content.ToUpperInvariant(); lock(applyLock){
if (upperMsg != LastMessage || (string.IsNullOrWhiteSpace(upperMsg) && message.Attachments.Any())) var upperMsg = message.Content.ToUpperInvariant();
{ if (upperMsg != LastMessage || (string.IsNullOrWhiteSpace(upperMsg) && message.Attachments.Any()))
LastMessage = upperMsg; {
Count = 0; LastMessage = upperMsg;
} //todo c#7
else Timer old;
{ while(timers.TryDequeue(out old))
Count++; old.Change(Timeout.Infinite, Timeout.Infinite);
}
var t = new Timer((_) => {
//todo c#7
Timer __;
if(timers.TryDequeue(out __))
__.Change(Timeout.Infinite, Timeout.Infinite);
}, null, TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(30));
timers.Enqueue(t);
} }
} }
public void Dispose()
{
//todo c#7
Timer old;
while(timers.TryDequeue(out old))
old.Change(Timeout.Infinite, Timeout.Infinite);
}
} }
[Group] [Group]
@ -112,7 +135,7 @@ namespace NadekoBot.Modules.Administration
})) }))
return; return;
var stats = spamSettings.UserStats.AddOrUpdate(msg.Author.Id, new UserSpamStats(msg.Content), var stats = spamSettings.UserStats.AddOrUpdate(msg.Author.Id, (id) => new UserSpamStats(msg),
(id, old) => (id, old) =>
{ {
old.ApplyNextMessage(msg); return old; old.ApplyNextMessage(msg); return old;
@ -122,6 +145,7 @@ namespace NadekoBot.Modules.Administration
{ {
if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats)) if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats))
{ {
stats.Dispose();
await PunishUsers(spamSettings.AntiSpamSettings.Action, ProtectionType.Spamming, (IGuildUser)msg.Author) await PunishUsers(spamSettings.AntiSpamSettings.Action, ProtectionType.Spamming, (IGuildUser)msg.Author)
.ConfigureAwait(false); .ConfigureAwait(false);
} }
@ -317,6 +341,7 @@ namespace NadekoBot.Modules.Administration
AntiSpamStats throwaway; AntiSpamStats throwaway;
if (_antiSpamGuilds.TryRemove(Context.Guild.Id, out throwaway)) if (_antiSpamGuilds.TryRemove(Context.Guild.Id, out throwaway))
{ {
throwaway.UserStats.ForEach(x => x.Value.Dispose());
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting) var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting)