-
Notifications
You must be signed in to change notification settings - Fork 99
Events
discordrb has a sophisticated event system that allows registration of multiple event handlers and event filtering using an easy syntax.
An event is registered by calling the appropriate method on the bot object with a block and optionally filter attributes:
bot.message(with_text: "Ping!") do |event|
event.respond "Pong!"
end
This would register an event handler for the message
event, filtered using the with_text
attribute.
Events can be filtered using attributes. An event handler will only be activated if all of the given attributes match; so for example the following event handler would only activate if the user is meew0
and the channel is #testing
:
bot.message(from: "meew0", in: "#testing") do |event|
event.respond "Event handler was activated!"
end
You can specify an array of values as the attribute value. In that case, the event will be activated if any of the values in the array match. This event handler will be activated if the user is "meew0" or "Mewtwo":
bot.message(from: ["meew0", "Mewtwo"]) do |event|
event.respond "Event handler was activated!"
end
Attributes can also be negated using the not!
function. The following event handler will only be activated if the message doesn't contain the string "unicorn":
bot.message(containing: not!("unicorn")) do |event|
event.respond "There were no unicorns in your message..."
end
Of course, you can also negate arrays. This event handler will only be activated if the message was neither sent in the "#general" channel nor in the "#testing" channel:
bot.message(in: not!(["#general", "#testing"])) do |event|
event.respond "There were no unicorns in your message..."
end
See the EventContainer documentation for information on what events are available. (All the methods marked with "event" are events.)