Class: Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/chatx/hooks.rb

Direct Known Subclasses

Room

Defined Under Namespace

Classes: Room

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, server: nil) ⇒ Hook

Returns a new instance of Hook.



70
71
72
73
# File 'lib/chatx/hooks.rb', line 70

def initialize(bot, server: nil)
  @bot = bot
  @default_server = server || @bot.default_server
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



67
68
69
# File 'lib/chatx/hooks.rb', line 67

def bot
  @bot
end

#current_roomObject

Returns the value of attribute current_room.



68
69
70
# File 'lib/chatx/hooks.rb', line 68

def current_room
  @current_room
end

Instance Method Details

#command(prefix, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/chatx/hooks.rb', line 91

def command(prefix, &block)
  on "message" do |e|
    msg = HTMLEntities.new.decode(e.content)
    if msg.downcase == prefix || msg.downcase.start_with?("#{prefix} ")
      args = msg.scan(%r{\"(.*)\"|\'(.*)\'|([^\s]*)}).flatten.reject { |a| a.to_s.empty? }[1..-1]
      block.call(*args)
    end
  end
end

#on(event, server: @default_server, &block) ⇒ Object



85
86
87
88
89
# File 'lib/chatx/hooks.rb', line 85

def on(event, server: @default_server, &block)
  @bot.hooks[server] ||= {}
  @bot.hooks[server][EVENT_SHORTHAND.index(event)] ||= []
  @bot.hooks[server][EVENT_SHORTHAND.index(event)].push ['*', block]
end

#room(room_id, autojoin: true, server: @default_server, &block) ⇒ Object



79
80
81
82
83
# File 'lib/chatx/hooks.rb', line 79

def room(room_id, autojoin: true, server: @default_server, &block)
  room = Room.new(room_id, self, server: server)
  @bot.join_room(room_id.to_i, server: server) unless !autojoin || @bot.rooms.keys.include?(room_id)
  room.instance_eval(&block)
end

#say(message, room_id = @current_room) ⇒ Object



75
76
77
# File 'lib/chatx/hooks.rb', line 75

def say(message, room_id = @current_room)
  @bot.say(message, room_id)
end