Module: OnyxCord::Bot::Awaits

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

Instance Method Summary collapse

Instance Method Details

#add_await(key, type, attributes = {}) {|event| ... } ⇒ Await

Deprecated.

Will be changed to blocking behavior in v4.0. Use #add_await! instead.

Add an await the bot should listen to. For information on awaits, see Await.

Parameters:

  • key (Symbol)

    The key that uniquely identifies the await for Events::AwaitEvents to listen to (see EventContainer#await).

  • type (Class)

    The event class that should be listened for.

  • attributes (Hash) (defaults to: {})

    The attributes the event should check for. The block will only be executed if all attributes match.

Yields:

  • Is executed when the await is triggered.

Yield Parameters:

  • event (Event)

    The event object that was triggered.

Returns:

  • (Await)

    The await that was created.



14
15
16
17
18
19
20
# File 'lib/onyxcord/core/bot/awaits.rb', line 14

def add_await(key, type, attributes = {}, &block)
  raise "You can't await an AwaitEvent!" if type == OnyxCord::Events::AwaitEvent

  await = Await.new(self, key, type, attributes, block)
  @awaits ||= {}
  @awaits[key] = await
end

#add_await!(type, attributes = {}) {|event| ... } ⇒ Event?

Awaits an event, blocking the current thread until a response is received.

Parameters:

  • type (Class)

    The event class that should be listened for.

  • attributes (Hash) (defaults to: {})

    a customizable set of options

Options Hash (attributes):

  • :timeout (Numeric)

    the amount of time (in seconds) to wait for a response before returning nil. Waits forever if omitted.

Yields:

  • Executed when a matching event is received.

Yield Parameters:

  • event (Event)

    The event object that was triggered.

Yield Returns:

  • (true, false)

    Whether the event matches extra await criteria described by the block

Returns:

  • (Event, nil)

    The event object that was triggered, or nil if a timeout was set and no event was raised in time.

Raises:

  • (ArgumentError)

    if timeout is given and is not a positive numeric value



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/onyxcord/core/bot/awaits.rb', line 30

def add_await!(type, attributes = {})
  raise "You can't await an AwaitEvent!" if type == OnyxCord::Events::AwaitEvent

  timeout = attributes[:timeout]
  raise ArgumentError, 'Timeout must be a number > 0' if timeout.is_a?(Numeric) && !timeout.positive?

  mutex = Mutex.new
  cv = ConditionVariable.new
  response = nil
  block = lambda do |event|
    mutex.synchronize do
      response = event
      if block_given?
        result = yield(event)
        cv.signal if result.is_a?(TrueClass)
      else
        cv.signal
      end
    end
  end

  handler = register_event(type, attributes, block)

  if timeout
    Thread.new do
      sleep timeout
      mutex.synchronize { cv.signal }
    end
  end

  mutex.synchronize { cv.wait(mutex) }

  remove_handler(handler)
  raise 'ConditionVariable was signaled without returning an event!' if response.nil? && timeout.nil?

  response
end

#debug(message) ⇒ Object

See Also:

  • Logger#debug


90
91
92
# File 'lib/onyxcord/core/bot/awaits.rb', line 90

def debug(message)
  LOGGER.debug(message)
end

#dispatch(type, data = nil) ⇒ Object

Dispatches an event to this bot. Called by the gateway connection handler used internally.



100
101
102
103
104
# File 'lib/onyxcord/core/bot/awaits.rb', line 100

def dispatch(type, data = nil)
  return dispatch_packet(type) if data.nil? && type.is_a?(Hash)

  handle_dispatch(type, data)
end

#ignore_user(user) ⇒ Object

Note:

Ignoring a user only prevents any message events (including mentions, commands etc.) from them! Typing and presence and any other events will still be received.

Add a user to the list of ignored users. Those users will be ignored in message events at event processing level.

Parameters:



72
73
74
# File 'lib/onyxcord/core/bot/awaits.rb', line 72

def ignore_user(user)
  @ignored_ids << user.resolve_id
end

#ignored?(user) ⇒ true, false

Checks whether a user is being ignored.

Parameters:

Returns:

  • (true, false)

    whether or not the user is ignored.



85
86
87
# File 'lib/onyxcord/core/bot/awaits.rb', line 85

def ignored?(user)
  @ignored_ids.include?(user.resolve_id)
end

#log_exception(e) ⇒ Object



95
96
97
# File 'lib/onyxcord/core/bot/awaits.rb', line 95

def log_exception(e)
  LOGGER.log_exception(e)
end

#raise_heartbeat_eventObject

Raises a heartbeat event. Called by the gateway connection handler used internally.



107
108
109
# File 'lib/onyxcord/core/bot/awaits.rb', line 107

def raise_heartbeat_event
  raise_event(OnyxCord::Events::HeartbeatEvent.new(self))
end

#unignore_user(user) ⇒ Object

Remove a user from the ignore list.

Parameters:



78
79
80
# File 'lib/onyxcord/core/bot/awaits.rb', line 78

def unignore_user(user)
  @ignored_ids.delete(user.resolve_id)
end