Class: Discorb::Client
- Inherits:
-
Object
- Object
- Discorb::Client
- Includes:
- Discorb::Command::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::Command::Command>
readonly
The commands that the client is using.
-
#emojis -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Emoji}
readonly
A dictionary of emojis.
-
#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.
-
#log -> Discorb::Logger
readonly
The logger.
-
#messages -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Message}
readonly
A dictionary of messages.
- #ping -> Float? readonly
-
#session_id -> Integer
readonly
The session ID of connection.
-
#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) -> Object
Dispatch an event.
-
#event_lock(event, timeout = nil, &check) -> Object
(also: #await)
Method to wait for a event.
-
#extend(mod) -> Object
Load the extension.
-
#fetch_application(force: false) -> Discorb::Application
Fetch webhook from ID.
-
#fetch_channel(id) -> Discorb::Channel
Fetch channel from ID.
-
#fetch_guild(id) -> Discorb::Guild
Fetch guild from ID.
-
#fetch_invite(code, with_count: false, with_expiration: false) -> Discorb::Invite
Fetch invite from code.
-
#fetch_nitro_sticker_packs -> Array<Discorb::Sticker::Pack>
Fetch nitro sticker pack from ID.
-
#fetch_user(id) -> Discorb::User
Fetch user from ID.
-
#initialize(allowed_mentions: nil, intents: nil, message_caches: 1000, log: nil, colorize_log: false, log_level: :info, wait_until_ready: true) -> Client
constructor
Initializes a new client.
- #inspect -> Object
-
#on(event_name, id: nil, **discriminator, &block) -> Discorb::Event
Registers an event handler.
-
#once(event_name, id: nil, **discriminator, &block) -> Discorb::Event
Almost same as #on, but only triggers the event once.
-
#remove_event(event_name, id) -> Object
Remove event by ID.
-
#run(token) -> Object
Starts the client.
-
#update_presence(activity = nil, status: nil) -> Object
(also: #change_presence)
Update presence of the client.
Methods included from Discorb::Command::Handler
#message_command, #setup_commands, #slash, #slash_group, #user_command
Constructor Details
#initialize(allowed_mentions: nil, intents: nil, message_caches: 1000, log: nil, colorize_log: false, log_level: :info, wait_until_ready: true) -> Client
Initializes a new client.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/discorb/client.rb', line 68 def initialize( allowed_mentions: nil, intents: nil, message_caches: 1000, log: nil, colorize_log: false, log_level: :info, wait_until_ready: true ) @allowed_mentions = allowed_mentions || AllowedMentions.new(everyone: true, roles: true, users: true) @intents = (intents or Intents.default) @events = {} @api_version = nil @log = Logger.new(log, colorize_log, log_level) @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 = [] @bottom_commands = [] @status = :initialized end |
Instance Attribute Details
#allowed_mentions -> Discorb::AllowedMentions (readonly)
Returns The allowed mentions that the client is using.
29 30 31 |
# File 'lib/discorb/client.rb', line 29 def allowed_mentions @allowed_mentions end |
#api_version -> Integer? (readonly)
25 26 27 |
# File 'lib/discorb/client.rb', line 25 def api_version @api_version end |
#application -> Discorb::Application? (readonly)
18 19 20 |
# File 'lib/discorb/client.rb', line 18 def application @application end |
#channels -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Channel} (readonly)
Returns A dictionary of channels.
37 38 39 |
# File 'lib/discorb/client.rb', line 37 def channels @channels end |
#commands -> Array<Discorb::Command::Command> (readonly)
Returns The commands that the client is using.
45 46 47 |
# File 'lib/discorb/client.rb', line 45 def commands @commands end |
#emojis -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Emoji} (readonly)
Returns A dictionary of emojis.
39 40 41 |
# File 'lib/discorb/client.rb', line 39 def emojis @emojis end |
#guilds -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Guild} (readonly)
Returns A dictionary of guilds.
33 34 35 |
# File 'lib/discorb/client.rb', line 33 def guilds @guilds end |
#heartbeat_interval -> Integer (readonly)
Returns The heartbeat interval.
22 23 24 |
# File 'lib/discorb/client.rb', line 22 def heartbeat_interval @heartbeat_interval end |
#http -> Discorb::HTTP (readonly)
Returns The http client.
20 21 22 |
# File 'lib/discorb/client.rb', line 20 def http @http end |
#intents -> Discorb::Intents
Returns The intents that the client is currently using.
15 16 17 |
# File 'lib/discorb/client.rb', line 15 def intents @intents end |
#log -> Discorb::Logger (readonly)
Returns The logger.
43 44 45 |
# File 'lib/discorb/client.rb', line 43 def log @log end |
#messages -> Discorb::Dictionary{Discorb::Snowflake => Discorb::Message} (readonly)
Returns A dictionary of messages.
41 42 43 |
# File 'lib/discorb/client.rb', line 41 def @messages end |
#ping -> Float? (readonly)
49 50 51 |
# File 'lib/discorb/client.rb', line 49 def ping @ping end |
#session_id -> Integer (readonly)
Returns The session ID of connection.
53 54 55 |
# File 'lib/discorb/client.rb', line 53 def session_id @session_id end |
#status -> :initialized, ... (readonly)
Returns The status of the client.
51 52 53 |
# File 'lib/discorb/client.rb', line 51 def status @status end |
#token -> String (readonly)
Returns The token of the client.
27 28 29 |
# File 'lib/discorb/client.rb', line 27 def token @token end |
#user -> Discorb::ClientUser (readonly)
Returns The client user.
31 32 33 |
# File 'lib/discorb/client.rb', line 31 def user @user end |
#users -> Discorb::Dictionary{Discorb::Snowflake => Discorb::User} (readonly)
Returns A dictionary of users.
35 36 37 |
# File 'lib/discorb/client.rb', line 35 def users @users end |
Instance Method Details
#close! -> Object
Stops the client.
446 447 448 449 450 451 |
# File 'lib/discorb/client.rb', line 446 def close! @connection.send_close @tasks.each(&:stop) @status = :closed @close_condition.signal end |
#dispatch(event_name, *args) -> Object
Dispatch an event.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/discorb/client.rb', line 145 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? @log.debug "Event #{event_name} doesn't have any proc, skipping" next end @log.debug "Dispatching event #{event_name}" events.each do |block| lambda { |event_args| Async(annotation: "Discorb event: #{event_name}") do |task| if block.is_a?(Discorb::Event) @events[event_name].delete(block) if block.discriminator[:once] end block.call(*event_args) @log.debug "Dispatched proc with ID #{block.id.inspect}" rescue StandardError, ScriptError => e = "An error occurred while dispatching proc with ID #{block.id.inspect}\n#{e.}" dispatch(:error, event_name, event_args, e) if @log.out @log.error else warn end end }.call(args) end end end |
#event_lock(event, timeout = nil, &check) -> Object Also known as: await
Method to wait for a event.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/discorb/client.rb', line 338 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 |
#extend(mod) -> Object
Load the extension.
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/discorb/client.rb', line 369 def extend(mod) if mod.respond_to?(:events) @events.each_value do |event| event.delete_if { |c| c.discriminator[:extension] == mod.name } end mod.events.each do |name, events| @events[name] = [] if @events[name].nil? events.each do |event| @events[name] << event end end @commands.delete_if do |cmd| cmd.respond_to? :extension and cmd.extension == mod.name end mod.commands.each do |cmd| cmd.define_singleton_method(:extension) { mod.name } @commands << cmd end @bottom_commands += mod.bottom_commands mod.client = self end super(mod) end |
#fetch_application(force: false) -> Discorb::Application
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch webhook from ID. If application was cached, it will be used.
280 281 282 283 284 285 286 287 288 |
# File 'lib/discorb/client.rb', line 280 def fetch_application(force: false) Async do next @application if @application && !force _resp, data = http.get("/oauth2/applications/@me").wait @application = Application.new(self, data) @application end end |
#fetch_channel(id) -> Discorb::Channel
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch channel from ID.
227 228 229 230 231 232 |
# File 'lib/discorb/client.rb', line 227 def fetch_channel(id) Async do _resp, data = http.get("/channels/#{id}").wait Channel.make_channel(self, data) end end |
#fetch_guild(id) -> Discorb::Guild
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch guild from ID.
245 246 247 248 249 250 |
# File 'lib/discorb/client.rb', line 245 def fetch_guild(id) Async do _resp, data = http.get("/guilds/#{id}").wait Guild.new(self, data, false) end end |
#fetch_invite(code, with_count: false, with_expiration: false) -> Discorb::Invite
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch invite from code.
263 264 265 266 267 268 |
# File 'lib/discorb/client.rb', line 263 def fetch_invite(code, with_count: false, with_expiration: false) Async do _resp, data = http.get("/invites/#{code}?with_count=#{with_count}&with_expiration=#{with_expiration}").wait Invite.new(self, data, false) end end |
#fetch_nitro_sticker_packs -> Array<Discorb::Sticker::Pack>
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch nitro sticker pack from ID.
297 298 299 300 301 302 |
# File 'lib/discorb/client.rb', line 297 def fetch_nitro_sticker_packs Async do _resp, data = http.get("/stickers-packs").wait data.map { |pack| Sticker::Pack.new(self, pack) } end end |
#fetch_user(id) -> Discorb::User
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch user from ID.
209 210 211 212 213 214 |
# File 'lib/discorb/client.rb', line 209 def fetch_user(id) Async do _resp, data = http.get("/users/#{id}").wait User.new(self, data) end end |
#inspect -> Object
360 361 362 |
# File 'lib/discorb/client.rb', line 360 def inspect "#<#{self.class} user=\"#{user}\">" end |
#on(event_name, id: nil, **discriminator, &block) -> Discorb::Event
Registers an event handler.
107 108 109 110 111 112 |
# File 'lib/discorb/client.rb', line 107 def on(event_name, id: nil, **discriminator, &block) ne = Event.new(block, id, discriminator) @events[event_name] ||= [] @events[event_name] << ne ne end |
#once(event_name, id: nil, **discriminator, &block) -> Discorb::Event
Almost same as #on, but only triggers the event once.
121 122 123 124 125 126 127 |
# File 'lib/discorb/client.rb', line 121 def once(event_name, id: nil, **discriminator, &block) discriminator[:once] = true ne = Event.new(block, id, discriminator) @events[event_name] ||= [] @events[event_name] << ne ne end |
#remove_event(event_name, id) -> Object
Remove event by ID.
135 136 137 |
# File 'lib/discorb/client.rb', line 135 def remove_event(event_name, id) @events[event_name].delete_if { |e| e.id == id } end |
#run(token) -> Object
This method behavior will change by CLI.
Starts the client.
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 |
# File 'lib/discorb/client.rb', line 403 def run(token) case ENV["DISCORB_CLI_FLAG"] when nil start_client(token) when "run" require "json" = JSON.parse(ENV["DISCORB_CLI_OPTIONS"], symbolize_names: true) Process.daemon if [:daemon] setup_commands(token) if [:setup] if [:log_level] if [:log_level] == "none" @log.out = nil else @log.out = case [:log_file] when nil, "stderr" $stderr when "stdout" $stdout else ::File.open([:log_file], "a") end @log.level = [:log_level].to_sym @log.colorize_log = case [:log_color] when nil if @log.out == $stdout || @log.out == $stderr true else false end when true, false [:log_color] end end end start_client(token) when "setup" setup_commands(token) end end |
#update_presence(activity = nil, status: nil) -> Object Also known as: change_presence
Update presence of the client.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/discorb/client.rb', line 310 def update_presence(activity = nil, status: nil) payload = {} if !activity.nil? payload[:activities] = [activity.to_hash] end payload[:status] = status unless status.nil? if @connection Async do send_gateway(3, **payload) end else @identify_presence = payload end end |