Module: Discordrb::Commands::RateLimiter

Included in:
CommandContainer, SimpleRateLimiter
Defined in:
lib/discordrb/commands/rate_limiter.rb

Overview

Represents a collection of Buckets.

Instance Method Summary collapse

Instance Method Details

#bucket(key, attributes) ⇒ Bucket

Defines a new bucket for this rate limiter.

Options Hash (attributes):

  • :limit (Integer)

    The limit of requests to perform in the given time span.

  • :time_span (Integer)

    How many seconds until the limit should be reset.

  • :delay (Integer)

    How many seconds the user has to wait after each request.

See Also:


100
101
102
103
# File 'lib/discordrb/commands/rate_limiter.rb', line 100

def bucket(key, attributes)
  @buckets ||= {}
  @buckets[key] = Bucket.new(attributes[:limit], attributes[:time_span], attributes[:delay])
end

#cleanObject

Cleans all buckets

See Also:


120
121
122
# File 'lib/discordrb/commands/rate_limiter.rb', line 120

def clean
  @buckets.each(&:clean)
end

#include_buckets(limiter) ⇒ Object

Adds all the buckets from another RateLimiter onto this one.


126
127
128
129
130
# File 'lib/discordrb/commands/rate_limiter.rb', line 126

def include_buckets(limiter)
  buckets = limiter.instance_variable_get('@buckets') || {}
  @buckets ||= {}
  @buckets.merge! buckets
end

#rate_limited?(key, thing, increment: 1) ⇒ Integer, false

Performs a rate limit request.


111
112
113
114
115
116
# File 'lib/discordrb/commands/rate_limiter.rb', line 111

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