module Discordrb::Commands::RateLimiter

Represents a collection of {Bucket}s.

Public Instance Methods

bucket(key, attributes) click to toggle source

Defines a new bucket for this rate limiter. @param key [Symbol] The name for this new bucket. @param attributes [Hash] The attributes to initialize the bucket with. @option attributes [Integer] :limit The limit of requests to perform in the given time span. @option attributes [Integer] :time_span How many seconds until the limit should be reset. @option attributes [Integer] :delay How many seconds the user has to wait after each request. @see Bucket#initialize @return [Bucket] the created bucket.

# File lib/discordrb/commands/rate_limiter.rb, line 101
def bucket(key, attributes)
  @buckets ||= {}
  @buckets[key] = Bucket.new(attributes[:limit], attributes[:time_span], attributes[:delay])
end
clean() click to toggle source

Cleans all buckets @see Bucket#clean

# File lib/discordrb/commands/rate_limiter.rb, line 121
def clean
  @buckets.each(&:clean)
end
include_buckets(limiter) click to toggle source

Adds all the buckets from another RateLimiter onto this one. @param limiter [Module] Another {RateLimiter} module

# File lib/discordrb/commands/rate_limiter.rb, line 127
def include_buckets(limiter)
  buckets = limiter.instance_variable_get(:@buckets) || {}
  @buckets ||= {}
  @buckets.merge! buckets
end
rate_limited?(key, thing, increment: 1) click to toggle source

Performs a rate limit request. @param key [Symbol] Which bucket to perform the request for. @param thing [String, Integer, Symbol] What should be rate-limited. @param increment (see Bucket#rate_limited?) @see Bucket#rate_limited? @return [Integer, false] How much time to wait or false if the request succeeded.

# File lib/discordrb/commands/rate_limiter.rb, line 112
def rate_limited?(key, thing, increment: 1)
  # Check whether the bucket actually exists
  return false unless @buckets && @buckets[key]

  @buckets[key].rate_limited?(thing, increment: increment)
end