Fix AutoRespond rate limit being applied incorrectly
And renamed RateLimitCache method to something that makes more sense.
This commit is contained in:
parent
042e98a022
commit
a8b4dfd4c3
2 changed files with 12 additions and 9 deletions
|
@ -10,12 +10,12 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
|
|||
{
|
||||
// Check filters
|
||||
if (def.Filter.IsFiltered(msg)) return;
|
||||
|
||||
// Check if the trigger is a match
|
||||
if (!def.Trigger.IsMatch(msg.Content)) return;
|
||||
|
||||
// Check rate limit
|
||||
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.RateLimit.AllowUsage(msg.Channel.Id)) return;
|
||||
|
||||
await Log($"'{def.Label}' triggered in #{msg.Channel.Name} by {msg.Author}");
|
||||
var (type, text) = def.Response;
|
||||
|
|
|
@ -25,18 +25,21 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
|
|||
}
|
||||
|
||||
/// <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"/>.
|
||||
/// </summary>
|
||||
/// <param name="id">The ID to add to the cache.</param>
|
||||
/// <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;
|
||||
|
||||
Clean();
|
||||
if (_cache.ContainsKey(id)) return false;
|
||||
_cache.Add(id, DateTime.Now);
|
||||
lock (this)
|
||||
{
|
||||
Clean();
|
||||
if (_cache.ContainsKey(id)) return false;
|
||||
_cache.Add(id, DateTime.Now);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue