Class: Hook
- Inherits:
-
Object
- Object
- Hook
- Defined in:
- lib/chatx/hooks.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Room
Instance Attribute Summary collapse
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#current_room ⇒ Object
Returns the value of attribute current_room.
Instance Method Summary collapse
- #command(prefix, &block) ⇒ Object
-
#initialize(bot, server: nil) ⇒ Hook
constructor
A new instance of Hook.
- #on(event, server: @default_server, &block) ⇒ Object
- #room(room_id, autojoin: true, server: @default_server, &block) ⇒ Object
- #say(message, room_id = @current_room) ⇒ Object
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
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
67 68 69 |
# File 'lib/chatx/hooks.rb', line 67 def bot @bot end |
#current_room ⇒ Object
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(, room_id = @current_room) @bot.say(, room_id) end |