Class: MoonBot::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/MoonBot/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Bot

Returns a new instance of Bot.



5
6
7
8
9
10
# File 'lib/MoonBot/bot.rb', line 5

def initialize host, port
  @host = host
  @port = port
  
  @plugins = {}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/MoonBot/bot.rb', line 3

def client
  @client
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/MoonBot/bot.rb', line 3

def host
  @host
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



3
4
5
# File 'lib/MoonBot/bot.rb', line 3

def plugins
  @plugins
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/MoonBot/bot.rb', line 3

def port
  @port
end

Instance Method Details

#command(message) ⇒ Object



31
32
33
# File 'lib/MoonBot/bot.rb', line 31

def command message
  self.client.send_data(message + "\r\n")
end

#handle(message) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/MoonBot/bot.rb', line 18

def handle message
  return if message.nil?
  @plugins.each do |name, instance|
    instance.send('on_' + message[:command].downcase, message) rescue nil
    instance.send('any', message) if instance.respond_to? 'any'          
  end
end

#register(plugin) ⇒ Object



26
27
28
29
# File 'lib/MoonBot/bot.rb', line 26

def register plugin
  @plugins[plugin.to_s] = plugin.new(self)
  puts "Loading #{plugin.to_s}"
end

#startObject



12
13
14
15
16
# File 'lib/MoonBot/bot.rb', line 12

def start
  EventMachine.run do
    @client = EventMachine.connect @host, @port, Client, Proc.new {|m| handle m}
  end
end