Class: Discordrb::Await

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/await.rb

Overview

Awaits are a way to register new, temporary event handlers on the fly. Awaits can be registered using Bot#add_await, User#await, Message#await and Channel#await.

Awaits contain a block that will be called before the await event will be triggered. If this block returns anything that is not false exactly, the await will be deleted. If no block is present, the await will also be deleted. This is an easy way to make temporary events that are only temporary under certain conditions.

Besides the given block, an Events::AwaitEvent will also be executed with the key and the type of the await that was triggered. It's possible to register multiple events that trigger on the same await.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesHash (readonly)

The attributes of the event that will be listened for.

Returns:

  • (Hash)

    A hash of attributes.


26
27
28
# File 'lib/discordrb/await.rb', line 26

def attributes
  @attributes
end

#keySymbol (readonly)

The key that uniquely identifies this await.

Returns:

  • (Symbol)

    The unique key.


18
19
20
# File 'lib/discordrb/await.rb', line 18

def key
  @key
end

#typeClass (readonly)

The class of the event that this await listens for.

Returns:

  • (Class)

    The event class.


22
23
24
# File 'lib/discordrb/await.rb', line 22

def type
  @type
end

Instance Method Details

#match(event) ⇒ Array

Checks whether the await can be triggered by the given event, and if it can, execute the block and return its result along with this await's key.

Parameters:

  • event (Event)

    An event to check for.

Returns:

  • (Array)

    This await's key and whether or not it should be deleted. If there was no match, both are nil.


42
43
44
45
46
47
48
49
50
# File 'lib/discordrb/await.rb', line 42

def match(event)
  dummy_handler = EventContainer.handler_class(@type).new(@attributes, @bot)
  return [nil, nil] unless event.instance_of?(@type) && dummy_handler.matches?(event)

  should_delete = nil
  should_delete = true if (@block && @block.call(event) != false) || !@block

  [@key, should_delete]
end