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.

Parameters:

  • key (Symbol)

    The name for this new bucket.

  • attributes (Hash)

    The attributes to initialize the bucket with.

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.

Returns:

  • (Bucket)

    the created bucket.

See Also:



101
102
103
104
# 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

#cleanObject

Cleans all buckets

See Also:



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

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

#include_buckets(limiter) ⇒ Object

Adds all the buckets from another RateLimiter onto this one.

Parameters:



127
128
129
130
131
# 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) ⇒ Integer, false

Performs a rate limit request.

Parameters:

  • key (Symbol)

    Which bucket to perform the request for.

  • thing (String, Integer, Symbol)

    What should be rate-limited.

  • increment (Integer) (defaults to: 1)

    How much to increment the rate-limit counter. Default is 1.

Returns:

  • (Integer, false)

    How much time to wait or false if the request succeeded.

See Also:



112
113
114
115
116
117
# 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