Module: Discordrb::EventContainer
Overview
This module provides the functionality required for events and awaits. It is separated from the Bot class so users can make their own container modules and include them.
Class Method Summary collapse
-
.class_from_string(str) ⇒ Class
Utility method to return a class object from a string of its name.
-
.event_class(handler_class) ⇒ Class?
Returns the event class for a handler class type.
-
.handler_class(event_class) ⇒ Class
Returns the handler class for an event class type.
Instance Method Summary collapse
-
#add_handler(handler) ⇒ Object
(also: #<<)
Adds an event handler to this container.
-
#await(attributes = {}) {|event| ... } ⇒ AwaitEventHandler
This event is raised when an Await is triggered.
-
#channel_create(attributes = {}) {|event| ... } ⇒ ChannelCreateEventHandler
This event is raised when a channel is created.
-
#channel_delete(attributes = {}) {|event| ... } ⇒ ChannelDeleteEventHandler
This event is raised when a channel is deleted.
-
#channel_recipient_add(attributes = {}) {|event| ... } ⇒ ChannelRecipientAddHandler
This event is raised when a recipient is added to a group channel.
-
#channel_recipient_remove(attributes = {}) {|event| ... } ⇒ ChannelRecipientRemoveHandler
This event is raised when a recipient is removed from a group channel.
-
#channel_update(attributes = {}) {|event| ... } ⇒ ChannelUpdateEventHandler
This event is raised when a channel is updated.
-
#clear! ⇒ Object
Removes all events from this event handler.
-
#disconnected(attributes = {}) {|event| ... } ⇒ DisconnectEventHandler
This event is raised when the bot has disconnected from the WebSocket, due to the Bot#stop method or external causes.
-
#heartbeat(attributes = {}) {|event| ... } ⇒ HeartbeatEventHandler
This event is raised every time the bot sends a heartbeat over the galaxy.
-
#include_events(container) ⇒ Object
(also: #include!)
Adds all event handlers from another container into this one.
-
#member_join(attributes = {}) {|event| ... } ⇒ ServerMemberAddEventHandler
This event is raised when a new user joins a server.
-
#member_leave(attributes = {}) {|event| ... } ⇒ ServerMemberDeleteEventHandler
This event is raised when a member leaves a server.
-
#member_update(attributes = {}) {|event| ... } ⇒ ServerMemberUpdateEventHandler
This event is raised when a member update happens.
-
#mention(attributes = {}) {|event| ... } ⇒ MentionEventHandler
This event is raised when the bot is mentioned in a message.
-
#message(attributes = {}) {|event| ... } ⇒ MessageEventHandler
This event is raised when a message is sent to a text channel the bot is currently in.
-
#message_delete(attributes = {}) {|event| ... } ⇒ MessageDeleteEventHandler
This event is raised when a message is deleted in a channel.
-
#message_edit(attributes = {}) {|event| ... } ⇒ MessageEditEventHandler
This event is raised when a message is edited in a channel.
-
#playing(attributes = {}) {|event| ... } ⇒ PlayingEventHandler
This event is raised when the game a user is playing changes.
-
#pm(attributes = {}) {|event| ... } ⇒ PrivateMessageEventHandler
(also: #private_message, #direct_message, #dm)
This event is raised when a private message is sent to the bot.
-
#presence(attributes = {}) {|event| ... } ⇒ PresenceEventHandler
This event is raised when a user's status (online/offline/idle) changes.
-
#raw(attributes = {}) {|event| ... } ⇒ RawEventHandler
This event is raised for every dispatch received over the gateway, whether supported by discordrb or not.
-
#reaction_add(attributes = {}) {|event| ... } ⇒ ReactionAddEventHandler
This event is raised when somebody reacts to a message.
-
#reaction_remove(attributes = {}) {|event| ... } ⇒ ReactionRemoveEventHandler
This event is raised when somebody removes a reaction from a message.
-
#reaction_remove_all(attributes = {}) {|event| ... } ⇒ ReactionRemoveAllEventHandler
This event is raised when somebody removes all reactions from a message.
-
#ready(attributes = {}) {|event| ... } ⇒ ReadyEventHandler
This event is raised when the READY packet is received, i.
-
#remove_handler(handler) ⇒ Object
Removes an event handler from this container.
-
#server_create(attributes = {}) {|event| ... } ⇒ ServerCreateEventHandler
This event is raised when a server is created respective to the bot, i.
-
#server_delete(attributes = {}) {|event| ... } ⇒ ServerDeleteEventHandler
This event is raised when a server is deleted, or when the bot leaves a server.
-
#server_emoji(attributes = {}) {|event| ... } ⇒ ServerEmojiChangeEventHandler
This event is raised when an emoji or collection of emojis is created/deleted/updated.
-
#server_emoji_create(attributes = {}) {|event| ... } ⇒ ServerEmojiCreateEventHandler
This event is raised when an emoji is created.
-
#server_emoji_delete(attributes = {}) {|event| ... } ⇒ ServerEmojiDeleteEventHandler
This event is raised when an emoji is deleted.
-
#server_emoji_update(attributes = {}) {|event| ... } ⇒ ServerEmojiUpdateEventHandler
This event is raised when an emoji is updated.
-
#server_update(attributes = {}) {|event| ... } ⇒ ServerUpdateEventHandler
This event is raised when a server is updated, for example if the name or region has changed.
-
#typing(attributes = {}) {|event| ... } ⇒ TypingEventHandler
This event is raised when somebody starts typing in a channel the bot is also in.
-
#unknown(attributes = {}) {|event| ... } ⇒ UnknownEventHandler
This event is raised for a dispatch received over the gateway that is not currently handled otherwise by discordrb.
-
#user_ban(attributes = {}) {|event| ... } ⇒ UserBanEventHandler
This event is raised when a user is banned from a server.
-
#user_unban(attributes = {}) {|event| ... } ⇒ UserUnbanEventHandler
This event is raised when a user is unbanned from a server.
-
#voice_state_update(attributes = {}) {|event| ... } ⇒ VoiceStateUpdateEventHandler
This event is raised when a user's voice state changes.
Methods included from Events
Class Method Details
.class_from_string(str) ⇒ Class
Utility method to return a class object from a string of its name. Mostly useful for internal stuff
501 502 503 504 505 |
# File 'lib/discordrb/container.rb', line 501 def self.class_from_string(str) str.split('::').inject(Object) do |mod, class_name| mod.const_get(class_name) end end |
.event_class(handler_class) ⇒ Class?
Returns the event class for a handler class type
491 492 493 494 495 496 |
# File 'lib/discordrb/container.rb', line 491 def self.event_class(handler_class) class_name = handler_class.to_s return nil unless class_name.end_with? 'Handler' EventContainer.class_from_string(class_name[0..-8]) end |
.handler_class(event_class) ⇒ Class
Returns the handler class for an event class type
483 484 485 |
# File 'lib/discordrb/container.rb', line 483 def self.handler_class(event_class) class_from_string(event_class.to_s + 'Handler') end |
Instance Method Details
#add_handler(handler) ⇒ Object Also known as: <<
Adds an event handler to this container. Usually, it's more expressive to just use one of the shorthand adder methods like #message, but if you want to create one manually you can use this.
460 461 462 463 464 |
# File 'lib/discordrb/container.rb', line 460 def add_handler(handler) clazz = EventContainer.event_class(handler.class) @event_handlers ||= {} @event_handlers[clazz] << handler end |
#await(attributes = {}) {|event| ... } ⇒ AwaitEventHandler
This event is raised when an Await is triggered. It provides an easy way to execute code on an await without having to rely on the await's block.
396 397 398 |
# File 'lib/discordrb/container.rb', line 396 def await(attributes = {}, &block) register_event(AwaitEvent, attributes, block) end |
#channel_create(attributes = {}) {|event| ... } ⇒ ChannelCreateEventHandler
This event is raised when a channel is created.
190 191 192 |
# File 'lib/discordrb/container.rb', line 190 def channel_create(attributes = {}, &block) register_event(ChannelCreateEvent, attributes, block) end |
#channel_delete(attributes = {}) {|event| ... } ⇒ ChannelDeleteEventHandler
This event is raised when a channel is deleted.
212 213 214 |
# File 'lib/discordrb/container.rb', line 212 def channel_delete(attributes = {}, &block) register_event(ChannelDeleteEvent, attributes, block) end |
#channel_recipient_add(attributes = {}) {|event| ... } ⇒ ChannelRecipientAddHandler
This event is raised when a recipient is added to a group channel.
224 225 226 |
# File 'lib/discordrb/container.rb', line 224 def channel_recipient_add(attributes = {}, &block) register_event(ChannelRecipientAddEvent, attributes, block) end |
#channel_recipient_remove(attributes = {}) {|event| ... } ⇒ ChannelRecipientRemoveHandler
This event is raised when a recipient is removed from a group channel.
236 237 238 |
# File 'lib/discordrb/container.rb', line 236 def channel_recipient_remove(attributes = {}, &block) register_event(ChannelRecipientRemoveEvent, attributes, block) end |
#channel_update(attributes = {}) {|event| ... } ⇒ ChannelUpdateEventHandler
This event is raised when a channel is updated.
201 202 203 |
# File 'lib/discordrb/container.rb', line 201 def channel_update(attributes = {}, &block) register_event(ChannelUpdateEvent, attributes, block) end |
#clear! ⇒ Object
Removes all events from this event handler.
453 454 455 |
# File 'lib/discordrb/container.rb', line 453 def clear! @event_handlers.clear if @event_handlers end |
#disconnected(attributes = {}) {|event| ... } ⇒ DisconnectEventHandler
This event is raised when the bot has disconnected from the WebSocket, due to the Bot#stop method or external causes. It's the recommended way to do clean-up tasks.
56 57 58 |
# File 'lib/discordrb/container.rb', line 56 def disconnected(attributes = {}, &block) register_event(DisconnectEvent, attributes, block) end |
#heartbeat(attributes = {}) {|event| ... } ⇒ HeartbeatEventHandler
This event is raised every time the bot sends a heartbeat over the galaxy. This happens roughly every 40 seconds, but may happen at a lower rate should Discord change their interval. It may also happen more quickly for periods of time, especially for unstable connections, since discordrb rather sends a heartbeat than not if there's a choice. (You shouldn't rely on all this to be accurately timed.)
All this makes this event useful to periodically trigger something, like doing some API request every hour, setting some kind of uptime variable or whatever else. The only limit is yourself.
71 72 73 |
# File 'lib/discordrb/container.rb', line 71 def heartbeat(attributes = {}, &block) register_event(HeartbeatEvent, attributes, block) end |
#include_events(container) ⇒ Object Also known as: include!
Adds all event handlers from another container into this one. Existing event handlers will be overwritten.
468 469 470 471 472 473 474 |
# File 'lib/discordrb/container.rb', line 468 def include_events(container) handlers = container.instance_variable_get '@event_handlers' return unless handlers @event_handlers ||= {} @event_handlers.merge!(handlers) { |_, old, new| old + new } end |
#member_join(attributes = {}) {|event| ... } ⇒ ServerMemberAddEventHandler
This event is raised when a new user joins a server.
262 263 264 |
# File 'lib/discordrb/container.rb', line 262 def member_join(attributes = {}, &block) register_event(ServerMemberAddEvent, attributes, block) end |
#member_leave(attributes = {}) {|event| ... } ⇒ ServerMemberDeleteEventHandler
This event is raised when a member leaves a server.
282 283 284 |
# File 'lib/discordrb/container.rb', line 282 def member_leave(attributes = {}, &block) register_event(ServerMemberDeleteEvent, attributes, block) end |
#member_update(attributes = {}) {|event| ... } ⇒ ServerMemberUpdateEventHandler
This event is raised when a member update happens.
272 273 274 |
# File 'lib/discordrb/container.rb', line 272 def member_update(attributes = {}, &block) register_event(ServerMemberUpdateEvent, attributes, block) end |
#mention(attributes = {}) {|event| ... } ⇒ MentionEventHandler
This event is raised when the bot is mentioned in a message.
179 180 181 |
# File 'lib/discordrb/container.rb', line 179 def mention(attributes = {}, &block) register_event(MentionEvent, attributes, block) end |
#message(attributes = {}) {|event| ... } ⇒ MessageEventHandler
This event is raised when a message is sent to a text channel the bot is currently in.
36 37 38 |
# File 'lib/discordrb/container.rb', line 36 def (attributes = {}, &block) register_event(MessageEvent, attributes, block) end |
#message_delete(attributes = {}) {|event| ... } ⇒ MessageDeleteEventHandler
This event is raised when a message is deleted in a channel.
108 109 110 |
# File 'lib/discordrb/container.rb', line 108 def (attributes = {}, &block) register_event(MessageDeleteEvent, attributes, block) end |
#message_edit(attributes = {}) {|event| ... } ⇒ MessageEditEventHandler
This event is raised when a message is edited in a channel.
97 98 99 |
# File 'lib/discordrb/container.rb', line 97 def (attributes = {}, &block) register_event(MessageEditEvent, attributes, block) end |
#playing(attributes = {}) {|event| ... } ⇒ PlayingEventHandler
This event is raised when the game a user is playing changes.
161 162 163 |
# File 'lib/discordrb/container.rb', line 161 def (attributes = {}, &block) register_event(PlayingEvent, attributes, block) end |
#pm(attributes = {}) {|event| ... } ⇒ PrivateMessageEventHandler Also known as: private_message, direct_message, dm
This event is raised when a private message is sent to the bot.
414 415 416 |
# File 'lib/discordrb/container.rb', line 414 def pm(attributes = {}, &block) register_event(PrivateMessageEvent, attributes, block) end |
#presence(attributes = {}) {|event| ... } ⇒ PresenceEventHandler
This event is raised when a user's status (online/offline/idle) changes.
149 150 151 |
# File 'lib/discordrb/container.rb', line 149 def presence(attributes = {}, &block) register_event(PresenceEvent, attributes, block) end |
#raw(attributes = {}) {|event| ... } ⇒ RawEventHandler
This event is raised for every dispatch received over the gateway, whether supported by discordrb or not.
428 429 430 |
# File 'lib/discordrb/container.rb', line 428 def raw(attributes = {}, &block) register_event(RawEvent, attributes, block) end |
#reaction_add(attributes = {}) {|event| ... } ⇒ ReactionAddEventHandler
This event is raised when somebody reacts to a message.
118 119 120 |
# File 'lib/discordrb/container.rb', line 118 def reaction_add(attributes = {}, &block) register_event(ReactionAddEvent, attributes, block) end |
#reaction_remove(attributes = {}) {|event| ... } ⇒ ReactionRemoveEventHandler
This event is raised when somebody removes a reaction from a message.
129 130 131 |
# File 'lib/discordrb/container.rb', line 129 def reaction_remove(attributes = {}, &block) register_event(ReactionRemoveEvent, attributes, block) end |
#reaction_remove_all(attributes = {}) {|event| ... } ⇒ ReactionRemoveAllEventHandler
This event is raised when somebody removes all reactions from a message.
138 139 140 |
# File 'lib/discordrb/container.rb', line 138 def reaction_remove_all(attributes = {}, &block) register_event(ReactionRemoveAllEvent, attributes, block) end |
#ready(attributes = {}) {|event| ... } ⇒ ReadyEventHandler
This event is raised when the READY packet is received, i. e. servers and channels have finished initialization. It's the recommended way to do things when the bot has finished starting up.
46 47 48 |
# File 'lib/discordrb/container.rb', line 46 def ready(attributes = {}, &block) register_event(ReadyEvent, attributes, block) end |
#remove_handler(handler) ⇒ Object
Removes an event handler from this container. If you're looking for a way to do temporary events, I recommend Awaits instead of this.
446 447 448 449 450 |
# File 'lib/discordrb/container.rb', line 446 def remove_handler(handler) clazz = EventContainer.event_class(handler.class) @event_handlers ||= {} @event_handlers[clazz].delete(handler) end |
#server_create(attributes = {}) {|event| ... } ⇒ ServerCreateEventHandler
This event is raised when a server is created respective to the bot, i. e. the bot joins a server or creates a new one itself. It should never be necessary to listen to this event as it will only ever be triggered by things the bot itself does, but one can never know.
316 317 318 |
# File 'lib/discordrb/container.rb', line 316 def server_create(attributes = {}, &block) register_event(ServerCreateEvent, attributes, block) end |
#server_delete(attributes = {}) {|event| ... } ⇒ ServerDeleteEventHandler
This event is raised when a server is deleted, or when the bot leaves a server. (These two cases are identical to Discord.)
337 338 339 |
# File 'lib/discordrb/container.rb', line 337 def server_delete(attributes = {}, &block) register_event(ServerDeleteEvent, attributes, block) end |
#server_emoji(attributes = {}) {|event| ... } ⇒ ServerEmojiChangeEventHandler
This event is raised when an emoji or collection of emojis is created/deleted/updated.
347 348 349 |
# File 'lib/discordrb/container.rb', line 347 def server_emoji(attributes = {}, &block) register_event(ServerEmojiChangeEvent, attributes, block) end |
#server_emoji_create(attributes = {}) {|event| ... } ⇒ ServerEmojiCreateEventHandler
This event is raised when an emoji is created.
359 360 361 |
# File 'lib/discordrb/container.rb', line 359 def server_emoji_create(attributes = {}, &block) register_event(ServerEmojiCreateEvent, attributes, block) end |
#server_emoji_delete(attributes = {}) {|event| ... } ⇒ ServerEmojiDeleteEventHandler
This event is raised when an emoji is deleted.
371 372 373 |
# File 'lib/discordrb/container.rb', line 371 def server_emoji_delete(attributes = {}, &block) register_event(ServerEmojiDeleteEvent, attributes, block) end |
#server_emoji_update(attributes = {}) {|event| ... } ⇒ ServerEmojiUpdateEventHandler
This event is raised when an emoji is updated.
384 385 386 |
# File 'lib/discordrb/container.rb', line 384 def server_emoji_update(attributes = {}, &block) register_event(ServerEmojiUpdateEvent, attributes, block) end |
#server_update(attributes = {}) {|event| ... } ⇒ ServerUpdateEventHandler
This event is raised when a server is updated, for example if the name or region has changed.
326 327 328 |
# File 'lib/discordrb/container.rb', line 326 def server_update(attributes = {}, &block) register_event(ServerUpdateEvent, attributes, block) end |
#typing(attributes = {}) {|event| ... } ⇒ TypingEventHandler
This event is raised when somebody starts typing in a channel the bot is also in. The official Discord client would display the typing indicator for five seconds after receiving this event. If the user continues typing after five seconds, the event will be re-raised.
86 87 88 |
# File 'lib/discordrb/container.rb', line 86 def typing(attributes = {}, &block) register_event(TypingEvent, attributes, block) end |
#unknown(attributes = {}) {|event| ... } ⇒ UnknownEventHandler
This event is raised for a dispatch received over the gateway that is not currently handled otherwise by discordrb.
439 440 441 |
# File 'lib/discordrb/container.rb', line 439 def unknown(attributes = {}, &block) register_event(UnknownEvent, attributes, block) end |
#user_ban(attributes = {}) {|event| ... } ⇒ UserBanEventHandler
This event is raised when a user is banned from a server.
293 294 295 |
# File 'lib/discordrb/container.rb', line 293 def user_ban(attributes = {}, &block) register_event(UserBanEvent, attributes, block) end |
#user_unban(attributes = {}) {|event| ... } ⇒ UserUnbanEventHandler
This event is raised when a user is unbanned from a server.
304 305 306 |
# File 'lib/discordrb/container.rb', line 304 def user_unban(attributes = {}, &block) register_event(UserUnbanEvent, attributes, block) end |
#voice_state_update(attributes = {}) {|event| ... } ⇒ VoiceStateUpdateEventHandler
This event is raised when a user's voice state changes.
252 253 254 |
# File 'lib/discordrb/container.rb', line 252 def voice_state_update(attributes = {}, &block) register_event(VoiceStateUpdateEvent, attributes, block) end |