Class: DiscordRDA::Configuration
- Inherits:
-
Object
- Object
- DiscordRDA::Configuration
- Defined in:
- lib/discord_rda/core/configuration.rb
Overview
Immutable configuration for DiscordRDA. Uses frozen hash to prevent mutation after initialization.
Constant Summary collapse
- DEFAULTS =
Default configuration values
{ api_version: 10, gateway_encoding: :json, gateway_compression: :zlib_stream, shards: [0, 1], cache: :memory, max_reconnect_delay: 60.0, initial_reconnect_delay: 1.0, enable_resume: true, heartbeat_interval_buffer: 0.9, rest_timeout: 30.0, rest_open_timeout: 10.0, rest_read_timeout: 30.0, compression_threshold: 1024, intents: [:guilds], log_level: :info, log_format: :structured }.freeze
- INTENTS =
Valid intents mapping
{ guilds: 1 << 0, guild_members: 1 << 1, guild_moderation: 1 << 2, guild_emojis_and_stickers: 1 << 3, guild_integrations: 1 << 4, guild_webhooks: 1 << 5, guild_invites: 1 << 6, guild_voice_states: 1 << 7, guild_presences: 1 << 8, guild_messages: 1 << 9, guild_message_reactions: 1 << 10, guild_message_typing: 1 << 11, direct_messages: 1 << 12, direct_message_reactions: 1 << 13, direct_message_typing: 1 << 14, message_content: 1 << 15, guild_scheduled_events: 1 << 16, auto_moderation_configuration: 1 << 20, auto_moderation_execution: 1 << 21, guild_message_polls: 1 << 24, direct_message_polls: 1 << 25 }.freeze
Instance Attribute Summary collapse
-
#api_version ⇒ Integer
readonly
Discord API version.
-
#cache ⇒ Symbol
readonly
Cache backend (:memory or :redis).
-
#compression_threshold ⇒ Integer
readonly
Compression threshold in bytes.
-
#enable_resume ⇒ Boolean
readonly
Enable session resumption.
-
#gateway_compression ⇒ Symbol
readonly
Gateway compression (:zlib_stream or nil).
-
#gateway_encoding ⇒ Symbol
readonly
Gateway encoding (:json or :etf).
-
#heartbeat_interval_buffer ⇒ Float
readonly
Heartbeat interval multiplier (0.0-1.0).
-
#initial_reconnect_delay ⇒ Float
readonly
Initial reconnect delay in seconds.
-
#intents ⇒ Array<Symbol>
readonly
Enabled gateway intents.
-
#log_format ⇒ Symbol
readonly
Log format (:simple, :structured).
-
#log_level ⇒ Symbol
readonly
Log level (:debug, :info, :warn, :error).
-
#max_reconnect_delay ⇒ Float
readonly
Maximum reconnect delay in seconds.
-
#rest_open_timeout ⇒ Float
readonly
REST connection open timeout.
-
#rest_read_timeout ⇒ Float
readonly
REST read timeout.
-
#rest_timeout ⇒ Float
readonly
REST request timeout.
-
#shards ⇒ Array<Integer>, Symbol
readonly
Shards configuration or :auto.
-
#token ⇒ String
readonly
Bot token.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Configuration
constructor
Create a new configuration.
-
#intents_bitmask ⇒ Integer
Calculate the intents bitmask for Gateway identify.
-
#to_h ⇒ Hash
Convert configuration to hash.
-
#with(**overrides) ⇒ Configuration
Get a new configuration with modified options.
Constructor Details
#initialize(options = {}) ⇒ Configuration
Create a new configuration
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/discord_rda/core/configuration.rb', line 113 def initialize( = {}) config = DEFAULTS.merge() # Validate required options raise ArgumentError, 'Token is required' unless config[:token] @token = config[:token].freeze @api_version = config[:api_version].to_i @gateway_encoding = config[:gateway_encoding].to_sym @gateway_compression = config[:gateway_compression]&.to_sym @shards = normalize_shards(config[:shards]) @cache = config[:cache].to_sym @max_reconnect_delay = config[:max_reconnect_delay].to_f @initial_reconnect_delay = config[:initial_reconnect_delay].to_f @enable_resume = !!config[:enable_resume] @heartbeat_interval_buffer = config[:heartbeat_interval_buffer].to_f.clamp(0.0, 1.0) @rest_timeout = config[:rest_timeout].to_f @rest_open_timeout = config[:rest_open_timeout].to_f @rest_read_timeout = config[:rest_read_timeout].to_f @compression_threshold = config[:compression_threshold].to_i @intents = normalize_intents(config[:intents]) @log_level = config[:log_level].to_sym @log_format = config[:log_format].to_sym freeze end |
Instance Attribute Details
#api_version ⇒ Integer (readonly)
Returns Discord API version.
64 65 66 |
# File 'lib/discord_rda/core/configuration.rb', line 64 def api_version @api_version end |
#cache ⇒ Symbol (readonly)
Returns Cache backend (:memory or :redis).
76 77 78 |
# File 'lib/discord_rda/core/configuration.rb', line 76 def cache @cache end |
#compression_threshold ⇒ Integer (readonly)
Returns Compression threshold in bytes.
100 101 102 |
# File 'lib/discord_rda/core/configuration.rb', line 100 def compression_threshold @compression_threshold end |
#enable_resume ⇒ Boolean (readonly)
Returns Enable session resumption.
85 86 87 |
# File 'lib/discord_rda/core/configuration.rb', line 85 def enable_resume @enable_resume end |
#gateway_compression ⇒ Symbol (readonly)
Returns Gateway compression (:zlib_stream or nil).
70 71 72 |
# File 'lib/discord_rda/core/configuration.rb', line 70 def gateway_compression @gateway_compression end |
#gateway_encoding ⇒ Symbol (readonly)
Returns Gateway encoding (:json or :etf).
67 68 69 |
# File 'lib/discord_rda/core/configuration.rb', line 67 def gateway_encoding @gateway_encoding end |
#heartbeat_interval_buffer ⇒ Float (readonly)
Returns Heartbeat interval multiplier (0.0-1.0).
88 89 90 |
# File 'lib/discord_rda/core/configuration.rb', line 88 def heartbeat_interval_buffer @heartbeat_interval_buffer end |
#initial_reconnect_delay ⇒ Float (readonly)
Returns Initial reconnect delay in seconds.
82 83 84 |
# File 'lib/discord_rda/core/configuration.rb', line 82 def initial_reconnect_delay @initial_reconnect_delay end |
#intents ⇒ Array<Symbol> (readonly)
Returns Enabled gateway intents.
103 104 105 |
# File 'lib/discord_rda/core/configuration.rb', line 103 def intents @intents end |
#log_format ⇒ Symbol (readonly)
Returns Log format (:simple, :structured).
109 110 111 |
# File 'lib/discord_rda/core/configuration.rb', line 109 def log_format @log_format end |
#log_level ⇒ Symbol (readonly)
Returns Log level (:debug, :info, :warn, :error).
106 107 108 |
# File 'lib/discord_rda/core/configuration.rb', line 106 def log_level @log_level end |
#max_reconnect_delay ⇒ Float (readonly)
Returns Maximum reconnect delay in seconds.
79 80 81 |
# File 'lib/discord_rda/core/configuration.rb', line 79 def max_reconnect_delay @max_reconnect_delay end |
#rest_open_timeout ⇒ Float (readonly)
Returns REST connection open timeout.
94 95 96 |
# File 'lib/discord_rda/core/configuration.rb', line 94 def rest_open_timeout @rest_open_timeout end |
#rest_read_timeout ⇒ Float (readonly)
Returns REST read timeout.
97 98 99 |
# File 'lib/discord_rda/core/configuration.rb', line 97 def rest_read_timeout @rest_read_timeout end |
#rest_timeout ⇒ Float (readonly)
Returns REST request timeout.
91 92 93 |
# File 'lib/discord_rda/core/configuration.rb', line 91 def rest_timeout @rest_timeout end |
#shards ⇒ Array<Integer>, Symbol (readonly)
Returns Shards configuration or :auto.
73 74 75 |
# File 'lib/discord_rda/core/configuration.rb', line 73 def shards @shards end |
#token ⇒ String (readonly)
Returns Bot token.
61 62 63 |
# File 'lib/discord_rda/core/configuration.rb', line 61 def token @token end |
Instance Method Details
#intents_bitmask ⇒ Integer
Calculate the intents bitmask for Gateway identify
142 143 144 |
# File 'lib/discord_rda/core/configuration.rb', line 142 def intents_bitmask @intents.sum { |intent| INTENTS.fetch(intent, 0) } end |
#to_h ⇒ Hash
Convert configuration to hash
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/discord_rda/core/configuration.rb', line 155 def to_h { token: @token, api_version: @api_version, gateway_encoding: @gateway_encoding, gateway_compression: @gateway_compression, shards: @shards, cache: @cache, max_reconnect_delay: @max_reconnect_delay, initial_reconnect_delay: @initial_reconnect_delay, enable_resume: @enable_resume, heartbeat_interval_buffer: @heartbeat_interval_buffer, rest_timeout: @rest_timeout, rest_open_timeout: @rest_open_timeout, rest_read_timeout: @rest_read_timeout, compression_threshold: @compression_threshold, intents: @intents, log_level: @log_level, log_format: @log_format } end |
#with(**overrides) ⇒ Configuration
Get a new configuration with modified options
149 150 151 |
# File 'lib/discord_rda/core/configuration.rb', line 149 def with(**overrides) self.class.new(to_h.merge(overrides)) end |