Method: ChatBot#add_hook

Defined in:
lib/chatx/hooks.rb

#add_hook(room_id, event, server: @default_server, &action) ⇒ Object

A convinant way to hook into an event.

Parameters:

  • room_id (#to_i)

    The ID of th room to listen in.

  • event (String)

    The [EVENT_SHORTHAND] for the event ID.

  • action (Proc)

    This is a block which will run when the hook is triggered. It is passed one parameter, which is the event. It is important to note that it will NOT be passed an Event, rather, it will be passed a sub event designated in Event::EVENT_CLASSES.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chatx/hooks.rb', line 40

def add_hook(room_id, event, server: @default_server, &action)
  @hooks[server] ||= {}
  @hook ||= Hook.new(self)
  if event == '*'
    @hooks[server]['*'] ||= []
    @hooks[server]['*'].push [room_id, action]
  else
    @hooks[server][EVENT_SHORTHAND.index(event)] ||= []
    @hooks[server][EVENT_SHORTHAND.index(event)].push [room_id, action]
  end
end