discorb 0.11.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c80c426387c9e5ba44589c5851e7c1a4d54a5de99c82d5b3571c4200dcaf135
4
- data.tar.gz: 2aa6db0900cafe2120793b295b68e5de357e97e0797cd416a06caca9491b09e1
3
+ metadata.gz: 6b979da1f1325d5d9cae229e0262cce1d6cf16b6aca91feba731ec6a0146fe42
4
+ data.tar.gz: e6e1fcbe4265cfb54828fef5c7e2d6304dd306544e941cb787233ad563d9abaf
5
5
  SHA512:
6
- metadata.gz: afe14b35796820b8b7af5baaab6a79a5a770c55d11d30d25b2a9647034658e27346e918be42fb8d4a30edbf27873bd367cf3551cf1b091e82c49150c886ef03e
7
- data.tar.gz: 9c817738b2a4b3763d94182e2b47f583aaa7129a8769cdc9b8f7fc7e10f0656f43183f0d416d72aa20112b6b34addfdd38e36b7710c6405d2f29f658cb238c4c
6
+ metadata.gz: bb35c0736b2e549964030103dfb1202ef7711fd3c36323710d3f94631b958a9976f7c111f340d03cf2ed67032a8f74f5165f66b5ff92c59d0628af25b4a95e79
7
+ data.tar.gz: 01d59859fb5e84beaafdc48e333ba841398a8fe5867c52bb9621b2d676493dbc776297c94e303297ffeef591c7f2e527ea30aaae381323e4ed8e8b1ce632b1ef
data/Changelog.md CHANGED
@@ -4,6 +4,13 @@
4
4
 
5
5
  ## v0.11
6
6
 
7
+ ### v0.11.1
8
+
9
+ - Improve: Improve rate limit handling
10
+ - Fix: Fix bug in Integration initalization
11
+ - Change: Change log style
12
+ - Add: Support OP code 7
13
+
7
14
  ### v0.11.0
8
15
 
9
16
  - Add: Improve documents
@@ -4,7 +4,7 @@ module Discorb
4
4
  # @return [String] The API base URL.
5
5
  API_BASE_URL = "https://discord.com/api/v9"
6
6
  # @return [String] The version of discorb.
7
- VERSION = "0.11.0"
7
+ VERSION = "0.11.1"
8
8
  # @return [String] The user agent for the bot.
9
9
  USER_AGENT = "DiscordBot (https://discorb-lib.github.io #{VERSION}) Ruby/#{RUBY_VERSION}"
10
10
 
@@ -495,6 +495,7 @@ module Discorb
495
495
  @connection = connection
496
496
  @zlib_stream = Zlib::Inflate.new(Zlib::MAX_WBITS)
497
497
  @buffer = +""
498
+
498
499
  while (message = @connection.read)
499
500
  @buffer << message
500
501
  if message.end_with?((+"\x00\x00\xff\xff").force_encoding("ASCII-8BIT"))
@@ -505,7 +506,7 @@ module Discorb
505
506
  rescue JSON::ParserError
506
507
  @buffer = +""
507
508
  @log.error "Received invalid JSON from gateway."
508
- @log.debug data
509
+ @log.debug "#{data}"
509
510
  else
510
511
  handle_gateway(message)
511
512
  end
@@ -524,7 +525,10 @@ module Discorb
524
525
  @log.error "Discord WebSocket closed: #{e.message}"
525
526
  connect_gateway(false)
526
527
  end
527
- rescue EOFError, Async::Wrapper::Cancelled
528
+ rescue EOFError, Async::Wrapper::Cancelled, Async::Wrapper::WaitError
529
+ connect_gateway(false)
530
+ rescue => e
531
+ @log.error "Discord WebSocket error: #{e.message}"
528
532
  connect_gateway(false)
529
533
  end
530
534
  end
@@ -533,7 +537,7 @@ module Discorb
533
537
  def send_gateway(opcode, **value)
534
538
  @connection.write({ op: opcode, d: value }.to_json)
535
539
  @connection.flush
536
- @log.debug "Sent message: #{{ op: opcode, d: value }.to_json.gsub(@token, "[Token]")}"
540
+ @log.debug "Sent message #{{ op: opcode, d: value }.to_json.gsub(@token, "[Token]")}"
537
541
  end
538
542
 
539
543
  def handle_gateway(payload)
@@ -568,6 +572,11 @@ module Discorb
568
572
  }
569
573
  send_gateway(6, **payload)
570
574
  end
575
+ when 7
576
+ @log.info "Received opcode 7, reconnecting"
577
+ @tasks.map(&:stop)
578
+ @connection.close
579
+ connect_gateway(false)
571
580
  when 9
572
581
  @log.warn "Received opcode 9, closed connection"
573
582
  @tasks.map(&:stop)
@@ -1024,7 +1033,7 @@ module Discorb
1024
1033
  if respond_to?("event_" + event_name.downcase)
1025
1034
  __send__("event_" + event_name.downcase, data)
1026
1035
  else
1027
- @log.debug "Received unknown event: #{event_name}\n#{data.inspect}"
1036
+ @log.debug "#{event_name}\n#{data.inspect}"
1028
1037
  end
1029
1038
  end
1030
1039
  end
data/lib/discorb/http.rb CHANGED
@@ -175,6 +175,7 @@ module Discorb
175
175
  def handle_response(method, resp, data, path, body, headers, audit_log_reason, kwargs)
176
176
  case resp.code
177
177
  when "429"
178
+ @client.log.info("Rate limit exceeded for #{method} #{path}, waiting #{data[:retry_after]} seconds")
178
179
  sleep(data[:retry_after])
179
180
  if body
180
181
  __send__(method, path, body, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait
@@ -227,15 +228,19 @@ module Discorb
227
228
  end
228
229
 
229
230
  def get_response_data(resp)
230
- if resp["Via"].nil? && resp.code == "429"
231
- raise CloudFlareBanError.new(resp, @client)
231
+ begin
232
+ data = JSON.parse(resp.body, symbolize_names: true)
233
+ rescue JSON::ParserError, TypeError
234
+ if resp.body.nil? || resp.body.empty?
235
+ data = nil
236
+ else
237
+ data = resp.body
238
+ end
232
239
  end
233
- rd = resp.body
234
- if rd.nil? || rd.empty?
235
- nil
236
- else
237
- JSON.parse(rd, symbolize_names: true)
240
+ if resp["Via"].nil? && resp.code == "429" && data.is_a?(String)
241
+ raise CloudFlareBanError.new(resp, @client)
238
242
  end
243
+ data
239
244
  end
240
245
 
241
246
  def http
@@ -80,7 +80,7 @@ module Discorb
80
80
  @enable_emoticons = data[:enable_emoticons]
81
81
  @expire_behavior = self.class.expire_behavior[data[:expire_behavior]]
82
82
  @expire_grace_period = data[:expire_grace_period]
83
- @user = @client.users[data[:user].to_i]
83
+ @user = @client.users[data[:user][:id]] or Discorb::User.new(@client, data[:user])
84
84
  @account = Account.new(data[:account])
85
85
  @subscriber_count = data[:subscriber_count]
86
86
  @revoked = data[:revoked]
@@ -9,8 +9,8 @@ module Discorb
9
9
  # @private
10
10
  def initialize(client)
11
11
  @client = client
12
- @ratelimit_hash = {}
13
- @path_ratelimit_hash = {}
12
+ @current_ratelimits = {}
13
+ @path_ratelimit_bucket = {}
14
14
  @global = false
15
15
  end
16
16
 
@@ -25,24 +25,23 @@ module Discorb
25
25
 
26
26
  if @global
27
27
  time = b[:reset_at] - Time.now.to_i
28
- @client.log.info("Global ratelimit reached, waiting #{time} seconds")
28
+ @client.log.info("global rate limit reached, waiting #{time} seconds")
29
29
  sleep(time)
30
30
  @global = false
31
-
32
31
  end
33
32
 
34
- return unless hash = @path_ratelimit_hash[method + path]
33
+ return unless hash = @path_ratelimit_bucket[method + path]
35
34
 
36
- return unless b = @ratelimit_hash[hash]
35
+ return unless b = @current_ratelimits[hash]
37
36
 
38
- if b[:reset_at] < Time.now.to_i
39
- @ratelimit_hash.delete(hash)
37
+ if b[:reset_at] < Time.now.to_f
38
+ @current_ratelimits.delete(hash)
40
39
  return
41
40
  end
42
41
  return if b[:remaining] > 0
43
42
 
44
- time = b[:reset_at] - Time.now.to_i
45
- @client.log.info("Ratelimit reached, waiting #{time} seconds")
43
+ time = b[:reset_at] - Time.now.to_f
44
+ @client.log.info("rate limit for #{method} #{path} reached, waiting #{time} seconds")
46
45
  sleep(time)
47
46
  end
48
47
 
@@ -59,10 +58,10 @@ module Discorb
59
58
  end
60
59
  return unless resp["X-RateLimit-Remaining"]
61
60
 
62
- @path_ratelimit_hash[method + path] = resp["X-RateLimit-Bucket"]
63
- @ratelimit_hash[resp["X-RateLimit-Bucket"]] = {
61
+ @path_ratelimit_bucket[method + path] = resp["X-RateLimit-Bucket"]
62
+ @current_ratelimits[resp["X-RateLimit-Bucket"]] = {
64
63
  remaining: resp["X-RateLimit-Remaining"].to_i,
65
- reset_at: resp["X-RateLimit-Reset"].to_i,
64
+ reset_at: Time.now.to_f + resp["X-RateLimit-Reset-After"].to_f,
66
65
  }
67
66
  end
68
67
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discorb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sevenc-nanashi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-10 00:00:00.000000000 Z
11
+ date: 2021-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async