mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Telegram.Bot.Types;
|
|
|
|
namespace Kruzya.TelegramBot.Core.Service;
|
|
|
|
public class MemoryAntiFloodService : IAntiFlood
|
|
{
|
|
private IDictionary<string, IList<DateTime>> _floodUsers;
|
|
private IDictionary<string, DateTime> _bannedUsers;
|
|
private int _floodStartsAfter = 3;
|
|
private TimeSpan _banningTimeSpan;
|
|
|
|
public MemoryAntiFloodService(IConfiguration configuration)
|
|
{
|
|
_floodUsers = new Dictionary<string, IList<DateTime>>();
|
|
_bannedUsers = new Dictionary<string, DateTime>();
|
|
_floodStartsAfter = configuration.GetValue<int>("FloodActions");
|
|
_banningTimeSpan = TimeSpan.FromMinutes(configuration.GetValue<double>("FloodBanTime"));
|
|
}
|
|
|
|
public bool IsUserFlooding(Chat chat, User user)
|
|
{
|
|
var dictKey = DictKey(chat, user);
|
|
if (_bannedUsers.TryGetValue(dictKey, out var expirationTime))
|
|
{
|
|
if (DateTime.UtcNow <= expirationTime)
|
|
{
|
|
expirationTime = DateTime.MinValue;
|
|
_bannedUsers.Remove(dictKey);
|
|
}
|
|
}
|
|
|
|
return (DateTime.UtcNow >= expirationTime);
|
|
}
|
|
|
|
public void RecordAction(Chat chat, User user)
|
|
{
|
|
var dictKey = DictKey(chat, user);
|
|
if (!_floodUsers.TryGetValue(dictKey, out var actionTimeList))
|
|
{
|
|
actionTimeList = new List<DateTime>();
|
|
}
|
|
|
|
actionTimeList.Add(DateTime.UtcNow);
|
|
actionTimeList = actionTimeList.Where(e => e.Subtract(_banningTimeSpan) <= DateTime.UtcNow).ToList();
|
|
_floodUsers[dictKey] = actionTimeList;
|
|
|
|
if (actionTimeList.Count > _floodStartsAfter)
|
|
{
|
|
actionTimeList.Clear();
|
|
_bannedUsers[dictKey] = DateTime.UtcNow.Add(_banningTimeSpan);
|
|
}
|
|
}
|
|
|
|
private string DictKey(Chat chat, User user)
|
|
{
|
|
return $"{chat.Id}-{user.Id}";
|
|
}
|
|
}
|