Module: OnyxCord::Bot::Awaits
- Included in:
- OnyxCord::Bot
- Defined in:
- lib/onyxcord/core/bot/awaits.rb
Instance Method Summary collapse
-
#add_await(key, type, attributes = {}) {|event| ... } ⇒ Await
deprecated
Deprecated.
Will be changed to blocking behavior in v4.0. Use #add_await! instead.
-
#add_await!(type, attributes = {}) {|event| ... } ⇒ Event?
Awaits an event, blocking the current thread until a response is received.
- #debug(message) ⇒ Object
-
#dispatch(type, data = nil) ⇒ Object
Dispatches an event to this bot.
-
#ignore_user(user) ⇒ Object
Add a user to the list of ignored users.
-
#ignored?(user) ⇒ true, false
Checks whether a user is being ignored.
- #log_exception(e) ⇒ Object
-
#raise_heartbeat_event ⇒ Object
Raises a heartbeat event.
-
#unignore_user(user) ⇒ Object
Remove a user from the ignore list.
Instance Method Details
#add_await(key, type, attributes = {}) {|event| ... } ⇒ Await
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.
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.
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
90 91 92 |
# File 'lib/onyxcord/core/bot/awaits.rb', line 90 def debug() LOGGER.debug() 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
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.
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.
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_event ⇒ Object
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.
78 79 80 |
# File 'lib/onyxcord/core/bot/awaits.rb', line 78 def unignore_user(user) @ignored_ids.delete(user.resolve_id) end |