dsu3 1.1.0 → 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: 3836a58d3aac7cf05d21fddc541bff2daed7ae535c9afbf57aedccaaa452a11a
4
- data.tar.gz: 5dec7ec5f4704f993efeccda45900e6824da03bf5c9c9715203d446cb0231a67
3
+ metadata.gz: cab6805b66145e0cd335d4431b7d16146ecc05b119159eaf741d2cb986c9bd7d
4
+ data.tar.gz: 11c4787411fb7fb0210e8e2599f8d1d3205745447d0ba380963e8687837e6497
5
5
  SHA512:
6
- metadata.gz: 803eafe94df41b818bb6d90bb068b6f27bc7f0a7c718c854069070c7e381fe62e5f85598cc43ed594bbe63284e6542cf3bbe70cb865daea6a80cd293607b6493
7
- data.tar.gz: 8872033f7b17ffd4498f4bb35003543f8c8f0f29a11cc480924e92141b0eb577c2a730a49fd31173c56c496e03f14fe213f167613420c17ab7a3355adccd8b1e
6
+ metadata.gz: 05fd5a369ad65fe3fe4330ee216b4108f894695964211389c16d3996c7c428a2e387eda909f3e96689511112f39fe526777b25053d3fd493958d67b13c85ccfb
7
+ data.tar.gz: aad9ef63b68458829d85845015d2f70e8f93611d85fc4e2e86f19136cccbf1cbf213d764be3a15b512c86c8f42d882dba653c4c121f84b7d63088fbee5028257
@@ -23,4 +23,4 @@ module DSU3
23
23
  end
24
24
  end
25
25
  end
26
- end
26
+ end
data/lib/dsu3/api/user.rb CHANGED
@@ -16,4 +16,4 @@ module DSU3
16
16
  end
17
17
  end
18
18
  end
19
- end
19
+ end
data/lib/dsu3/api.rb CHANGED
@@ -15,8 +15,6 @@ module DSU3
15
15
  module API
16
16
  API_BASE = 'https://discord.com/api/v9/'
17
17
 
18
- @ratelimits = []
19
-
20
18
  module_function
21
19
 
22
20
  # Makes an API request without any error handling
@@ -40,16 +38,14 @@ module DSU3
40
38
  # @param (see #raw_request)
41
39
  # @return (see #raw_request)
42
40
  def request(...)
41
+ raw_request(...)
42
+ rescue RestClient::ExceptionWithResponse => e
43
43
  begin
44
- raw_request(...)
45
- rescue RestClient::ExceptionWithResponse => e
46
- begin
47
- data = JSON.parse(e.response.body)
48
- raise DSU3::RateLimitError('ratelimit exceeded', data['retry_after']) if e.is_a?(RestClient::TooManyRequests)
49
- raise DSU3::CodeError.new("#{data['code']}: #{data['message']}")
50
- rescue JSON::ParserError
51
- raise e
52
- end
44
+ data = JSON.parse(e.response.body)
45
+ raise DSU3::RateLimitError.new('ratelimit exceeded', data['retry_after']) if e.is_a?(RestClient::TooManyRequests)
46
+ raise DSU3::CodeError, "#{data['code']}: #{data['message']}"
47
+ rescue JSON::ParserError
48
+ raise e
53
49
  end
54
50
  end
55
51
  end
@@ -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
@@ -27,7 +27,7 @@ module DSU3
27
27
  # Returns Message instance
28
28
  # @param [String, Integer] message Message ID
29
29
  def message(message_id)
30
- DSU3::Message.new(@tokens, @id, message_id)
30
+ DSU3::Message.new(self, message_id)
31
31
  end
32
32
  end
33
33
  end
@@ -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
- end
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
- end
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
- end
21
+ end
data/lib/dsu3/data.rb CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'dsu3/api'
2
4
  require 'dsu3/dobject'
3
5
  require 'dsu3/data/channel'
4
6
  require 'dsu3/data/guild'
5
7
  require 'dsu3/data/invite'
6
- require 'dsu3/data/message'
8
+ require 'dsu3/data/message'
data/lib/dsu3/dobject.rb CHANGED
@@ -12,4 +12,4 @@ module DSU3
12
12
  @id = id
13
13
  end
14
14
  end
15
- end
15
+ end
data/lib/dsu3/errors.rb CHANGED
@@ -11,4 +11,4 @@ module DSU3
11
11
  end
12
12
 
13
13
  CodeError = Class.new(StandardError)
14
- end
14
+ end
data/lib/dsu3/net.rb CHANGED
@@ -5,37 +5,47 @@ 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]
22
24
 
23
- @tokens.each do |token|
25
+ mutex = opts[:seq] ? Mutex.new : nil
26
+
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
- end.join
34
- end
43
+ end
44
+ end.join(&:map)
35
45
  end
36
46
 
37
47
  # I'm too lazy to try to document it
38
- %w{channel guild invite}.each do |s|
48
+ %w[channel guild invite].each do |s|
39
49
  define_method s do |id|
40
50
  DSU3.const_get(s.capitalize).new(self, id)
41
51
  end
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.0'
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.0
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