Class: Rubord::Client
- Inherits:
-
Object
- Object
- Rubord::Client
- Defined in:
- lib/rubord/structs/client.rb
Overview
Main client class for interacting with the Discord API.
The Client serves as the primary interface for creating Discord bots and managing connections to the Discord Gateway and REST API.
Instance Attribute Summary collapse
-
#channels ⇒ Rubord::Collection
readonly
Collection of cached channels.
-
#commands ⇒ Rubord::Commands
readonly
Commands for the bot.
-
#extends ⇒ Array<Class>
Array of extended classes/modules.
-
#gateway ⇒ Rubord::Gateway
readonly
The WebSocket gateway connection instance.
-
#guilds ⇒ Rubord::Collection
readonly
Collection of cached guilds (servers).
-
#intents ⇒ Integer
readonly
Bitwise value representing enabled Discord intents.
-
#messages ⇒ Rubord::Collection
readonly
Collection of cached messages.
-
#prefix ⇒ String
readonly
Command prefix for message-based commands.
-
#rest ⇒ Rubord::REST
readonly
The REST API client instance.
-
#user ⇒ Rubord::User?
readonly
The bot user account, available after login.
-
#users ⇒ Rubord::Collection
readonly
Collection of cached users.
Instance Method Summary collapse
- #application ⇒ Object
-
#fetch_channel(channel_id) ⇒ Rubord::Channel
Fetches a channel from Discord API and caches it.
-
#fetch_message(channel_id, message_id) ⇒ Rubord::Message
Fetches a message from Discord API and caches it.
-
#initialize(prefix: "", intents: []) ⇒ Rubord::Client
constructor
Initializes a new Discord client instance.
-
#latency ⇒ Integer
Returns the current WebSocket gateway latency in milliseconds.
-
#login(token) ⇒ Rubord::Client
Authenticates and connects to the Discord API.
-
#on(event) {|*args| ... } ⇒ void
Registers an event listener callback.
- #on_interaction {|interaction| ... } ⇒ Object
- #on_message {|message| ... } ⇒ Object
- #on_ready(&block) ⇒ Object
-
#stop ⇒ void
Gracefully disconnects from Discord and stops the client.
-
#uptime ⇒ Integer
Returns the bot’s uptime in seconds since login.
Constructor Details
#initialize(prefix: "", intents: []) ⇒ Rubord::Client
Initializes a new Discord client instance.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rubord/structs/client.rb', line 78 def initialize(prefix: "", intents: []) @token = nil @intents = parse_intents(intents) @prefix = prefix @rest = nil @gateway = nil @listeners = Hash.new { |h, k| h[k] = [] } @user = nil @start_time = Time.now.to_i @commands = Rubord::CommandRegistry.new @channels = Rubord::Collection.new @messages = Rubord::Collection.new @guilds = Rubord::Collection.new @users = Rubord::Collection.new end |
Instance Attribute Details
#channels ⇒ Rubord::Collection (readonly)
Returns Collection of cached channels.
38 39 40 |
# File 'lib/rubord/structs/client.rb', line 38 def channels @channels end |
#commands ⇒ Rubord::Commands (readonly)
Returns Commands for the bot.
56 57 58 |
# File 'lib/rubord/structs/client.rb', line 56 def commands @commands end |
#extends ⇒ Array<Class>
Returns Array of extended classes/modules.
59 60 61 |
# File 'lib/rubord/structs/client.rb', line 59 def extends @extends end |
#gateway ⇒ Rubord::Gateway (readonly)
Returns The WebSocket gateway connection instance.
32 33 34 |
# File 'lib/rubord/structs/client.rb', line 32 def gateway @gateway end |
#guilds ⇒ Rubord::Collection (readonly)
Returns Collection of cached guilds (servers).
53 54 55 |
# File 'lib/rubord/structs/client.rb', line 53 def guilds @guilds end |
#intents ⇒ Integer (readonly)
Returns Bitwise value representing enabled Discord intents.
41 42 43 |
# File 'lib/rubord/structs/client.rb', line 41 def intents @intents end |
#messages ⇒ Rubord::Collection (readonly)
Returns Collection of cached messages.
35 36 37 |
# File 'lib/rubord/structs/client.rb', line 35 def @messages end |
#prefix ⇒ String (readonly)
Returns Command prefix for message-based commands.
44 45 46 |
# File 'lib/rubord/structs/client.rb', line 44 def prefix @prefix end |
#rest ⇒ Rubord::REST (readonly)
Returns The REST API client instance.
29 30 31 |
# File 'lib/rubord/structs/client.rb', line 29 def rest @rest end |
#user ⇒ Rubord::User? (readonly)
Returns The bot user account, available after login.
47 48 49 |
# File 'lib/rubord/structs/client.rb', line 47 def user @user end |
#users ⇒ Rubord::Collection (readonly)
Returns Collection of cached users.
50 51 52 |
# File 'lib/rubord/structs/client.rb', line 50 def users @users end |
Instance Method Details
#application ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/rubord/structs/client.rb', line 118 def application return nil unless @user @application ||= begin data = @rest.get_application Rubord::Application.new(data, self) end end |
#fetch_channel(channel_id) ⇒ Rubord::Channel
Fetches a channel from Discord API and caches it.
229 230 231 232 233 234 |
# File 'lib/rubord/structs/client.rb', line 229 def fetch_channel(channel_id) data = @rest.get_channel(channel_id) channel = Rubord::Channel.new(data, self) @channels.set(channel.id, channel) channel end |
#fetch_message(channel_id, message_id) ⇒ Rubord::Message
Fetches a message from Discord API and caches it.
246 247 248 249 250 251 |
# File 'lib/rubord/structs/client.rb', line 246 def (channel_id, ) data = @rest.(channel_id, ) msg = Rubord::Message.new(data, self) @messages.set(msg.id, msg) msg end |
#latency ⇒ Integer
Returns the current WebSocket gateway latency in milliseconds.
103 104 105 |
# File 'lib/rubord/structs/client.rb', line 103 def latency @gateway&.latency || 0 end |
#login(token) ⇒ Rubord::Client
Authenticates and connects to the Discord API.
This method initializes the REST client, connects to the Gateway, and starts processing Discord events.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rubord/structs/client.rb', line 146 def login(token) raise InvalidTokenError, "Discord token cannot be empty" if token.nil? || token.strip.empty? @token = token @rest = Rubord::REST.new(token) @gateway = Rubord::Gateway.new(token, @intents) @gateway.connect do |event, data| handle_event(event, data) end @start_time = Time.now.to_i self end |
#on(event) {|*args| ... } ⇒ void
This method returns an undefined value.
Registers an event listener callback.
216 217 218 |
# File 'lib/rubord/structs/client.rb', line 216 def on(event, &block) @listeners[event.to_sym] << block end |
#on_interaction {|interaction| ... } ⇒ Object
190 191 192 |
# File 'lib/rubord/structs/client.rb', line 190 def on_interaction(&block) on(:interaction_create, &block) end |
#on_message {|message| ... } ⇒ Object
185 186 187 |
# File 'lib/rubord/structs/client.rb', line 185 def (&block) on(:message_create, &block) end |
#on_ready(&block) ⇒ Object
180 181 182 |
# File 'lib/rubord/structs/client.rb', line 180 def on_ready(&block) on(:ready, &block) end |
#stop ⇒ void
This method returns an undefined value.
Gracefully disconnects from Discord and stops the client.
This method closes the WebSocket connection and stops event processing.
176 177 178 |
# File 'lib/rubord/structs/client.rb', line 176 def stop @gateway&.close end |
#uptime ⇒ Integer
Returns the bot’s uptime in seconds since login.
113 114 115 116 |
# File 'lib/rubord/structs/client.rb', line 113 def uptime return 0 unless @start_time @start_time end |