Class: DSU3::Net
- Inherits:
-
Object
- Object
- DSU3::Net
- Defined in:
- lib/dsu3/net.rb
Overview
Class used to manage multiple bots
Instance Method Summary collapse
-
#initialize(*tokens) ⇒ Net
constructor
A new instance of Net.
-
#request(opts = {}) ⇒ Object
Make Discord API calls.
Constructor Details
#initialize(*tokens) ⇒ Net
Returns a new instance of Net.
9 10 11 12 13 |
# File 'lib/dsu3/net.rb', line 9 def initialize(*tokens) raise 'There must be at least one token' if tokens.empty? @tokens = tokens.uniq end |
Instance Method Details
#request(opts = {}) ⇒ Object
Make Discord API calls
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dsu3/net.rb', line 20 def request(opts = {}) opts = { seq: false, loop: false, cooldown: 0 }.merge(opts) raise 'sequential requests do not support looping' if opts[:seq] && opts[:loop] mutex = opts[:seq] ? Mutex.new : nil @tokens.map do |token| Thread.new do begin mutex&.lock yield token sleep(opts[:cooldown]) mutex&.unlock rescue DSU3::RateLimitError => e mutex&.unlock DSU3::LOGGER.warn('ratelimit exceeded') sleep(e.retry_after) retry end while opts[:loop] end end.join(&:map) end |