dsu3 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25b1258b84cb23af62808ce8711f4098013e6bb4686c23238a704f00d53e24e8
4
- data.tar.gz: 3b701469299fc81e49d1df571c02c4ef6b11d2ae8ad5cf20ef56463c3697dace
3
+ metadata.gz: cab6805b66145e0cd335d4431b7d16146ecc05b119159eaf741d2cb986c9bd7d
4
+ data.tar.gz: 11c4787411fb7fb0210e8e2599f8d1d3205745447d0ba380963e8687837e6497
5
5
  SHA512:
6
- metadata.gz: 25c924f2d718fb9f02b1ae8b709665dd065adad0d77ec0d2b48873f85f0f65d738dd00cd21b361d0d08a120e25628151524840d8e050f9c9424e3d9cb9481cc3
7
- data.tar.gz: a26e90b80489b8035acf0710591163b9294607c80dfd981dba70853cc01a0d3bec59d537ca4ee5400d309ad58dc54612dee25be2cf5a4700dcdd9a84d11d03d1
6
+ metadata.gz: 05fd5a369ad65fe3fe4330ee216b4108f894695964211389c16d3996c7c428a2e387eda909f3e96689511112f39fe526777b25053d3fd493958d67b13c85ccfb
7
+ data.tar.gz: aad9ef63b68458829d85845015d2f70e8f93611d85fc4e2e86f19136cccbf1cbf213d764be3a15b512c86c8f42d882dba653c4c121f84b7d63088fbee5028257
data/lib/dsu3/api.rb CHANGED
@@ -42,7 +42,7 @@ module DSU3
42
42
  rescue RestClient::ExceptionWithResponse => e
43
43
  begin
44
44
  data = JSON.parse(e.response.body)
45
- raise DSU3::RateLimitError('ratelimit exceeded', data['retry_after']) if e.is_a?(RestClient::TooManyRequests)
45
+ raise DSU3::RateLimitError.new('ratelimit exceeded', data['retry_after']) if e.is_a?(RestClient::TooManyRequests)
46
46
  raise DSU3::CodeError, "#{data['code']}: #{data['message']}"
47
47
  rescue JSON::ParserError
48
48
  raise e
@@ -5,7 +5,7 @@ module DSU3
5
5
  class Channel < DObject
6
6
  # Types text into a channel
7
7
  def type
8
- @net.request(cooldown: 9, loop: true) { DSU3::API::Channel.type(_1, @id) }
8
+ @net.request(loop: true, cooldown: 9) { DSU3::API::Channel.type(_1, @id) }
9
9
  end
10
10
 
11
11
  # Sends a message to a channel
@@ -5,7 +5,7 @@ module DSU3
5
5
  class Guild < DObject
6
6
  # Verifies accounts on the server
7
7
  def verify
8
- @net.request(cooldown: 0.5) { DSU3::API::Guild.verify(_1, @id) }
8
+ @net.request(seq: true, cooldown: 0.5) { DSU3::API::Guild.verify(_1, @id) }
9
9
  end
10
10
  end
11
11
  end
@@ -5,7 +5,7 @@ module DSU3
5
5
  class Invite < DObject
6
6
  # Joins guild
7
7
  def join
8
- @net.request(cooldown: 0.5) { DSU3::API::Invites.join(token, @id) }
8
+ @net.request(seq: true, cooldown: 0.5) { DSU3::API::Invite.join(_1, @id) }
9
9
  end
10
10
  end
11
11
  end
@@ -13,8 +13,9 @@ module DSU3
13
13
  # Reacts to a message
14
14
  # @note To use custom emoji, you must encode it in the format name:id with the emoji name and emoji id
15
15
  # @param [String] emoji
16
- def react(emoji)
17
- @net.request { DSU3::API::Message.react(_1, @channel_id, @id, emoji) }
16
+ # @param [Integer] cooldown
17
+ def react(emoji, cooldown = 0)
18
+ @net.request(seq: true, cooldown: cooldown) { DSU3::API::Message.react(_1, @channel_id, @id, emoji) }
18
19
  end
19
20
  end
20
21
  end
data/lib/dsu3/net.rb CHANGED
@@ -5,33 +5,43 @@ require 'dsu3/data'
5
5
  module DSU3
6
6
  # Class used to manage multiple bots
7
7
  class Net
8
- attr_reader :tokens, :mutex
9
-
10
8
  # @param [Array] tokens List of bot tokens
11
9
  def initialize(*tokens)
12
10
  raise 'There must be at least one token' if tokens.empty?
13
11
 
14
- @tokens = tokens
15
- @mutex = Mutex.new
12
+ @tokens = tokens.uniq
16
13
  end
17
14
 
18
- # An auxiliary method that can be used to make an Discord API call from multiple tokens with ratelimit and thread support
15
+ # Make Discord API calls
19
16
  # @param [Hash] opts Options
17
+ # @option opts [Bool] :seq Whether we have to make API calls consecutively
18
+ # @option opts [Bool] :loop If threads should loop
19
+ # @option opts [Integer, Float] :cooldown Cooldown between API calls
20
20
  def request(opts = {})
21
- opts = { cooldown: 0, loop: false }.merge(opts)
21
+ opts = { seq: false, loop: false, cooldown: 0 }.merge(opts)
22
+
23
+ raise 'sequential requests do not support looping' if opts[:seq] && opts[:loop]
24
+
25
+ mutex = opts[:seq] ? Mutex.new : nil
22
26
 
23
27
  @tokens.map do |token|
24
28
  Thread.new do
25
29
  begin
26
- @mutex.synchronize { yield token }
30
+ mutex&.lock
31
+
32
+ yield token
27
33
  sleep(opts[:cooldown])
34
+
35
+ mutex&.unlock
28
36
  rescue DSU3::RateLimitError => e
37
+ mutex&.unlock
38
+
29
39
  DSU3::LOGGER.warn('ratelimit exceeded')
30
- @mutex.sleep(e.retry_after)
40
+ sleep(e.retry_after)
31
41
  retry
32
42
  end while opts[:loop]
33
43
  end
34
- end.each(&:join)
44
+ end.join(&:map)
35
45
  end
36
46
 
37
47
  # I'm too lazy to try to document it
data/lib/dsu3/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DSU3
4
- VERSION = '1.1.2'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsu3
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Sheremetjev IV
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-23 00:00:00.000000000 Z
11
+ date: 2022-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client