class DiscordMiddleware::RateLimiter

Overview

Middleware for performing rate limiting on message events. Rate limiting can be configured to be per-user, per-channel, or per-guild by passing a RateLimiterKey option.

If the client has a cache enabled, it will be used to resolve the guild to be rate limited on.

If message contains the substring "%time%" it will be replaced with the remaining time until the rate limit expires.

limiter = RateLimiter(UInt64).new

# Limit 3 events per second
limiter.bucket(:foo, 3_u32, 1.seconds)

middleware = DiscordMiddleware::RateLimiter.new(
  limiter,
  :foo,
  DiscordMiddleware::RateLimiterKey::ChannelID
  "Slow down! Try again in %time%."
)

client.on_message_create(middleware) do |payload, context|
  # Post memes, but not too quickly per-channel
end

Included Modules

Defined in:

discordcr-middleware/middleware/rate_limiter.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(limiter : ::RateLimiter(Discord::Snowflake), bucket : Symbol, key : RateLimiterKey = RateLimiterKey::UserID, message : String? = nil) #

[View source]

Instance Method Detail

def call(payload : Discord::Message, context : Discord::Context, &block) #

[View source]