Class: Rubord::Client

Inherits:
Object
  • Object
show all
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.

Examples:

Creating a basic bot client

client = Rubord::Client.new(prefix: "!", intents: [:messages])
client.("YOUR_BOT_TOKEN")

client.on(:ready) do |user|
  puts "Logged in as #{user.username}"
end

client.on(:message_create) do |message|
  if message.content.start_with?("!ping")
    message.reply("Pong!")
  end
end

See Also:

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix: "", intents: []) ⇒ Rubord::Client

Initializes a new Discord client instance.

Examples:

With prefix and intents

client = Rubord::Client.new(
  prefix: "!",
  intents: [:guilds, :guild_messages, :message_content]
)

With minimal configuration

client = Rubord::Client.new

Parameters:

  • prefix (String) (defaults to: "")

    The command prefix for message commands. Default is empty string (no prefix required).

  • intents (Array<Symbol, Integer>) (defaults to: [])

    Discord intents to enable. Can be symbols (e.g., ‘:guilds`, `:messages`) or integer bit values.

Since:

  • 1.0.0



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

#channelsRubord::Collection (readonly)

Returns Collection of cached channels.

Returns:

Since:

  • 1.0.0



38
39
40
# File 'lib/rubord/structs/client.rb', line 38

def channels
  @channels
end

#commandsRubord::Commands (readonly)

Returns Commands for the bot.

Returns:

  • (Rubord::Commands)

    Commands for the bot.

Since:

  • 1.0.0



56
57
58
# File 'lib/rubord/structs/client.rb', line 56

def commands
  @commands
end

#extendsArray<Class>

Returns Array of extended classes/modules.

Returns:

  • (Array<Class>)

    Array of extended classes/modules.

Since:

  • 1.0.0



59
60
61
# File 'lib/rubord/structs/client.rb', line 59

def extends
  @extends
end

#gatewayRubord::Gateway (readonly)

Returns The WebSocket gateway connection instance.

Returns:

Since:

  • 1.0.0



32
33
34
# File 'lib/rubord/structs/client.rb', line 32

def gateway
  @gateway
end

#guildsRubord::Collection (readonly)

Returns Collection of cached guilds (servers).

Returns:

Since:

  • 1.0.0



53
54
55
# File 'lib/rubord/structs/client.rb', line 53

def guilds
  @guilds
end

#intentsInteger (readonly)

Returns Bitwise value representing enabled Discord intents.

Returns:

  • (Integer)

    Bitwise value representing enabled Discord intents.

Since:

  • 1.0.0



41
42
43
# File 'lib/rubord/structs/client.rb', line 41

def intents
  @intents
end

#messagesRubord::Collection (readonly)

Returns Collection of cached messages.

Returns:

Since:

  • 1.0.0



35
36
37
# File 'lib/rubord/structs/client.rb', line 35

def messages
  @messages
end

#prefixString (readonly)

Returns Command prefix for message-based commands.

Returns:

  • (String)

    Command prefix for message-based commands.

Since:

  • 1.0.0



44
45
46
# File 'lib/rubord/structs/client.rb', line 44

def prefix
  @prefix
end

#restRubord::REST (readonly)

Returns The REST API client instance.

Returns:

Since:

  • 1.0.0



29
30
31
# File 'lib/rubord/structs/client.rb', line 29

def rest
  @rest
end

#userRubord::User? (readonly)

Returns The bot user account, available after login.

Returns:

  • (Rubord::User, nil)

    The bot user account, available after login.

Since:

  • 1.0.0



47
48
49
# File 'lib/rubord/structs/client.rb', line 47

def user
  @user
end

#usersRubord::Collection (readonly)

Returns Collection of cached users.

Returns:

Since:

  • 1.0.0



50
51
52
# File 'lib/rubord/structs/client.rb', line 50

def users
  @users
end

Instance Method Details

#applicationObject

Since:

  • 1.0.0



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.

Examples:

channel = client.fetch_channel("123456789012345678")
puts "Channel name: #{channel.name}"

Parameters:

  • channel_id (String, Integer)

    The Discord channel ID to fetch.

Returns:

Since:

  • 1.0.0



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.

Examples:

message = client.fetch_message("123456789012345678", "987654321098765432")
puts "Message content: #{message.content}"

Parameters:

  • channel_id (String, Integer)

    The ID of the channel containing the message.

  • message_id (String, Integer)

    The ID of the message to fetch.

Returns:

Since:

  • 1.0.0



246
247
248
249
250
251
# File 'lib/rubord/structs/client.rb', line 246

def fetch_message(channel_id, message_id)
  data = @rest.get_message(channel_id, message_id)
  msg = Rubord::Message.new(data, self)
  @messages.set(msg.id, msg)
  msg
end

#latencyInteger

Returns the current WebSocket gateway latency in milliseconds.

Examples:

puts "Latency: #{client.latency}ms"

Returns:

  • (Integer)

    Gateway latency in ms, or 0 if not connected.

Since:

  • 1.0.0



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.

Examples:

Basic login

client.("Bot MTExODg0OTgxOTk0NzMxOTgwOA.G0L2QN.secret")

With method chaining

client
  .(token)
  .on(:ready) { |user| puts "Ready!" }

Parameters:

  • token (String)

    The Discord bot token. Format: “Bot YOUR_TOKEN_HERE” or just “YOUR_TOKEN_HERE”.

Returns:

Raises:

Since:

  • 1.0.0



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rubord/structs/client.rb', line 146

def (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.

Examples:

Listening for ready event

client.on(:ready) do |user|
  puts "Logged in as #{user.username}"
end

Listening for messages

client.on(:message_create) do |message|
  if message.content == "ping"
    message.reply("pong")
  end
end

Parameters:

  • event (Symbol, String)

    The event name to listen for. Common events: ‘:ready`, `:message_create`, `:guild_create`, etc.

Yields:

  • (*args)

    The block to execute when the event fires. Block arguments vary by event type.

See Also:

  • For event triggering mechanism

Since:

  • 1.0.0



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

Yield Parameters:

Since:

  • 1.0.0



190
191
192
# File 'lib/rubord/structs/client.rb', line 190

def on_interaction(&block)
  on(:interaction_create, &block)
end

#on_message {|message| ... } ⇒ Object

Yield Parameters:

Since:

  • 1.0.0



185
186
187
# File 'lib/rubord/structs/client.rb', line 185

def on_message(&block)
  on(:message_create, &block)
end

#on_ready(&block) ⇒ Object

Since:

  • 1.0.0



180
181
182
# File 'lib/rubord/structs/client.rb', line 180

def on_ready(&block)
  on(:ready, &block)
end

#stopvoid

This method returns an undefined value.

Gracefully disconnects from Discord and stops the client.

This method closes the WebSocket connection and stops event processing.

Examples:

# Handle graceful shutdown
Signal.trap("INT") do
  puts "Shutting down..."
  client.stop
  exit
end

Since:

  • 1.0.0



176
177
178
# File 'lib/rubord/structs/client.rb', line 176

def stop
  @gateway&.close
end

#uptimeInteger

Returns the bot’s uptime in seconds since login.

Examples:

puts "Bot has been running for #{client.uptime} seconds"

Returns:

  • (Integer)

    Uptime in seconds, or 0 if not logged in.

Since:

  • 1.0.0



113
114
115
116
# File 'lib/rubord/structs/client.rb', line 113

def uptime
  return 0 unless @start_time
  @start_time
end