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
|
// Check filters
|
||||||
if (def.Filter.IsFiltered(msg)) return;
|
if (def.Filter.IsFiltered(msg)) return;
|
||||||
|
|
||||||
|
// Check if the trigger is a match
|
||||||
|
if (!def.Trigger.IsMatch(msg.Content)) return;
|
||||||
|
|
||||||
// Check rate limit
|
// Check rate limit
|
||||||
if (!def.RateLimit.AddUsage(msg.Channel.Id)) return;
|
if (!def.RateLimit.AllowUsage(msg.Channel.Id)) return;
|
||||||
|
|
||||||
// Check if the trigger is a match, of course
|
|
||||||
if (!def.Trigger.IsMatch(msg.Content)) 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;
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue