Module: OnyxCord::Bot::Runtime

Included in:
OnyxCord::Bot
Defined in:
lib/onyxcord/core/bot/runtime.rb

Instance Method Summary collapse

Instance Method Details

#connected?true, false

Returns whether or not the bot is currently connected to Discord.

Returns:

  • (true, false)

    whether or not the bot is currently connected to Discord.



44
45
46
# File 'lib/onyxcord/core/bot/runtime.rb', line 44

def connected?
  @gateway.open?
end

#debug=(new_debug) ⇒ Object

Sets debug mode. If debug mode is on, many things will be outputted to STDOUT.



53
54
55
# File 'lib/onyxcord/core/bot/runtime.rb', line 53

def debug=(new_debug)
  LOGGER.debug = new_debug
end

#joinObject Also known as: sync



31
32
33
# File 'lib/onyxcord/core/bot/runtime.rb', line 31

def join
  @run_task&.wait
end

#latencyObject



48
49
50
# File 'lib/onyxcord/core/bot/runtime.rb', line 48

def latency
  @gateway.latency
end

#mode=(new_mode) ⇒ Object

Sets the logging mode

See Also:



59
60
61
# File 'lib/onyxcord/core/bot/runtime.rb', line 59

def mode=(new_mode)
  LOGGER.mode = new_mode
end

#run(background = false) ⇒ Object

Note:

Running the bot in the background means that you can call some methods that require a gateway connection before that connection is established. In most cases an exception will be raised if you try to do this. If you need a way to safely run code after the bot is fully connected, use a EventContainer#ready event handler instead.

Runs the bot, which logs into Discord and connects the WebSocket. This prevents all further execution unless it is executed with background = true.

Parameters:

  • background (true, false) (defaults to: false)

    If it is true, then the bot will run in another thread to allow further execution. If it is false, this method will block until #stop is called. If the bot is run with true, make sure to eventually call #join so the script doesn't stop prematurely.



18
19
20
21
22
23
24
25
# File 'lib/onyxcord/core/bot/runtime.rb', line 18

def run(background = false)
  if background
    @run_task = Internal::AsyncRuntime.async { run_forever }
    return @run_task
  end

  Internal::AsyncRuntime.run { run_forever }
end

#run_foreverObject



27
28
29
# File 'lib/onyxcord/core/bot/runtime.rb', line 27

def run_forever
  @gateway.run
end

#runtime_statsObject



63
64
65
66
67
68
69
70
71
# File 'lib/onyxcord/core/bot/runtime.rb', line 63

def runtime_stats
  {
    mode: @mode,
    cache: cache_stats,
    event_executor: @event_executor.class.name,
    event_threads: @event_threads&.count(&:alive?) || 0,
    event_queue_size: @event_executor.respond_to?(:queue_size) ? @event_executor.queue_size : 0
  }
end

#stop(_no_sync = nil) ⇒ Object



36
37
38
39
40
41
# File 'lib/onyxcord/core/bot/runtime.rb', line 36

def stop(_no_sync = nil)
  @gateway.stop
  @event_executor.shutdown
  @run_task&.stop if @run_task.respond_to?(:stop)
  nil
end

#suppress_ready_debugObject

Prevents the READY packet from being printed regardless of debug mode.



74
75
76
# File 'lib/onyxcord/core/bot/runtime.rb', line 74

def suppress_ready_debug
  @prevent_ready = true
end