Allow longer rate limit

Also change some stuff to auto properties
This commit is contained in:
Noikoio 2019-02-02 20:04:19 -08:00
parent 762950062d
commit d6b822ff17
2 changed files with 15 additions and 21 deletions

View file

@ -15,28 +15,24 @@ namespace Noikoio.RegexBot.Module.AutoRespond
public enum ResponseType { None, Exec, Reply } public enum ResponseType { None, Exec, Reply }
private static Random ChangeRng = new Random(); private static Random ChangeRng = new Random();
string _label;
IEnumerable<Regex> _regex;
ResponseType _rtype; ResponseType _rtype;
string _rbody; string _rbody;
private FilterList _filter;
private RateLimitCache _limit;
private double _random; private double _random;
public string Label => _label; public string Label { get; }
public IEnumerable<Regex> Regex => _regex; public IEnumerable<Regex> Regex { get; }
public (ResponseType, string) Response => (_rtype, _rbody); public (ResponseType, string) Response => (_rtype, _rbody);
public FilterList Filter => _filter; public FilterList Filter { get; }
public RateLimitCache RateLimit => _limit; public RateLimitCache RateLimit { get; }
public double RandomChance => _random; public double RandomChance => _random;
public ConfigItem(JProperty definition) public ConfigItem(JProperty definition)
{ {
_label = definition.Name; Label = definition.Name;
var data = (JObject)definition.Value; var data = (JObject)definition.Value;
// error postfix string // error postfix string
string errorpfx = $" in response definition for '{_label}'."; string errorpfx = $" in response definition for '{Label}'.";
// regex trigger // regex trigger
const string NoRegexError = "No regular expression patterns are defined"; const string NoRegexError = "No regular expression patterns are defined";
@ -74,7 +70,7 @@ namespace Noikoio.RegexBot.Module.AutoRespond
$"Failed to parse regular expression pattern '{rxstr}'{errorpfx}"); $"Failed to parse regular expression pattern '{rxstr}'{errorpfx}");
} }
} }
_regex = regexes.ToArray(); Regex = regexes.ToArray();
// response - defined in either "exec" or "reply", but not both // response - defined in either "exec" or "reply", but not both
_rbody = null; _rbody = null;
@ -103,19 +99,19 @@ namespace Noikoio.RegexBot.Module.AutoRespond
// --- // ---
// whitelist/blacklist filtering // whitelist/blacklist filtering
_filter = new FilterList(data); Filter = new FilterList(data);
// rate limiting // rate limiting
string rlstr = data["ratelimit"]?.Value<string>(); string rlstr = data["ratelimit"]?.Value<string>();
if (string.IsNullOrWhiteSpace(rlstr)) if (string.IsNullOrWhiteSpace(rlstr))
{ {
_limit = new RateLimitCache(RateLimitCache.DefaultTimeout); RateLimit = new RateLimitCache(RateLimitCache.DefaultTimeout);
} }
else else
{ {
if (ushort.TryParse(rlstr, out var rlval)) if (uint.TryParse(rlstr, out var rlval))
{ {
_limit = new RateLimitCache(rlval); RateLimit = new RateLimitCache(rlval);
} }
else else
{ {

View file

@ -9,18 +9,16 @@ namespace Noikoio.RegexBot.Module.AutoRespond
class RateLimitCache class RateLimitCache
{ {
public const ushort DefaultTimeout = 20; // this is Skeeter's fault public const ushort DefaultTimeout = 20; // this is Skeeter's fault
private readonly ushort _timeout;
private Dictionary<ulong, DateTime> _cache; private Dictionary<ulong, DateTime> _cache;
public ushort Timeout => _timeout; public uint Timeout { get; }
/// <summary> /// <summary>
/// Sets up a new instance of <see cref="RateLimitCache"/>. /// Sets up a new instance of <see cref="RateLimitCache"/>.
/// </summary> /// </summary>
public RateLimitCache(ushort timeout) public RateLimitCache(uint timeout)
{ {
_timeout = timeout; Timeout = timeout;
_cache = new Dictionary<ulong, DateTime>(); _cache = new Dictionary<ulong, DateTime>();
} }
@ -32,7 +30,7 @@ namespace Noikoio.RegexBot.Module.AutoRespond
/// <returns>True on success. False if the given ID already exists.</returns> /// <returns>True on success. False if the given ID already exists.</returns>
public bool AllowUsage(ulong id) public bool AllowUsage(ulong id)
{ {
if (_timeout == 0) return true; if (Timeout == 0) return true;
lock (this) lock (this)
{ {