discorb 0.15.1 → 0.16.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.
@@ -100,7 +100,7 @@ module Discorb
100
100
  interaction = klass.make_interaction(client, data) if !klass.interaction_type.nil? && klass.interaction_type == data[:type]
101
101
  end
102
102
  if interaction.nil?
103
- client.log.warn("Unknown interaction type #{data[:type]}, initialized Interaction")
103
+ client.logger.warn("Unknown interaction type #{data[:type]}, initialized Interaction")
104
104
  interaction = Interaction.new(client, data)
105
105
  end
106
106
  interaction
@@ -32,7 +32,7 @@ module Discorb
32
32
  # return if path.url.start_with?("https://")
33
33
  if @global && @global > Time.now.to_f
34
34
  time = @global - Time.now.to_f
35
- @client.log.info("global rate limit reached, waiting #{time} seconds")
35
+ @client.logger.info("global rate limit reached, waiting #{time} seconds")
36
36
  sleep(time)
37
37
  @global = false
38
38
  end
@@ -48,7 +48,7 @@ module Discorb
48
48
  return if (bucket[:remaining]).positive?
49
49
 
50
50
  time = bucket[:reset_at] - Time.now.to_f
51
- @client.log.info("rate limit for #{path.identifier} with #{path.major_param} reached, waiting #{time.round(4)} seconds")
51
+ @client.logger.info("rate limit for #{path.identifier} with #{path.major_param} reached, waiting #{time.round(4)} seconds")
52
52
  sleep(time)
53
53
  end
54
54
 
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents a shard.
6
+ #
7
+ class Shard
8
+ # @return [Integer] The ID of the shard.
9
+ attr_reader :id
10
+ # @return [Thread] The thread of the shard.
11
+ attr_reader :thread
12
+ # @return [Logger] The logger of the shard.
13
+ attr_reader :logger
14
+ # @private
15
+ # @return [Integer] The internal index of the shard.
16
+ attr_reader :index
17
+ # @private
18
+ attr_accessor :status, :connection, :session_id, :next_shard, :main_task
19
+
20
+ #
21
+ # Initializes a new shard.
22
+ # @private
23
+ #
24
+ # @param [Discorb::Client] client The client.
25
+ # @param [Integer] id The ID of the shard.
26
+ # @param [Integer] count The number of shards.
27
+ # @param [Integer] index The index of the shard.
28
+ #
29
+ def initialize(client, id, count, index)
30
+ @client = client
31
+ @id = id
32
+ @shard_count = count
33
+ @status = :idle
34
+ @index = index
35
+ @session_id = nil
36
+ @next_shard = nil
37
+ @main_task = nil
38
+ @logger = client.logger.dup.tap { |l| l.progname = "discorb: shard #{id}" }
39
+ @thread = Thread.new do
40
+ Thread.current.thread_variable_set("shard_id", id)
41
+ Thread.current.thread_variable_set("shard", self)
42
+ if @index.positive?
43
+ Thread.stop
44
+ sleep 5 # Somehow discord disconnects the shard without a little sleep.
45
+ end
46
+ client.send(:main_loop, id)
47
+ end
48
+ end
49
+
50
+ #
51
+ # Starts the shard.
52
+ #
53
+ # @return [void]
54
+ #
55
+ def start
56
+ @thread.wakeup
57
+ end
58
+
59
+ #
60
+ # Stops the shard.
61
+ #
62
+ # @return [void]
63
+ #
64
+ def close!
65
+ @status = :closed
66
+ @main_task&.stop
67
+ @thread.kill
68
+ end
69
+
70
+ def inspect
71
+ "#<#{self.class} #{id}/#{@shard_count} #{@status}>"
72
+ end
73
+ end
74
+ end
data/lib/discorb.rb CHANGED
@@ -45,10 +45,10 @@ require_order = %w[common flag dictionary error rate_limit http intents emoji_ta
45
45
  %w[message_meta allowed_mentions] +
46
46
  %w[user member guild emoji channel embed message] +
47
47
  %w[application audit_logs color components event event_handler] +
48
- %w[attachment guild_template image integration interaction invite log permission] +
48
+ %w[attachment guild_template image integration interaction invite permission] +
49
49
  %w[presence reaction role sticker utils voice_state webhook] +
50
50
  %w[gateway_requests gateway app_command] +
51
- %w[asset extension client extend]
51
+ %w[asset extension shard client extend]
52
52
  require_order.each do |name|
53
53
  require_relative "discorb/#{name}.rb"
54
54
  end