Class: Discorb::Client
- Inherits:
-
Object
- Object
- Discorb::Client
- Includes:
- ApplicationCommand::Handler, Gateway::Handler
- Defined in:
- lib/discorb/client.rb
Overview
Class for connecting to the Discord server.
Instance Attribute Summary collapse
-
#allowed_mentions -> Discorb::AllowedMentions
readonly
The allowed mentions that the client is using.
- #api_version -> Integer? readonly
- #application -> Discorb::Application? readonly
-
#channels -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Channel}
readonly
A dictionary of channels.
-
#commands -> Array<Discorb::ApplicationCommand::Command>
readonly
The commands that the client is using.
-
#emojis -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Emoji}
readonly
A dictionary of emojis.
-
#extensions -> Hash{String => Discorb::Extension}
readonly
The loaded extensions.
-
#guilds -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Guild}
readonly
A dictionary of guilds.
-
#heartbeat_interval -> Integer
readonly
The heartbeat interval.
-
#http -> Discorb::HTTP
readonly
The http client.
-
#intents -> Discorb::Intents
The intents that the client is currently using.
-
#logger -> Logger
readonly
The logger.
-
#messages -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Message}
readonly
A dictionary of messages.
- #ping -> Float? readonly
- #session_id -> String? readonly
- #shard -> Discorb::Shard? readonly
- #shard_id -> Integer? readonly
-
#shards -> Hash{Integer => Discorb::Shard}
readonly
The shards of the client.
-
#status -> :initialized, ...
readonly
The status of the client.
-
#token -> String
readonly
The token of the client.
-
#user -> Discorb::ClientUser
readonly
The client user.
-
#users -> Discorb::Dictionary{Discorb::Snowflake => Discorb::User}
readonly
A dictionary of users.
Instance Method Summary collapse
-
#close -> Object
Stops the client.
-
#dispatch(event_name, *args) -> Async::Task<void>
Dispatch an event.
-
#event_lock(event, timeout = nil, &check) -> Async::Task<Object>
(also: #await)
Method to wait for a event.
-
#fetch_application(force: false) -> Async::Task<Discorb::Application>
Fetch webhook from ID.
-
#fetch_channel(id) -> Async::Task<Discorb::Channel>
Fetch channel from ID.
-
#fetch_guild(id) -> Async::Task<Discorb::Guild>
Fetch guild from ID.
-
#fetch_invite(code, with_count: true, with_expiration: true) -> Async::Task<Discorb::Invite>
Fetch invite from code.
-
#fetch_nitro_sticker_packs -> Async::Task<Array<Discorb::Sticker::Pack>>
Fetch nitro sticker pack from ID.
-
#fetch_user(id) -> Async::Task<Discorb::User>
Fetch user from ID.
-
#initialize(allowed_mentions: nil, intents: nil, message_caches: 1000, logger: nil, wait_until_ready: true, fetch_member: false, title: nil) -> Client
constructor
Initializes a new client.
- #inspect -> Object
-
#load_extension(ext) -> Object
Load the extension.
-
#on(event_name, id: nil, **metadata, &block) -> Discorb::EventHandler
Registers an event handler.
-
#once(event_name, id: nil, **metadata, &block) -> Discorb::EventHandler
Almost same as #on, but only triggers the event once.
-
#remove_event(event_name, id) -> Object
Remove event by ID.
-
#run(token = nil, shards: nil, shard_count: nil) -> Object
Starts the client.
-
#update_presence(activity = nil, status: nil) -> Object
(also: #change_presence)
Update presence of the client.
Methods included from ApplicationCommand::Handler
#clear_commands, #message_command, #setup_commands, #slash, #slash_group, #user_command
Constructor Details
#initialize(allowed_mentions: nil, intents: nil, message_caches: 1000, logger: nil, wait_until_ready: true, fetch_member: false, title: nil) -> Client
Initializes a new client.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/discorb/client.rb', line 87 def initialize( allowed_mentions: nil, intents: nil, message_caches: 1000, logger: nil, wait_until_ready: true, fetch_member: false, title: nil ) @allowed_mentions = allowed_mentions || AllowedMentions.new(everyone: true, roles: true, users: true) @intents = (intents or Intents.default) @events = {} @api_version = nil @logger = logger || Logger.new( $stdout, progname: "discorb", level: Logger::ERROR, ) @user = nil @users = Discorb::Dictionary.new @channels = Discorb::Dictionary.new @guilds = Discorb::Dictionary.new(sort: ->(k) { k[0].to_i }) @emojis = Discorb::Dictionary.new @messages = Discorb::Dictionary.new(limit: ) @application = nil @last_s = nil @identify_presence = nil @wait_until_ready = wait_until_ready @ready = false @tasks = [] @conditions = {} @commands = [] @callable_commands = [] @status = :initialized @fetch_member = fetch_member @title = title @extensions = {} @mutex = {} @shards = {} set_default_events end |
Instance Attribute Details
#allowed_mentions -> Discorb::AllowedMentions (readonly)
Returns The allowed mentions that the client is using.
30 31 32 |
# File 'lib/discorb/client.rb', line 30 def allowed_mentions @allowed_mentions end |
#api_version -> Integer? (readonly)
26 27 28 |
# File 'lib/discorb/client.rb', line 26 def api_version @api_version end |
#application -> Discorb::Application? (readonly)
19 20 21 |
# File 'lib/discorb/client.rb', line 19 def application @application end |
#channels -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Channel} (readonly)
Returns A dictionary of channels.
38 39 40 |
# File 'lib/discorb/client.rb', line 38 def channels @channels end |
#commands -> Array<Discorb::ApplicationCommand::Command> (readonly)
Returns The commands that the client is using.
44 45 46 |
# File 'lib/discorb/client.rb', line 44 def commands @commands end |
#emojis -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Emoji} (readonly)
Returns A dictionary of emojis.
40 41 42 |
# File 'lib/discorb/client.rb', line 40 def emojis @emojis end |
#extensions -> Hash{String => Discorb::Extension} (readonly)
Returns The loaded extensions.
52 53 54 |
# File 'lib/discorb/client.rb', line 52 def extensions @extensions end |
#guilds -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Guild} (readonly)
Returns A dictionary of guilds.
34 35 36 |
# File 'lib/discorb/client.rb', line 34 def guilds @guilds end |
#heartbeat_interval -> Integer (readonly)
Returns The heartbeat interval.
23 24 25 |
# File 'lib/discorb/client.rb', line 23 def heartbeat_interval @heartbeat_interval end |
#http -> Discorb::HTTP (readonly)
Returns The http client.
21 22 23 |
# File 'lib/discorb/client.rb', line 21 def http @http end |
#intents -> Discorb::Intents
Returns The intents that the client is currently using.
16 17 18 |
# File 'lib/discorb/client.rb', line 16 def intents @intents end |
#logger -> Logger (readonly)
Returns The logger.
|
# File 'lib/discorb/client.rb', line 62
|
#messages -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Message} (readonly)
Returns A dictionary of messages.
42 43 44 |
# File 'lib/discorb/client.rb', line 42 def @messages end |
#ping -> Float? (readonly)
48 49 50 |
# File 'lib/discorb/client.rb', line 48 def ping @ping end |
#session_id -> String?
|
# File 'lib/discorb/client.rb', line 62
|
#shard_id -> Integer? (readonly)
|
# File 'lib/discorb/client.rb', line 62
|
#shards -> Hash{Integer => Discorb::Shard} (readonly)
Returns The shards of the client.
54 55 56 |
# File 'lib/discorb/client.rb', line 54 def shards @shards end |
#status -> :initialized, ... (readonly)
Returns The status of the client.
50 51 52 |
# File 'lib/discorb/client.rb', line 50 def status @status end |
#token -> String (readonly)
Returns The token of the client.
28 29 30 |
# File 'lib/discorb/client.rb', line 28 def token @token end |
#user -> Discorb::ClientUser (readonly)
Returns The client user.
32 33 34 |
# File 'lib/discorb/client.rb', line 32 def user @user end |
#users -> Discorb::Dictionary{Discorb::Snowflake => Discorb::User} (readonly)
Returns A dictionary of users.
36 37 38 |
# File 'lib/discorb/client.rb', line 36 def users @users end |
Instance Method Details
#close -> Object
Stops the client.
476 477 478 479 480 481 482 483 484 |
# File 'lib/discorb/client.rb', line 476 def close if @shards.any? @shards.each_value(&:close) else @connection.send_close end @tasks.each(&:stop) @status = :closed end |
#dispatch(event_name, *args) -> Async::Task<void>
Dispatch an event.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/discorb/client.rb', line 176 def dispatch(event_name, *args) Async do if (conditions = @conditions[event_name]) ids = Set[*conditions.map(&:first).map(&:object_id)] conditions.delete_if do |condition| next unless ids.include?(condition.first.object_id) check_result = condition[1].nil? || condition[1].call(*args) if check_result condition.first.signal(args) true else false end end end events = @events[event_name].dup || [] if respond_to?("on_" + event_name.to_s) event_method = method("on_" + event_name.to_s) class << event_method def id "method" end end events << event_method end if events.nil? logger.debug "Event #{event_name} doesn't have any proc, skipping" next end logger.debug "Dispatching event #{event_name}" events.each do |block| Async do Async(annotation: "Discorb event: #{event_name}") do |_task| @events[event_name].delete(block) if block.is_a?(Discorb::EventHandler) && block.[:once] block.call(*args) logger.debug "Dispatched proc with ID #{block.id.inspect}" rescue StandardError, ScriptError => e if event_name == :error raise e else dispatch(:error, event_name, args, e) end end end end end end |
#event_lock(event, timeout = nil, &check) -> Async::Task<Object> Also known as: await
Method to wait for a event.
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/discorb/client.rb', line 369 def event_lock(event, timeout = nil, &check) Async do |task| condition = Async::Condition.new @conditions[event] ||= [] @conditions[event] << [condition, check] if timeout.nil? value = condition.wait else timeout_task = task.with_timeout(timeout) do condition.wait rescue Async::TimeoutError @conditions[event].delete_if { |c| c.first == condition } raise Discorb::TimeoutError, "Timeout waiting for event #{event}", cause: nil end value = timeout_task end value.length <= 1 ? value.first : value end end |
#fetch_application(force: false) -> Async::Task<Discorb::Application>
Fetch webhook from ID. If application was cached, it will be used.
308 309 310 311 312 313 314 315 316 |
# File 'lib/discorb/client.rb', line 308 def fetch_application(force: false) Async do next @application if @application && !force _resp, data = @http.request(Route.new("/oauth2/applications/@me", "//oauth2/applications/@me", :get)).wait @application = Application.new(self, data) @application end end |
#fetch_channel(id) -> Async::Task<Discorb::Channel>
Fetch channel from ID.
252 253 254 255 256 257 |
# File 'lib/discorb/client.rb', line 252 def fetch_channel(id) Async do _resp, data = @http.request(Route.new("/channels/#{id}", "//channels/:channel_id", :get)).wait Channel.make_channel(self, data) end end |
#fetch_guild(id) -> Async::Task<Discorb::Guild>
Fetch guild from ID.
269 270 271 272 273 274 |
# File 'lib/discorb/client.rb', line 269 def fetch_guild(id) Async do _resp, data = @http.request(Route.new("/guilds/#{id}", "//guilds/:guild_id", :get)).wait Guild.new(self, data, false) end end |
#fetch_invite(code, with_count: true, with_expiration: true) -> Async::Task<Discorb::Invite>
Fetch invite from code.
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/discorb/client.rb', line 286 def fetch_invite(code, with_count: true, with_expiration: true) Async do _resp, data = @http.request( Route.new( "/invites/#{code}?with_count=#{with_count}&with_expiration=#{with_expiration}", "//invites/:code", :get ) ).wait Invite.new(self, data, false) end end |
#fetch_nitro_sticker_packs -> Async::Task<Array<Discorb::Sticker::Pack>>
Fetch nitro sticker pack from ID.
324 325 326 327 328 329 |
# File 'lib/discorb/client.rb', line 324 def fetch_nitro_sticker_packs Async do _resp, data = @http.request(Route.new("/sticker-packs", "//sticker-packs", :get)).wait data[:sticker_packs].map { |pack| Sticker::Pack.new(self, pack) } end end |
#fetch_user(id) -> Async::Task<Discorb::User>
Fetch user from ID.
235 236 237 238 239 240 |
# File 'lib/discorb/client.rb', line 235 def fetch_user(id) Async do _resp, data = @http.request(Route.new("/users/#{id}", "//users/:user_id", :get)).wait User.new(self, data) end end |
#inspect -> Object
391 392 393 |
# File 'lib/discorb/client.rb', line 391 def inspect "#<#{self.class} user=\"#{user}\">" end |
#load_extension(ext) -> Object
Load the extension.
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/discorb/client.rb', line 401 def load_extension(ext, ...) case ext when Class raise ArgumentError, "#{ext} is not a extension" unless ext < Discorb::Extension ins = ext.new(self, ...) when Discorb::Extension ins = ext else raise ArgumentError, "#{ext} is not a extension" end @events.each_value do |event| event.delete_if { |c| c.[:extension] == ins.class.name } end ins.events.each do |name, events| @events[name] ||= [] events.each do |event| @events[name] << event end end @commands.delete_if do |cmd| cmd.respond_to? :extension and cmd.extension == ins.class.name end ins.class.commands.each do |cmd| cmd.define_singleton_method(:extension) { ins.class.name } cmd.replace_block(ins) cmd.block.define_singleton_method(:self_replaced) { true } @commands << cmd end cls = ins.class cls.loaded(self, ...) if cls.respond_to? :loaded ins.class.callable_commands.each do |cmd| unless cmd.respond_to? :self_replaced cmd.define_singleton_method(:extension) { ins.class.name } cmd.replace_block(ins) cmd.block.define_singleton_method(:self_replaced) { true } end @callable_commands << cmd end @extensions[ins.class.name] = ins ins end |
#on(event_name, id: nil, **metadata, &block) -> Discorb::EventHandler
Registers an event handler.
137 138 139 140 141 142 143 |
# File 'lib/discorb/client.rb', line 137 def on(event_name, id: nil, **, &block) ne = EventHandler.new(block, id, ) @events[event_name] ||= [] @events[event_name].delete_if { |e| e.[:override] } @events[event_name] << ne ne end |
#once(event_name, id: nil, **metadata, &block) -> Discorb::EventHandler
Almost same as #on, but only triggers the event once.
152 153 154 155 |
# File 'lib/discorb/client.rb', line 152 def once(event_name, id: nil, **, &block) [:once] = true on(event_name, id: id, **, &block) end |
#remove_event(event_name, id) -> Object
Remove event by ID.
163 164 165 |
# File 'lib/discorb/client.rb', line 163 def remove_event(event_name, id) @events[event_name].delete_if { |e| e.id == id } end |
#run(token = nil, shards: nil, shard_count: nil) -> Object
This method behavior will change by CLI.
If the token is nil, you should use discorb run
with the -e
or --env
option.
Starts the client.
458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/discorb/client.rb', line 458 def run(token = nil, shards: nil, shard_count: nil) token ||= ENV.fetch("DISCORB_CLI_TOKEN", nil) raise ArgumentError, "Token is not specified, and -e/--env is not specified" if token.nil? case ENV.fetch("DISCORB_CLI_FLAG", nil) when nil start_client(token, shards: shards, shard_count: shard_count) when "run" before_run(token) start_client(token, shards: shards, shard_count: shard_count) when "setup" run_setup(token) end end |
#update_presence(activity = nil, status: nil) -> Object Also known as: change_presence
Update presence of the client.
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/discorb/client.rb', line 337 def update_presence(activity = nil, status: nil) payload = { activities: [], status: status, since: nil, afk: nil, } payload[:activities] = [activity.to_hash] unless activity.nil? payload[:status] = status unless status.nil? if connection Async do send_gateway(3, **payload) end else @identify_presence = payload end end |