Class: Discordrb::Await
- Inherits:
-
Object
- Object
- Discordrb::Await
- 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
-
#attributes ⇒ Hash
readonly
The attributes of the event that will be listened for.
-
#key ⇒ Symbol
readonly
The key that uniquely identifies this await.
-
#type ⇒ Class
readonly
The class of the event that this await listens for.
Instance Method Summary collapse
-
#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.
Instance Attribute Details
#attributes ⇒ Hash (readonly)
The attributes of the event that will be listened for.
26 27 28 |
# File 'lib/discordrb/await.rb', line 26 def attributes @attributes end |
#key ⇒ Symbol (readonly)
The key that uniquely identifies this await.
18 19 20 |
# File 'lib/discordrb/await.rb', line 18 def key @key end |
#type ⇒ Class (readonly)
The class of the event that this await listens for.
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.
42 43 44 45 46 47 48 49 |
# 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 = true if (@block && @block.call(event) != false) || !@block [@key, should_delete] end |