Fix AutoRespond rate limit being applied incorrectly

And renamed RateLimitCache method to something that makes more sense.
This commit is contained in:
Noikoio 2017-09-01 00:53:40 -07:00
parent 042e98a022
commit a8b4dfd4c3
2 changed files with 12 additions and 9 deletions

View file

@ -11,12 +11,12 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
// Check filters // Check filters
if (def.Filter.IsFiltered(msg)) return; if (def.Filter.IsFiltered(msg)) return;
// Check rate limit // Check if the trigger is a match
if (!def.RateLimit.AddUsage(msg.Channel.Id)) return;
// Check if the trigger is a match, of course
if (!def.Trigger.IsMatch(msg.Content)) return; if (!def.Trigger.IsMatch(msg.Content)) return;
// Check rate limit
if (!def.RateLimit.AllowUsage(msg.Channel.Id)) return;
await Log($"'{def.Label}' triggered in #{msg.Channel.Name} by {msg.Author}"); await Log($"'{def.Label}' triggered in #{msg.Channel.Name} by {msg.Author}");
var (type, text) = def.Response; var (type, text) = def.Response;
if (type == ResponseDefinition.ResponseType.Reply) await ProcessReply(msg, text); if (type == ResponseDefinition.ResponseType.Reply) await ProcessReply(msg, text);

View file

@ -25,18 +25,21 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
} }
/// <summary> /// <summary>
/// Adds a cache item corersponding to the given ID. /// Checks if a "usage" is allowed for the given value.
/// Items added to cache will be removed after the number of seconds specified in <see cref="Timeout"/>. /// Items added to cache will be removed after the number of seconds specified in <see cref="Timeout"/>.
/// </summary> /// </summary>
/// <param name="id">The ID to add to the cache.</param> /// <param name="id">The ID to add to the cache.</param>
/// <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 AddUsage(ulong id) public bool AllowUsage(ulong id)
{ {
if (_timeout == 0) return true; if (_timeout == 0) return true;
Clean(); lock (this)
if (_cache.ContainsKey(id)) return false; {
_cache.Add(id, DateTime.Now); Clean();
if (_cache.ContainsKey(id)) return false;
_cache.Add(id, DateTime.Now);
}
return true; return true;
} }