Implementing rate limiting for discord bot interactions with absolute expiration of items
I’m trying to create a simple and customizable
I'm too confused about all the possibilities. So, how would you implement this? What decisions would you make and what data structures would you use?
/ping
— Pong!
/ping
— Pong!
/ping
— Wait 60 seconds for limit to expire!
/ping
— Wait 30 seconds for limit to expire!
…
— Remove rate limit from store in background.
/ping
— Pong!
RateLimitAttribute for interactions in Discord.Net using their preconditions.I'm too confused about all the possibilities. So, how would you implement this? What decisions would you make and what data structures would you use?
Expected usage
Expected behavior
/ping
— Pong!
(count is now 1)/ping
— Pong!
(count is now 2)/ping
— Wait 60 seconds for limit to expire!
/ping
(30 seconds later)— Wait 30 seconds for limit to expire!
…
(30 seconds later)— Remove rate limit from store in background.
/ping
(any time later)— Pong!
(count is now 1)Wanted features:
- Users can define custom ignore policies: for instance, if you have a database with different roles like admins and regular users, you might want to apply rate limiting to all users but exclude admins.
- Users can access the store of rate limits: for example, EPIC RPG’s
/cdcommand shows the cooldowns for each command per user.
- Attributes require constant values:
TimeSpancannot be used directly. Instead, people use parameters specifying periods in seconds. Alternatively, a converter could be used withdouble periodandenum TimeMeasure. - Caching with absolute expiration:
IMemoryCache'sAbsoluteExpirationsimplifies caching implementation because it removes the rate limits right when they expire, but the keys there are strings and not generic types. UsingSystem.Threading.RateLimiting, it’s hard to display the exact remaining time until the limit expires.