.antispam is a bit smarter now, it will take messages from the last 30 minutes into consideration
This commit is contained in:
parent
d4be914cde
commit
cf9d58a7eb
@ -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,29 +37,50 @@ 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; }
|
||||||
|
|
||||||
|
public UserSpamStats(IUserMessage msg)
|
||||||
{
|
{
|
||||||
Count = 1;
|
LastMessage = msg.Content.ToUpperInvariant();
|
||||||
LastMessage = msg.ToUpperInvariant();
|
timers = new ConcurrentQueue<Timer>();
|
||||||
|
|
||||||
|
ApplyNextMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readonly object applyLock = new object();
|
||||||
public void ApplyNextMessage(IUserMessage message)
|
public void ApplyNextMessage(IUserMessage message)
|
||||||
{
|
{
|
||||||
|
lock(applyLock){
|
||||||
var upperMsg = message.Content.ToUpperInvariant();
|
var upperMsg = message.Content.ToUpperInvariant();
|
||||||
if (upperMsg != LastMessage || (string.IsNullOrWhiteSpace(upperMsg) && message.Attachments.Any()))
|
if (upperMsg != LastMessage || (string.IsNullOrWhiteSpace(upperMsg) && message.Attachments.Any()))
|
||||||
{
|
{
|
||||||
LastMessage = upperMsg;
|
LastMessage = upperMsg;
|
||||||
Count = 0;
|
//todo c#7
|
||||||
|
Timer old;
|
||||||
|
while(timers.TryDequeue(out old))
|
||||||
|
old.Change(Timeout.Infinite, Timeout.Infinite);
|
||||||
}
|
}
|
||||||
else
|
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()
|
||||||
{
|
{
|
||||||
Count++;
|
//todo c#7
|
||||||
}
|
Timer old;
|
||||||
|
while(timers.TryDequeue(out old))
|
||||||
|
old.Change(Timeout.Infinite, Timeout.Infinite);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user